1 package org.jahia.utils; 2 3 import java.util.*; 4 import java.io.Serializable ; 5 6 7 17 18 public class InsertionSortedMap extends AbstractMap implements Serializable { 19 20 private ArrayList internalList = new ArrayList(); 21 22 private class Entry implements Map.Entry, Serializable { 23 24 private Object key; 25 private Object value; 26 27 public Entry(Object key, Object value) { 28 this.key = key; 29 this.value = value; 30 } 31 32 public boolean equals (Object o) { 33 if (! (o instanceof Entry)) { 34 return false; 35 } 36 Entry e2 = (Entry) o; 37 if ( (this.getKey() == null ? 38 e2.getKey() == null : this.getKey().equals(e2.getKey())) && 39 (this.getValue() == null ? 40 e2.getValue() == null : this.getValue().equals(e2.getValue()))) { 41 return true; 42 } else { 43 return false; 44 } 45 46 } 47 48 public int hashCode() { 49 return ((this.getKey()==null ? 0 : this.getKey().hashCode()) ^ 50 (this.getValue()==null ? 0 : this.getValue().hashCode())); 51 } 52 53 public Object getKey() { 54 return key; 55 } 56 57 public Object getValue() { 58 return value; 59 } 60 61 public Object setValue(Object value) { 62 Object oldValue = this.value; 63 this.value = value; 64 return oldValue; 65 } 66 67 } 68 69 public InsertionSortedMap() { 70 } 71 72 public InsertionSortedMap(Map t) { 73 Iterator sourceEntryIter = t.entrySet().iterator(); 75 while (sourceEntryIter.hasNext()) { 76 Map.Entry curEntry = (Map.Entry) sourceEntryIter.next(); 77 internalList.add(curEntry.getKey()); 78 } 79 } 80 81 public Set entrySet() { 82 InsertionSortedSet insertionSortedSet = new InsertionSortedSet(); 83 insertionSortedSet.setInternalList(internalList); 84 return insertionSortedSet; 85 } 86 87 public Object put(Object key, 88 Object value) 89 throws UnsupportedOperationException , 90 ClassCastException , 91 IllegalArgumentException , 92 NullPointerException { 93 int pos = findKey(key); 94 if (pos == -1) { 95 Entry newEntry = new Entry(key, value); 96 internalList.add(newEntry); 97 return null; 98 } else { 99 Entry existingEntry = (Entry) internalList.get(pos); 100 Object oldValue = existingEntry.getValue(); 101 existingEntry.setValue(value); 102 return oldValue; 103 } 104 } 105 106 private int findKey(Object key) { 107 int pos = -1; 108 Iterator listIter = internalList.iterator(); 109 while (listIter.hasNext()) { 110 Map.Entry curEntry = (Map.Entry) listIter.next(); 111 pos++; 112 if (curEntry.getKey().equals(key)) { 113 return pos; 114 } 115 } 116 return -1; 117 } 118 119 } 120 | Popular Tags |