KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > Diffs


1 /*******************************************************************************
2  * Copyright (c) 2006 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  ******************************************************************************/

11
12 package org.eclipse.jface.internal.databinding.provisional.observable;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiff;
22 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiffEntry;
23 import org.eclipse.jface.internal.databinding.provisional.observable.set.SetDiff;
24 import org.eclipse.jface.internal.databinding.provisional.observable.value.ValueDiff;
25
26 /**
27  * @since 1.0
28  *
29  */

30 public class Diffs {
31
32     /**
33      * @param oldList
34      * @param newList
35      * @return the differences between oldList and newList
36      */

37     public static ListDiff computeListDiff(List JavaDoc oldList, List JavaDoc newList) {
38         List JavaDoc diffEntries = new ArrayList JavaDoc();
39         for (Iterator JavaDoc it = oldList.iterator(); it.hasNext();) {
40             Object JavaDoc oldElement = it.next();
41             diffEntries.add(createListDiffEntry(0, false, oldElement));
42         }
43         int i = 0;
44         for (Iterator JavaDoc it = newList.iterator(); it.hasNext();) {
45             Object JavaDoc newElement = it.next();
46             diffEntries.add(createListDiffEntry(i++, true, newElement));
47         }
48         ListDiff listDiff = createListDiff((ListDiffEntry[]) diffEntries
49                 .toArray(new ListDiffEntry[diffEntries.size()]));
50         return listDiff;
51     }
52
53     /**
54      * Checks whether the two objects are <code>null</code> -- allowing for
55      * <code>null</code>.
56      *
57      * @param left
58      * The left object to compare; may be <code>null</code>.
59      * @param right
60      * The right object to compare; may be <code>null</code>.
61      * @return <code>true</code> if the two objects are equivalent;
62      * <code>false</code> otherwise.
63      */

64     public static final boolean equals(final Object JavaDoc left, final Object JavaDoc right) {
65         return left == null ? right == null : ((right != null) && left
66                 .equals(right));
67     }
68
69     /**
70      * @param oldSet
71      * @param newSet
72      * @return a set diff
73      */

74     public static SetDiff computeSetDiff(Set JavaDoc oldSet, Set JavaDoc newSet) {
75         Set JavaDoc additions = new HashSet JavaDoc(newSet);
76         additions.removeAll(oldSet);
77         Set JavaDoc removals = new HashSet JavaDoc(oldSet);
78         removals.removeAll(newSet);
79         return createSetDiff(additions, removals);
80     }
81
82     /**
83      * @param oldValue
84      * @param newValue
85      * @return a value diff
86      */

87     public static ValueDiff createValueDiff(final Object JavaDoc oldValue,
88             final Object JavaDoc newValue) {
89         return new ValueDiff() {
90
91             public Object JavaDoc getOldValue() {
92                 return oldValue;
93             }
94
95             public Object JavaDoc getNewValue() {
96                 return newValue;
97             }
98         };
99     }
100
101     /**
102      * @param additions
103      * @param removals
104      * @return a set diff
105      */

106     public static SetDiff createSetDiff(Set JavaDoc additions, Set JavaDoc removals) {
107         final Set JavaDoc unmodifiableAdditions = Collections
108                 .unmodifiableSet(additions);
109         final Set JavaDoc unmodifiableRemovals = Collections.unmodifiableSet(removals);
110         return new SetDiff() {
111
112             public Set JavaDoc getAdditions() {
113                 return unmodifiableAdditions;
114             }
115
116             public Set JavaDoc getRemovals() {
117                 return unmodifiableRemovals;
118             }
119         };
120     }
121
122     /**
123      * @param difference
124      * @return a list diff with one differing entry
125      */

126     public static ListDiff createListDiff(ListDiffEntry difference) {
127         return createListDiff(new ListDiffEntry[] { difference });
128     }
129
130     /**
131      * @param difference1
132      * @param difference2
133      * @return a list diff with two differing entries
134      */

135     public static ListDiff createListDiff(ListDiffEntry difference1,
136             ListDiffEntry difference2) {
137         return createListDiff(new ListDiffEntry[] { difference1, difference2 });
138     }
139
140     /**
141      * @param differences
142      * @return a list diff with the given entries
143      */

144     public static ListDiff createListDiff(final ListDiffEntry[] differences) {
145         return new ListDiff() {
146             public ListDiffEntry[] getDifferences() {
147                 return differences;
148             }
149         };
150     }
151
152     /**
153      * @param position
154      * @param isAddition
155      * @param element
156      * @return a list diff entry
157      */

158     public static ListDiffEntry createListDiffEntry(final int position,
159             final boolean isAddition, final Object JavaDoc element) {
160         return new ListDiffEntry() {
161
162             public int getPosition() {
163                 return position;
164             }
165
166             public boolean isAddition() {
167                 return isAddition;
168             }
169
170             public Object JavaDoc getElement() {
171                 return element;
172             }
173         };
174     }
175
176 }
177
Popular Tags