summaryrefslogtreecommitdiff
path: root/NET/worlds/core/Hashtable.java
blob: adbc5fdb975112278353b0be2cf55685a583ded4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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);
   }
}