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