annotate org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/list/ListDiff.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 383ce7bd736b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
1 /*******************************************************************************
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
2 * Copyright (c) 2006, 2008 IBM Corporation and others.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
3 * All rights reserved. This program and the accompanying materials
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
5 * which accompanies this distribution, and is available at
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
7 *
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
8 * Contributors:
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
9 * IBM Corporation - initial API and implementation
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
10 * Matthew Hall - bug 208858
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
11 *******************************************************************************/
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
12
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
13 module org.eclipse.core.databinding.observable.list.ListDiff;
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
14
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
15 import java.lang.all;
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
16
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
17 import org.eclipse.core.internal.databinding.Util;
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
18
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
19 /**
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
20 * Object describing a diff between two lists.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
21 *
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
22 * @since 1.0
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
23 */
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
24 public abstract class ListDiff {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
25
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
26 /**
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
27 * Returns a ListDiffEntry array representing the differences in the list,
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
28 * in the order they are to be processed.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
29 *
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
30 * @return a ListDiffEntry array representing the differences in the list,
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
31 * in the order they are to be processed.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
32 */
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
33 public abstract ListDiffEntry[] getDifferences();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
34
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
35 /**
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
36 * Traverses the {@link #getDifferences()} array, calling the appropriate
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
37 * method in <code>visitor</code> for each difference.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
38 * <ol>
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
39 * <li>{@link ListDiffVisitor#handleReplace(int, Object, Object)} is called
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
40 * whenever a remove entry is immediately followed by an add entry which
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
41 * shares the same list index.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
42 * <li>{@link ListDiffVisitor#handleMove(int, int, Object)} is called
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
43 * whenever a remove entry is immediately followed by an add entry with an
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
44 * equivalent element.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
45 * <li>{@link ListDiffVisitor#handleRemove(int, Object)} is called whenever
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
46 * a remove entry does not match conditions 1 or 2.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
47 * <li>{@link ListDiffVisitor#handleAdd(int, Object)} is called whenever an
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
48 * add entry does not match conditions in 1 or 2.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
49 * </ol>
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
50 *
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
51 * @param visitor
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
52 * the visitor to receive callbacks.
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
53 * @see ListDiffVisitor
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
54 * @since 1.1
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
55 */
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
56 public void accept(ListDiffVisitor visitor) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
57 ListDiffEntry[] differences = getDifferences();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
58 for (int i = 0; i < differences.length; i++) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
59 ListDiffEntry entry = differences[i];
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
60 int position = entry.getPosition();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
61 Object element = entry.getElement();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
62 bool addition = entry.isAddition();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
63
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
64 if (!addition && i + 1 < differences.length) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
65 ListDiffEntry entry2 = differences[i + 1];
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
66 if (entry2.isAddition()) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
67 int position2 = entry2.getPosition();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
68 Object element2 = entry2.getElement();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
69 if (position is position2) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
70 visitor.handleReplace(position, element, element2);
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
71 i++;
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
72 continue;
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
73 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
74 if (Util.equals(element, element2)) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
75 visitor.handleMove(position, position2, element);
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
76 i++;
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
77 continue;
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
78 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
79 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
80 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
81 if (addition)
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
82 visitor.handleAdd(position, element);
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
83 else
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
84 visitor.handleRemove(position, element);
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
85 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
86 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
87
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
88 /**
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
89 * @see java.lang.Object#toString()
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
90 */
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
91 public String toString() {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
92 ListDiffEntry[] differences = getDifferences();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
93 StringBuffer buffer = new StringBuffer();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
94 buffer.append(getClass().getName());
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
95
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
96 if (differences is null || differences.length is 0) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
97 buffer
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
98 .append("{}"); //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
99 } else {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
100 buffer
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
101 .append("{"); //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
102
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
103 for (int i = 0; i < differences.length; i++) {
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
104 if (i > 0)
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
105 buffer.append(", "); //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
106
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
107 buffer
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
108 .append("difference[") //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
109 .append(i)
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
110 .append("] [") //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
111 .append(differences[i] !is null ? differences[i].toString() : "null") //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
112 .append("]"); //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
113 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
114 buffer.append("}"); //$NON-NLS-1$
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
115 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
116
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
117 return buffer.toString();
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
118 }
0a55d2d5a946 Added file for databinding
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
119 }