01: package org.space4j.indexing;
02:
03: import java.util.*;
04: import java.io.*;
05:
06: /**
07: * A key to be used by the Maps of the IndexManager.
08: */
09: public class Key implements Serializable {
10:
11: private ArrayList values = null;
12:
13: /**
14: * Creates a new Key.
15: */
16: public Key() {
17: values = new ArrayList();
18: }
19:
20: /**
21: * Creates a new Compound Key (int + String)
22: * @param i An integer
23: * @param s An String
24: */
25: public Key(int i, String s) {
26: this ();
27: add(new Integer(i));
28: add(s);
29: }
30:
31: /**
32: * Creates a new Key (int)
33: * @param i An integer
34: */
35: public Key(int i) {
36: this ();
37: add(new Integer(i));
38: }
39:
40: /**
41: * Creates a new Key (String)
42: * @param s An String
43: */
44: public Key(String s) {
45: this ();
46: add(s);
47: }
48:
49: /**
50: * Add a new value for this key.<br>
51: * Compound keys have more than one value.
52: * @param value The value to be added.
53: */
54: public void add(Object value) {
55: values.add(value);
56: }
57:
58: public int hashCode() {
59: Iterator iter = values.iterator();
60: int hash = 0;
61: while (iter.hasNext()) {
62: Object obj = iter.next();
63: if (obj == null) {
64: hash = hash * 31 + 1;
65: } else {
66: hash = hash * 31 + obj.hashCode();
67: }
68: }
69: return hash;
70: }
71:
72: public boolean equals(Object obj) {
73: if (!(obj instanceof Key))
74: return false;
75: Key key = (Key) obj;
76: if (key.values.size() != this .values.size())
77: return false;
78:
79: int x = key.values.size();
80: for (int i = 0; i < x; i++) {
81: Object key1 = key.values.get(i);
82: Object key2 = this .values.get(i);
83: if (key1 == null && key2 == null)
84: continue;
85: if (key1 == null || key2 == null)
86: return false;
87: if (!key1.equals(key2))
88: return false;
89: }
90: return true;
91: }
92: }
|