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;
|
|
18
|
|
19 import org.eclipse.core.internal.databinding.Util;
|
|
20
|
|
21 /**
|
|
22 * Object describing a diff between two lists.
|
|
23 *
|
|
24 * @since 1.0
|
|
25 */
|
|
26 public abstract class ListDiff {
|
|
27
|
|
28 /**
|
|
29 * Returns a ListDiffEntry array representing the differences in the list,
|
|
30 * in the order they are to be processed.
|
|
31 *
|
|
32 * @return a ListDiffEntry array representing the differences in the list,
|
|
33 * in the order they are to be processed.
|
|
34 */
|
|
35 public abstract ListDiffEntry[] getDifferences();
|
|
36
|
|
37 /**
|
|
38 * Traverses the {@link #getDifferences()} array, calling the appropriate
|
|
39 * method in <code>visitor</code> for each difference.
|
|
40 * <ol>
|
|
41 * <li>{@link ListDiffVisitor#handleReplace(int, Object, Object)} is called
|
|
42 * whenever a remove entry is immediately followed by an add entry which
|
|
43 * shares the same list index.
|
|
44 * <li>{@link ListDiffVisitor#handleMove(int, int, Object)} is called
|
|
45 * whenever a remove entry is immediately followed by an add entry with an
|
|
46 * equivalent element.
|
|
47 * <li>{@link ListDiffVisitor#handleRemove(int, Object)} is called whenever
|
|
48 * a remove entry does not match conditions 1 or 2.
|
|
49 * <li>{@link ListDiffVisitor#handleAdd(int, Object)} is called whenever an
|
|
50 * add entry does not match conditions in 1 or 2.
|
|
51 * </ol>
|
|
52 *
|
|
53 * @param visitor
|
|
54 * the visitor to receive callbacks.
|
|
55 * @see ListDiffVisitor
|
|
56 * @since 1.1
|
|
57 */
|
|
58 public void accept(ListDiffVisitor visitor) {
|
|
59 ListDiffEntry[] differences = getDifferences();
|
|
60 for (int i = 0; i < differences.length; i++) {
|
|
61 ListDiffEntry entry = differences[i];
|
|
62 int position = entry.getPosition();
|
|
63 Object element = entry.getElement();
|
|
64 bool addition = entry.isAddition();
|
|
65
|
|
66 if (!addition && i + 1 < differences.length) {
|
|
67 ListDiffEntry entry2 = differences[i + 1];
|
|
68 if (entry2.isAddition()) {
|
|
69 int position2 = entry2.getPosition();
|
|
70 Object element2 = entry2.getElement();
|
|
71 if (position is position2) {
|
|
72 visitor.handleReplace(position, element, element2);
|
|
73 i++;
|
|
74 continue;
|
|
75 }
|
|
76 if (Util.equals(element, element2)) {
|
|
77 visitor.handleMove(position, position2, element);
|
|
78 i++;
|
|
79 continue;
|
|
80 }
|
|
81 }
|
|
82 }
|
|
83 if (addition)
|
|
84 visitor.handleAdd(position, element);
|
|
85 else
|
|
86 visitor.handleRemove(position, element);
|
|
87 }
|
|
88 }
|
|
89
|
|
90 /**
|
|
91 * @see java.lang.Object#toString()
|
|
92 */
|
|
93 public String toString() {
|
|
94 ListDiffEntry[] differences = getDifferences();
|
|
95 StringBuffer buffer = new StringBuffer();
|
|
96 buffer.append(getClass().getName());
|
|
97
|
|
98 if (differences is null || differences.length is 0) {
|
|
99 buffer
|
|
100 .append("{}"); //$NON-NLS-1$
|
|
101 } else {
|
|
102 buffer
|
|
103 .append("{"); //$NON-NLS-1$
|
|
104
|
|
105 for (int i = 0; i < differences.length; i++) {
|
|
106 if (i > 0)
|
|
107 buffer.append(", "); //$NON-NLS-1$
|
|
108
|
|
109 buffer
|
|
110 .append("difference[") //$NON-NLS-1$
|
|
111 .append(i)
|
|
112 .append("] [") //$NON-NLS-1$
|
|
113 .append(differences[i] !is null ? differences[i].toString() : "null") //$NON-NLS-1$
|
|
114 .append("]"); //$NON-NLS-1$
|
|
115 }
|
|
116 buffer.append("}"); //$NON-NLS-1$
|
|
117 }
|
|
118
|
|
119 return buffer.toString();
|
|
120 }
|
|
121 }
|