1 19 package bak.pcj.map; 20 21 import bak.pcj.hash.DefaultLongHashFunction; 22 23 35 public abstract class AbstractLongKeyMap implements LongKeyMap { 36 37 38 protected AbstractLongKeyMap() { } 39 40 public void clear() { 41 LongKeyMapIterator i = entries(); 42 while (i.hasNext()) { 43 i.next(); 44 i.remove(); 45 } 46 } 47 48 public Object remove(long key) { 49 LongKeyMapIterator i = entries(); 50 while (i.hasNext()) { 51 i.next(); 52 if (i.getKey() == key) { 53 Object value = i.getValue(); 54 i.remove(); 55 return value; 56 } 57 } 58 return null; 59 } 60 61 public void putAll(LongKeyMap map) { 62 LongKeyMapIterator i = map.entries(); 63 while (i.hasNext()) { 64 i.next(); 65 put(i.getKey(), i.getValue()); 66 } 67 } 68 69 public boolean containsKey(long key) { 70 LongKeyMapIterator i = entries(); 71 while (i.hasNext()) { 72 i.next(); 73 if (i.getKey() == key) 74 return true; 75 } 76 return false; 77 } 78 79 public Object get(long key) { 80 LongKeyMapIterator i = entries(); 81 while (i.hasNext()) { 82 i.next(); 83 if (i.getKey() == key) 84 return i.getValue(); 85 } 86 return null; 87 } 88 89 public boolean containsValue(Object value) { 90 LongKeyMapIterator i = entries(); 91 if (value == null) { 92 while (i.hasNext()) { 93 i.next(); 94 if (value == null) 95 return true; 96 } 97 } else { 98 while (i.hasNext()) { 99 i.next(); 100 if (value.equals(i.getValue())) 101 return true; 102 } 103 } 104 return false; 105 } 106 107 public boolean equals(Object obj) { 108 if (!(obj instanceof LongKeyMap)) 109 return false; 110 LongKeyMap map = (LongKeyMap)obj; 111 if (size() != map.size()) 112 return false; 113 LongKeyMapIterator i = entries(); 114 while (i.hasNext()) { 115 i.next(); 116 long k = i.getKey(); 117 Object v = i.getValue(); 118 if (v == null) { 119 if (map.get(k) != null) 120 return false; 121 if (!map.containsKey(k)) 122 return false; 123 } else { 124 if (!v.equals(map.get(k))) 125 return false; 126 } 127 } 128 return true; 129 } 130 131 public int hashCode() { 132 int h = 0; 133 LongKeyMapIterator i = entries(); 134 while (i.hasNext()) { 135 i.next(); 136 h += (DefaultLongHashFunction.INSTANCE.hash(i.getKey()) ^ i.getValue().hashCode()); 137 } 138 return h; 139 } 140 141 public boolean isEmpty() 142 { return size() == 0; } 143 144 public int size() { 145 int size = 0; 146 LongKeyMapIterator i = entries(); 147 while (i.hasNext()) { 148 i.next(); 149 size++; 150 } 151 return size; 152 } 153 154 159 public String toString() { 160 StringBuffer s = new StringBuffer (); 161 s.append('['); 162 LongKeyMapIterator i = entries(); 163 while (i.hasNext()) { 164 if (s.length() > 1) 165 s.append(','); 166 i.next(); 167 s.append(String.valueOf(i.getKey())); 168 s.append("->"); 169 s.append(String.valueOf(i.getValue())); 170 } 171 s.append(']'); 172 return s.toString(); 173 } 174 175 180 public void trimToSize() 181 { } 182 183 } | Popular Tags |