1 package cz.cuni.amis.pogamut.base.communication.worldview.object;
2
3 import java.io.Serializable;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import cz.cuni.amis.utils.exception.PogamutException;
8 import cz.cuni.amis.utils.token.Token;
9 import cz.cuni.amis.utils.token.Tokens;
10
11
12
13
14
15
16
17
18
19
20 public class WorldObjectId implements Serializable {
21
22
23
24
25 protected Token token;
26
27
28
29
30 protected boolean isLong;
31
32
33
34
35 protected boolean isDouble;
36
37
38
39
40
41 protected WorldObjectId(String name) {
42 this.token = Tokens.get(name);
43 isLong = false;
44 isDouble = false;
45 }
46
47
48
49
50
51 protected WorldObjectId(long id) {
52 this.token = Tokens.get(id);
53 isLong = true;
54 isDouble = false;
55 }
56
57
58
59
60
61 protected WorldObjectId(double id) {
62 this.token = Tokens.get(id);
63 isLong = false;
64 isDouble = true;
65 }
66
67 protected WorldObjectId(Token token) {
68 this.token = token;
69 }
70
71 @Override
72 public int hashCode() {
73 return token.hashCode();
74 }
75
76 @Override
77 public boolean equals(Object o) {
78 if (o == this) return true;
79 if (o == null) return false;
80 if (!(o instanceof WorldObjectId)) return false;
81 return token.equals(((WorldObjectId)o).token);
82 }
83
84
85
86
87
88 public String getStringId() {
89 return token.getToken();
90 }
91
92
93
94
95
96 public boolean isNumericId() {
97 return isLong || isDouble;
98 }
99
100
101
102
103
104 public boolean isLongId() {
105 return isLong;
106 }
107
108
109
110
111
112 public boolean isDoubleId() {
113 return isDouble;
114 }
115
116
117
118
119
120
121 public long getLongId() {
122 if (!isLong) {
123 try {
124 long result = Long.parseLong(token.getToken());
125 isLong = true;
126 return result;
127 } catch (Exception e) {
128 throw new PogamutException("Id does not hold long value! Id: " + this, this);
129 }
130 }
131 return Long.parseLong(token.getToken());
132 }
133
134
135
136
137
138
139 public double getDoubleId() {
140 if (!isDouble) {
141 try {
142 double result = Double.parseDouble(token.getToken());
143 isDouble = true;
144 return result;
145 } catch (Exception e) {
146 throw new PogamutException("Id does not hold double value! Id: " + this, this);
147 }
148 }
149 return Double.parseDouble(token.getToken());
150 }
151
152
153
154
155
156
157 @Override
158 public String toString() {
159 return "WorldObjectId["+getStringId()+"]";
160 }
161
162
163
164
165 private static Map<String, WorldObjectId> map = new HashMap<String, WorldObjectId>();
166
167
168
169
170
171
172
173
174
175
176 public static WorldObjectId get(String name) {
177 if (name == null) throw new PogamutException("Could not return a WorldObjectId for 'null'!", WorldObjectId.class);
178 WorldObjectId id = null;
179 id = map.get(name);
180 if (id != null) return id;
181 synchronized(map) {
182 id = map.get(name);
183 if (id != null) return id;
184 id = new WorldObjectId(name);
185 map.put(name, id);
186 }
187 return id;
188 }
189
190
191
192
193
194
195
196
197
198
199 public static WorldObjectId get(long objId) {
200 return get(String.valueOf(objId));
201 }
202
203
204
205
206
207
208
209
210
211
212 public static WorldObjectId get(double objId) {
213 return get(String.valueOf(objId));
214 }
215
216 }