1 21 24 package org.lobobrowser.util; 25 26 import java.util.*; 27 28 public class ListSet implements List, Set { 29 private final List list = new ArrayList(); 30 private final Set set = new HashSet(); 31 32 public ListSet() { 33 super(); 34 } 35 36 39 public void add(int index, Object element) { 40 if(this.set.add(element)) { 41 list.add(index, element); 42 } 43 } 44 45 48 public boolean add(Object o) { 49 if(this.set.add(o)) { 50 return this.list.add(o); 51 } 52 else { 53 return false; 54 } 55 } 56 57 60 public boolean addAll(Collection c) { 61 boolean changed = false; 62 Iterator i = c.iterator(); 63 while(i.hasNext()) { 64 Object element = i.next(); 65 if(this.add(element)) { 66 changed = true; 67 } 68 } 69 return changed; 70 } 71 72 75 public boolean addAll(int index, Collection c) { 76 boolean changed = false; 77 int insertIndex = index; 78 Iterator i = c.iterator(); 79 while(i.hasNext()) { 80 Object element = i.next(); 81 if(this.set.add(element)) { 82 this.list.add(insertIndex++, element); 83 changed = true; 84 } 85 } 86 return changed; 87 } 88 89 92 public void clear() { 93 this.set.clear(); 94 this.list.clear(); 95 } 96 97 100 public boolean contains(Object o) { 101 return this.set.contains(o); 102 } 103 104 107 public boolean containsAll(Collection c) { 108 return this.set.containsAll(c); 109 } 110 111 114 public Object get(int index) { 115 return this.list.get(index); 116 } 117 118 121 public int indexOf(Object o) { 122 return this.list.indexOf(o); 123 } 124 125 128 public boolean isEmpty() { 129 return this.set.isEmpty(); 130 } 131 132 135 public Iterator iterator() { 136 return this.list.iterator(); 137 } 138 139 142 public int lastIndexOf(Object o) { 143 return this.list.lastIndexOf(o); 144 } 145 146 149 public ListIterator listIterator() { 150 return this.list.listIterator(); 151 } 152 153 156 public ListIterator listIterator(int index) { 157 return this.list.listIterator(index); 158 } 159 160 163 public Object remove(int index) { 164 Object element = this.list.remove(index); 165 if(element != null) { 166 this.set.remove(element); 167 } 168 return element; 169 } 170 171 174 public boolean remove(Object o) { 175 if(this.set.remove(o)) { 176 this.list.remove(o); 177 return true; 178 } 179 else { 180 return false; 181 } 182 } 183 184 187 public boolean removeAll(Collection c) { 188 if(this.set.removeAll(c)) { 189 this.list.removeAll(c); 190 return true; 191 } 192 else { 193 return false; 194 } 195 } 196 197 200 public boolean retainAll(Collection c) { 201 if(this.set.retainAll(c)) { 202 this.list.retainAll(c); 203 return true; 204 } 205 else { 206 return false; 207 } 208 } 209 210 213 public Object set(int index, Object element) { 214 this.set.add(element); 215 return this.list.set(index, element); 216 } 217 218 221 public int size() { 222 return this.list.size(); 223 } 224 225 228 public List subList(int fromIndex, int toIndex) { 229 return this.list.subList(fromIndex, toIndex); 230 } 231 232 235 public Object [] toArray() { 236 return this.list.toArray(); 237 } 238 239 242 public Object [] toArray(Object [] a) { 243 return this.list.toArray(a); 244 } 245 246 public boolean equals(Object other) { 247 return other instanceof ListSet && this.list.equals(((ListSet) other).list); 248 } 249 250 public int hashCode() { 251 return this.list.hashCode(); 252 } 253 } 254 | Popular Tags |