1 4 package com.tcclient.util; 5 6 import com.tc.object.SerializationUtil; 7 import com.tc.object.bytecode.ManagerUtil; 8 import com.tc.util.SetIteratorWrapper; 9 10 import java.lang.reflect.Array ; 11 import java.util.Collection ; 12 import java.util.Comparator ; 13 import java.util.Iterator ; 14 import java.util.SortedSet ; 15 16 19 public final class SortedViewSetWrapper implements SortedSet { 20 public final static String CLASS_SLASH = "com/tcclient/util/SortedViewSetWrapper"; 21 22 private transient SortedSet viewSet; 24 private SortedSet originalSet; 26 27 private Object fromKey; 28 private Object toKey; 29 private boolean head; 30 31 public SortedViewSetWrapper(SortedSet originalSet, SortedSet viewSet, Object fromKey, Object toKey) { 32 this.originalSet = originalSet; 33 this.viewSet = viewSet; 34 this.fromKey = fromKey; 35 this.toKey = toKey; 36 } 37 38 public SortedViewSetWrapper(SortedSet originalSet, SortedSet viewSet, Object key, boolean head) { 39 this.originalSet = originalSet; 40 this.viewSet = viewSet; 41 this.head = head; 42 if (head) { 43 this.toKey = key; 44 } else { 45 this.fromKey = key; 46 } 47 } 48 49 private SortedSet getViewSet() { 50 if (viewSet == null) { 51 if (head) { 52 viewSet = ((SortedViewSetWrapper) originalSet.headSet(toKey)).viewSet; 53 } else if (toKey == null) { 54 viewSet = ((SortedViewSetWrapper) originalSet.tailSet(fromKey)).viewSet; 55 } else { 56 viewSet = ((SortedViewSetWrapper) originalSet.subSet(fromKey, toKey)).viewSet; 57 } 58 } 59 return viewSet; 60 } 61 62 public final boolean add(Object o) { 63 ManagerUtil.checkWriteAccess(originalSet); 64 boolean flag = getViewSet().add(o); 65 if (flag) { 66 ManagerUtil.logicalInvoke(originalSet, SerializationUtil.ADD_SIGNATURE, new Object [] { o }); 67 } 68 69 return flag; 70 } 71 72 public final boolean addAll(Collection c) { 73 ManagerUtil.checkWriteAccess(originalSet); 74 boolean flag = getViewSet().addAll(c); 75 if (flag) { 76 ManagerUtil.logicalInvoke(originalSet, SerializationUtil.ADD_ALL_SIGNATURE, new Object [] { c }); 77 } 78 79 return flag; 80 } 81 82 public final void clear() { 83 ManagerUtil.checkWriteAccess(originalSet); 84 Object [] clearSet = getViewSet().toArray(); 85 getViewSet().clear(); 86 ManagerUtil.logicalInvoke(originalSet, SerializationUtil.REMOVE_ALL_SIGNATURE, clearSet); 87 } 88 89 public final boolean contains(Object o) { 90 return getViewSet().contains(o); 91 } 92 93 public final boolean containsAll(Collection c) { 94 return getViewSet().containsAll(c); 95 } 96 97 public final boolean equals(Object o) { 98 return getViewSet().equals(o); 99 } 100 101 public final int hashCode() { 102 return getViewSet().hashCode(); 103 } 104 105 public final boolean isEmpty() { 106 return getViewSet().isEmpty(); 107 } 108 109 public final Iterator iterator() { 110 return new SetIteratorWrapper(getViewSet().iterator(), originalSet); 111 } 112 113 public final boolean remove(Object o) { 114 ManagerUtil.checkWriteAccess(originalSet); 115 boolean removed = getViewSet().remove(o); 116 if (removed) { 117 ManagerUtil.logicalInvoke(originalSet, SerializationUtil.REMOVE_SIGNATURE, new Object [] { o }); 118 } 119 return removed; 120 } 121 122 public final boolean removeAll(Collection c) { 123 boolean modified = false; 124 125 if (size() > c.size()) { 126 for (Iterator i = c.iterator(); i.hasNext();) 127 modified |= remove(i.next()); 128 } else { 129 for (Iterator i = iterator(); i.hasNext();) { 130 if (c.contains(i.next())) { 131 i.remove(); 132 modified = true; 133 } 134 } 135 } 136 return modified; 137 } 138 139 public final boolean retainAll(Collection c) { 140 boolean modified = false; 141 Iterator i = iterator(); 142 while (i.hasNext()) { 143 if (!c.contains(i.next())) { 144 i.remove(); 145 modified = true; 146 } 147 } 148 return modified; 149 150 } 151 152 public final int size() { 153 return getViewSet().size(); 154 } 155 156 public final Object [] toArray() { 157 return getViewSet().toArray(); 158 } 159 160 public final Object [] toArray(Object [] a) { 161 int size = size(); 162 if (a.length < size) a = (Object []) Array.newInstance(((Object ) (a)).getClass().getComponentType(), size); 163 164 int index = 0; 165 for (Iterator iterator = iterator(); iterator.hasNext();) { 166 ManagerUtil.objectArrayChanged(a, index++, iterator.next()); 167 } 168 169 if (a.length > size) { 170 a[size] = null; 171 } 172 return a; 173 } 174 175 public final Comparator comparator() { 176 return getViewSet().comparator(); 177 } 178 179 public final SortedSet subSet(Object fromElement, Object toElement) { 180 return new SortedViewSetWrapper(originalSet, 181 ((SortedViewSetWrapper) viewSet.subSet(fromElement, toElement)).viewSet, 182 fromElement, toElement); 183 } 184 185 public final SortedSet headSet(Object toElement) { 186 return new SortedViewSetWrapper(originalSet, ((SortedViewSetWrapper) viewSet.headSet(toElement)).viewSet, 187 toElement, true); 188 } 189 190 public final SortedSet tailSet(Object fromElement) { 191 return new SortedViewSetWrapper(originalSet, ((SortedViewSetWrapper) viewSet.tailSet(fromElement)).viewSet, 192 fromElement, false); 193 } 194 195 public final Object first() { 196 return getViewSet().first(); 197 } 198 199 public final Object last() { 200 return getViewSet().last(); 201 } 202 203 public String toString() { 204 return viewSet.toString(); 205 } 206 } 207 | Popular Tags |