1 16 package org.apache.commons.collections.map; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.util.AbstractList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ListIterator ; 27 import java.util.Map ; 28 29 import org.apache.commons.collections.iterators.UnmodifiableIterator; 30 import org.apache.commons.collections.iterators.UnmodifiableListIterator; 31 import org.apache.commons.collections.list.UnmodifiableList; 32 33 58 public class LinkedMap 59 extends AbstractLinkedMap implements Serializable , Cloneable { 60 61 62 private static final long serialVersionUID = 9077234323521161066L; 63 64 67 public LinkedMap() { 68 super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD); 69 } 70 71 77 public LinkedMap(int initialCapacity) { 78 super(initialCapacity); 79 } 80 81 90 public LinkedMap(int initialCapacity, float loadFactor) { 91 super(initialCapacity, loadFactor); 92 } 93 94 100 public LinkedMap(Map map) { 101 super(map); 102 } 103 104 110 public Object clone() { 111 return super.clone(); 112 } 113 114 117 private void writeObject(ObjectOutputStream out) throws IOException { 118 out.defaultWriteObject(); 119 doWriteObject(out); 120 } 121 122 125 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 126 in.defaultReadObject(); 127 doReadObject(in); 128 } 129 130 138 public Object get(int index) { 139 return getEntry(index).getKey(); 140 } 141 142 149 public Object getValue(int index) { 150 return getEntry(index).getValue(); 151 } 152 153 159 public int indexOf(Object key) { 160 key = convertKey(key); 161 int i = 0; 162 for (LinkEntry entry = header.after; entry != header; entry = entry.after, i++) { 163 if (isEqualKey(key, entry.key)) { 164 return i; 165 } 166 } 167 return -1; 168 } 169 170 178 public Object remove(int index) { 179 return remove(get(index)); 180 } 181 182 197 public List asList() { 198 return new LinkedMapList(this); 199 } 200 201 204 static class LinkedMapList extends AbstractList { 205 206 final LinkedMap parent; 207 208 LinkedMapList(LinkedMap parent) { 209 this.parent = parent; 210 } 211 212 public int size() { 213 return parent.size(); 214 } 215 216 public Object get(int index) { 217 return parent.get(index); 218 } 219 220 public boolean contains(Object obj) { 221 return parent.containsKey(obj); 222 } 223 224 public int indexOf(Object obj) { 225 return parent.indexOf(obj); 226 } 227 228 public int lastIndexOf(Object obj) { 229 return parent.indexOf(obj); 230 } 231 232 public boolean containsAll(Collection coll) { 233 return parent.keySet().containsAll(coll); 234 } 235 236 public Object remove(int index) { 237 throw new UnsupportedOperationException (); 238 } 239 240 public boolean remove(Object obj) { 241 throw new UnsupportedOperationException (); 242 } 243 244 public boolean removeAll(Collection coll) { 245 throw new UnsupportedOperationException (); 246 } 247 248 public boolean retainAll(Collection coll) { 249 throw new UnsupportedOperationException (); 250 } 251 252 public void clear() { 253 throw new UnsupportedOperationException (); 254 } 255 256 public Object [] toArray() { 257 return parent.keySet().toArray(); 258 } 259 260 public Object [] toArray(Object [] array) { 261 return parent.keySet().toArray(array); 262 } 263 264 public Iterator iterator() { 265 return UnmodifiableIterator.decorate(parent.keySet().iterator()); 266 } 267 268 public ListIterator listIterator() { 269 return UnmodifiableListIterator.decorate(super.listIterator()); 270 } 271 272 public ListIterator listIterator(int fromIndex) { 273 return UnmodifiableListIterator.decorate(super.listIterator(fromIndex)); 274 } 275 276 public List subList(int fromIndexInclusive, int toIndexExclusive) { 277 return UnmodifiableList.decorate(super.subList(fromIndexInclusive, toIndexExclusive)); 278 } 279 } 280 281 } 282 | Popular Tags |