1 12 13 package org.eclipse.core.databinding.observable.set; 14 15 import java.util.Collection ; 16 import java.util.Collections ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 21 import org.eclipse.core.databinding.observable.Diffs; 22 import org.eclipse.core.databinding.observable.Realm; 23 24 35 public class WritableSet extends ObservableSet { 36 37 42 public WritableSet() { 43 this(Realm.getDefault()); 44 } 45 46 53 public WritableSet(Collection c, Object elementType) { 54 this(Realm.getDefault(), new HashSet (c), elementType); 55 } 56 57 62 public WritableSet(Realm realm) { 63 this(realm, new HashSet (), null); 64 } 65 66 74 public WritableSet(Realm realm, Collection c, Object elementType) { 75 super(realm, new HashSet (c), elementType); 76 this.elementType = elementType; 77 } 78 79 public boolean add(Object o) { 80 checkRealm(); 81 boolean added = wrappedSet.add(o); 82 if (added) { 83 fireSetChange(Diffs.createSetDiff(Collections.singleton(o), Collections.EMPTY_SET)); 84 } 85 return added; 86 } 87 88 public boolean addAll(Collection c) { 89 checkRealm(); 90 Set adds = new HashSet (); 91 Iterator it = c.iterator(); 92 while (it.hasNext()) { 93 Object element = it.next(); 94 if (wrappedSet.add(element)) { 95 adds.add(element); 96 } 97 } 98 if (adds.size() > 0) { 99 fireSetChange(Diffs.createSetDiff(adds, Collections.EMPTY_SET)); 100 return true; 101 } 102 return false; 103 } 104 105 public boolean remove(Object o) { 106 checkRealm(); 107 boolean removed = wrappedSet.remove(o); 108 if (removed) { 109 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, Collections 110 .singleton(o))); 111 } 112 return removed; 113 } 114 115 public boolean removeAll(Collection c) { 116 checkRealm(); 117 Set removes = new HashSet (); 118 Iterator it = c.iterator(); 119 while (it.hasNext()) { 120 Object element = it.next(); 121 if (wrappedSet.remove(element)) { 122 removes.add(element); 123 } 124 } 125 if (removes.size() > 0) { 126 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); 127 return true; 128 } 129 return false; 130 } 131 132 public boolean retainAll(Collection c) { 133 checkRealm(); 134 Set removes = new HashSet (); 135 Iterator it = wrappedSet.iterator(); 136 while (it.hasNext()) { 137 Object element = it.next(); 138 if (!c.contains(element)) { 139 it.remove(); 140 removes.add(element); 141 } 142 } 143 if (removes.size() > 0) { 144 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); 145 return true; 146 } 147 return false; 148 } 149 150 public void clear() { 151 checkRealm(); 152 Set removes = new HashSet (wrappedSet); 153 wrappedSet.clear(); 154 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); 155 } 156 157 161 public static WritableSet withElementType(Object elementType) { 162 return new WritableSet(Realm.getDefault(), new HashSet (), elementType); 163 } 164 } 165 | Popular Tags |