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