summaryrefslogtreecommitdiff
path: root/NET/worlds/core/Hashtable.java
diff options
context:
space:
mode:
Diffstat (limited to 'NET/worlds/core/Hashtable.java')
-rw-r--r--NET/worlds/core/Hashtable.java105
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);
+ }
+}