1 24 package org.ofbiz.base.util.collections; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Set ; 33 34 41 public class OrderedMap extends HashMap { 42 43 private List orderedKeys = new LinkedList (); 44 45 48 public Set keySet() { 49 return new OrderedSet(orderedKeys); 50 } 51 52 55 public List getOrderedKeys() { 56 return new LinkedList (this.orderedKeys); 57 } 58 59 62 public Object put(Object key, Object value) { 63 if (!orderedKeys.contains(key)) 64 orderedKeys.add(key); 65 return super.put(key, value); 66 } 67 68 71 public void clear() { 72 super.clear(); 73 orderedKeys.clear(); 74 } 75 76 79 public Object remove(Object key) { 80 if (orderedKeys.contains(key)) 81 orderedKeys.remove(key); 82 return super.remove(key); 83 } 84 85 88 public Collection values() { 89 Iterator i = orderedKeys.iterator(); 90 if (!i.hasNext()) { 91 return null; 92 } 93 94 List values = new ArrayList (); 95 while (i.hasNext()) { 96 values.add(this.get(i.next())); 97 } 98 return (Collection ) values; 99 } 100 101 public int indexOf(Object key) { 102 return orderedKeys.indexOf(key); 103 } 104 } 105 | Popular Tags |