View Javadoc

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   * Interface that is returning an unique id of the world object. The id must be unique
13   * among all the objects in the world.
14   * <p><p>
15   * Every implementations MUST implement equals() and hashCode() correctly as we will use this inside
16   * hashmaps/sets.
17   * 
18   * @author Jimmy
19   */
20  public class WorldObjectId implements Serializable {
21  	
22  	/**
23  	 * Unique representation of this id, contains ints that uniquely identifies it.
24  	 */
25  	protected Token token;
26  	
27  	/**
28  	 * Whether the id wolds 'long' inside {@link WorldObjectId#token}.
29  	 */
30  	protected boolean isLong;
31  	
32  	/**
33  	 * Whether the id wolds 'double' inside {@link WorldObjectId#token}.
34  	 */
35  	protected boolean isDouble;
36  	
37  	/**
38  	 * Instantiates a new object with id of 'name'.
39  	 * @param name
40  	 */
41  	protected WorldObjectId(String name) {
42  		this.token = Tokens.get(name);
43  		isLong = false;
44  		isDouble = false;
45  	}
46  	
47  	/**
48  	 * Instantiates a new object with id of 'id'.
49  	 * @param id
50  	 */
51  	protected WorldObjectId(long id) {
52  		this.token = Tokens.get(id);
53  		isLong = true;
54  		isDouble = false;
55  	}
56  	
57  	/**
58  	 * Instantiates a new object with id of 'id'.
59  	 * @param id
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; // early-success
79  		if (o == null) return false;
80  		if (!(o instanceof WorldObjectId)) return false;
81  		return token.equals(((WorldObjectId)o).token);
82  	}
83  
84  	/**
85  	 * Always returns a string representation of the ID.
86  	 * @return
87  	 */
88  	public String getStringId() {
89  		return token.getToken();
90  	}
91  	
92  	/**
93  	 * Whether the id holds a numeric value (i.e., it is either {@link WorldObjectId#isLongId()} or {@link WorldObjectId#isDoubleId()}).
94  	 * @return
95  	 */
96  	public boolean isNumericId() {
97  		return isLong || isDouble;
98  	}
99  	
100 	/**
101 	 * Whether the id is a long one.
102 	 * @return
103 	 */
104 	public boolean isLongId() {
105 		return isLong;
106 	}
107 	
108 	/**
109 	 * Whether the is is a double one.
110 	 * @return
111 	 */
112 	public boolean isDoubleId() {
113 		return isDouble;
114 	}	
115 	
116 	/**
117 	 * Returns id as 'long', only iff {@link WorldObjectId#isLongId()}, otherwise it raises a {@link PogamutException}.
118 	 * 
119 	 * @return long
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 	 * Returns id as 'double', only iff {@link WorldObjectId#isDoubleId()}, otherwise it raises a {@link PogamutException}.
136 	 * 
137 	 * @return long
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 	 * Returns string representation of the id, format: WorldObjectId[id] 
154 	 * 
155 	 * @return string representation of this object
156 	 */
157 	@Override
158 	public String toString() {
159 		return "WorldObjectId["+getStringId()+"]";
160 	}
161 
162 	/**
163 	 * Map that serves for translation of WorldObjectIds...
164 	 */
165 	private static Map<String, WorldObjectId> map  = new HashMap<String, WorldObjectId>();
166 	
167 	/**
168 	 * Returns shared instance of the {@link WorldObjectId} for 'name'. If no {@link WorldObjectId} exists for 'name',
169 	 * new one is created.
170 	 * <p><p>
171 	 * THREAD-SAFE!
172 	 * 
173 	 * @param name
174 	 * @return
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 	 * Returns shared instance of the {@link WorldObjectId} for 'objId'. If no {@link WorldObjectId} exists for 'objId',
192 	 * new one is created.
193 	 * <p><p>
194 	 * THREAD-SAFE!
195 	 * 
196 	 * @param name
197 	 * @return
198 	 */
199 	public static WorldObjectId get(long objId) {
200 		return get(String.valueOf(objId));
201 	}
202 	
203 	/**
204 	 * Returns shared instance of the {@link WorldObjectId} for 'objId'. If no {@link WorldObjectId} exists for 'objId',
205 	 * new one is created.
206 	 * <p><p>
207 	 * THREAD-SAFE!
208 	 * 
209 	 * @param name
210 	 * @return
211 	 */
212 	public static WorldObjectId get(double objId) {
213 		return get(String.valueOf(objId));
214 	}
215 	
216 }