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