1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.set; 13 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Set ; 19 20 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs; 21 22 26 public class WritableSet extends ObservableSet { 27 28 31 public WritableSet() { 32 this(Object .class); 33 } 34 35 38 public WritableSet(Collection c) { 39 this(c, Object .class); 40 } 41 42 46 public WritableSet(Collection c, Object elementType) { 47 super(new HashSet (c), elementType); 48 this.elementType = elementType; 49 } 50 51 54 public WritableSet(Object elementType) { 55 super(new HashSet (), elementType); 56 } 57 58 public boolean add(Object o) { 59 boolean added = wrappedSet.add(o); 60 if (added) { 61 fireSetChange(Diffs.createSetDiff(Collections.singleton(o), Collections.EMPTY_SET)); 62 } 63 return added; 64 } 65 66 public boolean addAll(Collection c) { 67 Set adds = new HashSet (); 68 Iterator it = c.iterator(); 69 while (it.hasNext()) { 70 Object element = it.next(); 71 if (wrappedSet.add(element)) { 72 adds.add(element); 73 } 74 } 75 if (adds.size() > 0) { 76 fireSetChange(Diffs.createSetDiff(adds, Collections.EMPTY_SET)); 77 return true; 78 } 79 return false; 80 } 81 82 public boolean remove(Object o) { 83 boolean removed = wrappedSet.remove(o); 84 if (removed) { 85 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, Collections 86 .singleton(o))); 87 } 88 return removed; 89 } 90 91 public boolean removeAll(Collection c) { 92 Set removes = new HashSet (); 93 Iterator it = c.iterator(); 94 while (it.hasNext()) { 95 Object element = it.next(); 96 if (wrappedSet.remove(element)) { 97 removes.add(element); 98 } 99 } 100 if (removes.size() > 0) { 101 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); 102 return true; 103 } 104 return false; 105 } 106 107 public boolean retainAll(Collection c) { 108 Set removes = new HashSet (); 109 Iterator it = wrappedSet.iterator(); 110 while (it.hasNext()) { 111 Object element = it.next(); 112 if (!c.contains(element)) { 113 it.remove(); 114 removes.add(element); 115 } 116 } 117 if (removes.size() > 0) { 118 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); 119 return true; 120 } 121 return false; 122 } 123 124 public void clear() { 125 Set removes = new HashSet (wrappedSet); 126 wrappedSet.clear(); 127 fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes)); 128 } 129 130 } 131 | Popular Tags |