mirror of
https://github.com/corda/corda.git
synced 2025-06-17 06:38:21 +00:00
sketch of serialization/deserialization code (broken)
This commit is contained in:
@ -3,17 +3,17 @@ package java.util;
|
||||
public class HashMap<K, V> implements Map<K, V> {
|
||||
private int size;
|
||||
private Cell[] array;
|
||||
private final CellFactory factory;
|
||||
private final Helper helper;
|
||||
|
||||
HashMap(int capacity, CellFactory<K, V> factory) {
|
||||
HashMap(int capacity, Helper<K, V> helper) {
|
||||
if (capacity > 0) {
|
||||
array = new Cell[nextPowerOfTwo(capacity)];
|
||||
}
|
||||
this.factory = factory;
|
||||
this.helper = helper;
|
||||
}
|
||||
|
||||
public HashMap(int capacity) {
|
||||
this(capacity, new MyCellFactory());
|
||||
this(capacity, new MyHelper());
|
||||
}
|
||||
|
||||
public HashMap() {
|
||||
@ -30,14 +30,6 @@ public class HashMap<K, V> implements Map<K, V> {
|
||||
return size;
|
||||
}
|
||||
|
||||
private static int hash(Object a) {
|
||||
return (a == null ? 0 : a.hashCode());
|
||||
}
|
||||
|
||||
private static boolean equal(Object a, Object b) {
|
||||
return (a == null && b == null) || (a != null && a.equals(b));
|
||||
}
|
||||
|
||||
private void resize() {
|
||||
if (array == null || size >= array.length * 2) {
|
||||
resize(array == null ? 16 : array.length * 2);
|
||||
@ -115,7 +107,7 @@ public class HashMap<K, V> implements Map<K, V> {
|
||||
private Cell<K, V> putCell(K key, V value) {
|
||||
Cell<K, V> c = find(key);
|
||||
if (c == null) {
|
||||
insert(factory.make(key, value, null));
|
||||
insert(helper.make(key, value, null));
|
||||
} else {
|
||||
V old = c.getValue();
|
||||
c.setValue(value);
|
||||
@ -196,11 +188,15 @@ public class HashMap<K, V> implements Map<K, V> {
|
||||
public void setNext(HashMap.Cell<K, V> next);
|
||||
}
|
||||
|
||||
interface CellFactory<K, V> {
|
||||
interface Helper<K, V> {
|
||||
public Cell<K, V> make(K key, V value, Cell<K, V> next);
|
||||
|
||||
public int hash(K key);
|
||||
|
||||
public boolean equal(K a, K b);
|
||||
}
|
||||
|
||||
private static class MyCell<K, V> implements Cell<K, V> {
|
||||
private class MyCell<K, V> implements Cell<K, V> {
|
||||
public final K key;
|
||||
public V value;
|
||||
public Cell<K, V> next;
|
||||
@ -232,14 +228,22 @@ public class HashMap<K, V> implements Map<K, V> {
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return (key == null ? 0 : key.hashCode());
|
||||
return helper.hash(key);
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyCellFactory<K, V> implements CellFactory<K, V> {
|
||||
static class MyHelper<K, V> implements Helper<K, V> {
|
||||
public Cell<K, V> make(K key, V value, Cell<K, V> next) {
|
||||
return new MyCell(key, value, next);
|
||||
}
|
||||
|
||||
public int hash(K a) {
|
||||
return (a == null ? 0 : a.hashCode());
|
||||
}
|
||||
|
||||
public boolean equal(K a, K b) {
|
||||
return (a == null && b == null) || (a != null && a.equals(b));
|
||||
}
|
||||
}
|
||||
|
||||
private class EntrySet implements Set<Entry<K, V>> {
|
||||
|
65
classpath/java/util/IdentityHashMap.java
Normal file
65
classpath/java/util/IdentityHashMap.java
Normal file
@ -0,0 +1,65 @@
|
||||
package java.util;
|
||||
|
||||
public class IdentityHashMap<K, V> implements Map<K, V> {
|
||||
private final HashMap<K, V> map;
|
||||
|
||||
public IdentityHashMap(int capacity) {
|
||||
map = new HashMap(capacity, new MyHelper());
|
||||
}
|
||||
|
||||
public IdentityHashMap() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public boolean containsKey(K key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(V value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public V put(K key, V value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
public V remove(K key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return map.entrySet();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
public Collection<V> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
private static class MyHelper<K, V>
|
||||
extends HashMap.MyHelper<K, V>
|
||||
{
|
||||
public int hash(K a) {
|
||||
return (a == null ? 0 : System.identityHashCode(a));
|
||||
}
|
||||
|
||||
public boolean equal(K a, K b) {
|
||||
return a == b;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ public class WeakHashMap<K, V> implements Map<K, V> {
|
||||
private final ReferenceQueue queue;
|
||||
|
||||
public WeakHashMap(int capacity) {
|
||||
map = new HashMap(capacity, new MyCellFactory());
|
||||
map = new HashMap(capacity, new MyHelper());
|
||||
queue = new ReferenceQueue();
|
||||
}
|
||||
|
||||
@ -111,8 +111,8 @@ public class WeakHashMap<K, V> implements Map<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyCellFactory<K, V>
|
||||
implements HashMap.CellFactory<K, V>
|
||||
private static class MyHelper<K, V>
|
||||
extends HashMap.MyHelper<K, V>
|
||||
{
|
||||
public HashMap.Cell<K, V> make(K key, V value, HashMap.Cell<K, V> next) {
|
||||
return new MyCell(key, value, next);
|
||||
|
Reference in New Issue
Block a user