1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable; 13 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 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 30 public class Diffs { 31 32 37 public static ListDiff computeListDiff(List oldList, List newList) { 38 List diffEntries = new ArrayList (); 39 for (Iterator it = oldList.iterator(); it.hasNext();) { 40 Object oldElement = it.next(); 41 diffEntries.add(createListDiffEntry(0, false, oldElement)); 42 } 43 int i = 0; 44 for (Iterator it = newList.iterator(); it.hasNext();) { 45 Object 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 64 public static final boolean equals(final Object left, final Object right) { 65 return left == null ? right == null : ((right != null) && left 66 .equals(right)); 67 } 68 69 74 public static SetDiff computeSetDiff(Set oldSet, Set newSet) { 75 Set additions = new HashSet (newSet); 76 additions.removeAll(oldSet); 77 Set removals = new HashSet (oldSet); 78 removals.removeAll(newSet); 79 return createSetDiff(additions, removals); 80 } 81 82 87 public static ValueDiff createValueDiff(final Object oldValue, 88 final Object newValue) { 89 return new ValueDiff() { 90 91 public Object getOldValue() { 92 return oldValue; 93 } 94 95 public Object getNewValue() { 96 return newValue; 97 } 98 }; 99 } 100 101 106 public static SetDiff createSetDiff(Set additions, Set removals) { 107 final Set unmodifiableAdditions = Collections 108 .unmodifiableSet(additions); 109 final Set unmodifiableRemovals = Collections.unmodifiableSet(removals); 110 return new SetDiff() { 111 112 public Set getAdditions() { 113 return unmodifiableAdditions; 114 } 115 116 public Set getRemovals() { 117 return unmodifiableRemovals; 118 } 119 }; 120 } 121 122 126 public static ListDiff createListDiff(ListDiffEntry difference) { 127 return createListDiff(new ListDiffEntry[] { difference }); 128 } 129 130 135 public static ListDiff createListDiff(ListDiffEntry difference1, 136 ListDiffEntry difference2) { 137 return createListDiff(new ListDiffEntry[] { difference1, difference2 }); 138 } 139 140 144 public static ListDiff createListDiff(final ListDiffEntry[] differences) { 145 return new ListDiff() { 146 public ListDiffEntry[] getDifferences() { 147 return differences; 148 } 149 }; 150 } 151 152 158 public static ListDiffEntry createListDiffEntry(final int position, 159 final boolean isAddition, final Object 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 getElement() { 171 return element; 172 } 173 }; 174 } 175 176 } 177 | Popular Tags |