1 package cz.cuni.amis.utils.token;
2
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.io.Serializable;
6 import java.util.Map;
7 import java.util.Set;
8
9 import cz.cuni.amis.utils.HashCode;
10 import cz.cuni.amis.utils.exception.PogamutException;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class Token implements IToken, Serializable {
27
28
29
30
31 private String name;
32
33
34
35
36
37
38
39 transient private long[] ids;
40
41
42
43
44 transient private int hashCode;
45 transient private String nameWithIds = null;
46
47
48
49
50
51
52 Token(String name, long[] ids) {
53 this.name = name;
54 this.ids = ids;
55 HashCode hc = new HashCode();
56 hc.add(ids.length);
57 for (long id : ids) {
58 hc.add(id);
59 }
60 this.hashCode = hc.getHash();
61 }
62
63
64
65
66
67
68 private void readObject(ObjectInputStream ois) {
69 try {
70 ois.defaultReadObject();
71 } catch (IOException e) {
72 throw new PogamutException("Could not deserialize Token", e);
73 } catch (ClassNotFoundException e) {
74 throw new PogamutException("Could not deserialize Token", e);
75 }
76 Token trueToken = Tokens.get(name);
77 this.ids = trueToken.ids;
78 this.hashCode = trueToken.hashCode;
79 }
80
81 @Override
82 public int hashCode() {
83 return hashCode;
84 }
85
86
87
88
89
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj) return true;
93 if (obj == null) return false;
94 if (hashCode != obj.hashCode()) return false;
95 if (!(obj instanceof IToken)) return false;
96 IToken token = (IToken)obj;
97 if (ids.length != token.getIds().length) return false;
98 for (int i = 0; i < ids.length; ++i) {
99 if (ids[i] != token.getIds()[i]) return false;
100 }
101 return true;
102 }
103
104
105
106
107
108 public String getToken() {
109 return name;
110 }
111
112
113
114
115 public long[] getIds() {
116 return ids;
117 }
118
119
120
121
122
123 public String getNameWithIds() {
124 if (nameWithIds != null) return nameWithIds ;
125 StringBuffer sb = new StringBuffer();
126 sb.append(name);
127 sb.append("[");
128 sb.append(ids[0]);
129 for (int i = 1; i < ids.length; ++i) {
130 sb.append(",");
131 sb.append(ids[i]);
132 }
133 sb.append("]");
134 nameWithIds = sb.toString();
135 return nameWithIds;
136 }
137
138
139
140
141 @Override
142 public String toString() {
143 return getNameWithIds();
144 }
145
146 }