package NET.worlds.core; import NET.worlds.scape.Persister; import NET.worlds.scape.Restorer; import NET.worlds.scape.Saver; import NET.worlds.scape.TooNewException; import java.io.IOException; import java.util.Enumeration; public class Hashtable extends java.util.Hashtable implements Persister { private static final long serialVersionUID = 1133311923215053895L; private static Object classCookie = new Object(); public Hashtable(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); } public Hashtable(int initialCapacity) { super(initialCapacity); } public Hashtable() { } public Object getKey(Object obj) { Enumeration e = this.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); if (this.get(key) == obj) { return key; } } return null; } @Override public void saveState(Saver s) throws IOException { s.saveVersion(0, classCookie); int count = 0; Enumeration e = this.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); Object obj = this.get(key); if ((key instanceof String || key instanceof Persister) && obj instanceof Persister) { count++; } } s.saveInt(count); e = this.keys(); while (e.hasMoreElements()) { Object key = e.nextElement(); Object obj = this.get(key); if ((key instanceof String || key instanceof Persister) && obj instanceof Persister) { if (key instanceof String) { s.saveBoolean(true); s.saveString((String)key); } else { s.saveBoolean(false); s.save((Persister)key); } s.save((Persister)obj); } } } @Override public void restoreState(Restorer r) throws IOException, TooNewException { int count = this.restoreCount(r); for (int i = 0; i < count; i++) { this.restoreEntry(r); } } @Override public void postRestore(int version) { } public int restoreCount(Restorer r) throws IOException, TooNewException { switch (r.restoreVersion(classCookie)) { case 0: return r.restoreInt(); default: throw new TooNewException(); } } public void restoreEntry(Restorer r) throws IOException, TooNewException { K key; if (r.restoreBoolean()) { key = (K)r.restoreString(); } else { key = (K)r.restore(); } V obj = (V)r.restore(); this.put(key, obj); } }