diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/core/Hashtable.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/core/Hashtable.java')
| -rw-r--r-- | NET/worlds/core/Hashtable.java | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/NET/worlds/core/Hashtable.java b/NET/worlds/core/Hashtable.java new file mode 100644 index 0000000..adbc5fd --- /dev/null +++ b/NET/worlds/core/Hashtable.java @@ -0,0 +1,105 @@ +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<K, V> extends java.util.Hashtable<K, V> 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<K> 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<K> 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); + } +} |