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