1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package fulmine.protocol.wire;
17
18 import static fulmine.util.Utils.CLOSE_BRACE;
19 import static fulmine.util.Utils.OPEN_BRACE;
20 import fulmine.model.component.IComponent;
21 import fulmine.util.reference.AutoCreatingStore;
22 import fulmine.util.reference.IAutoCreatingStore;
23 import fulmine.util.reference.IObjectBuilder;
24 import fulmine.util.reference.is;
25
26
27
28
29
30
31
32 public class WireIdentity implements IWireIdentity
33 {
34
35
36
37
38
39
40
41
42
43
44 private final boolean isInteger;
45
46
47 private final String wireIdentity;
48
49
50 private static final class IntegerWireIdBuilder implements
51 IObjectBuilder<Integer, WireIdentity>
52 {
53 public WireIdentity create(Integer key)
54 {
55 return new WireIdentity(key.intValue());
56 }
57 }
58
59
60 private static final class StringWireIdBuilder implements
61 IObjectBuilder<String, WireIdentity>
62 {
63 public WireIdentity create(String key)
64 {
65 return new WireIdentity(key);
66 }
67 }
68
69
70 private final static IAutoCreatingStore<String, WireIdentity> stringWireIdRegistry =
71 new AutoCreatingStore<String, WireIdentity>(new StringWireIdBuilder());
72
73
74 private final static IAutoCreatingStore<Integer, WireIdentity> intWireIdRegistry =
75 new AutoCreatingStore<Integer, WireIdentity>(new IntegerWireIdBuilder());
76
77
78
79
80
81
82
83
84
85
86 public static synchronized WireIdentity get(String stringWireId)
87 {
88 return stringWireIdRegistry.get(stringWireId);
89 }
90
91
92
93
94
95
96
97
98
99
100 public static synchronized WireIdentity get(int intWireId)
101 {
102 return intWireIdRegistry.get(Integer.valueOf(intWireId));
103 }
104
105
106
107
108
109
110
111 private WireIdentity(String stringWireId)
112 {
113 super();
114 this.isInteger = false;
115 this.wireIdentity = stringWireId;
116 }
117
118
119
120
121
122
123
124 private WireIdentity(int intWireId)
125 {
126 super();
127 this.isInteger = true;
128 this.wireIdentity = Integer.toString(intWireId);
129 }
130
131 public boolean isIntegerWireFormat()
132 {
133 return this.isInteger;
134 }
135
136 public int getAsInteger()
137 {
138 return Integer.parseInt(this.wireIdentity);
139 }
140
141 public String getAsString()
142 {
143 return this.wireIdentity;
144 }
145
146 @Override
147 public int hashCode()
148 {
149 final int prime = 31;
150 int result = 1;
151 result = prime * result + (isInteger ? 1231 : 1237);
152 result =
153 prime * result
154 + ((wireIdentity == null) ? 0 : wireIdentity.hashCode());
155 return result;
156 }
157
158 @Override
159 public boolean equals(Object obj)
160 {
161 if (is.same(this, obj))
162 {
163 return true;
164 }
165 if (is.differentClass(this, obj))
166 {
167 return false;
168 }
169 final WireIdentity other = (WireIdentity) obj;
170 return is.eq(this.isInteger, other.isInteger)
171 && is.eq(this.wireIdentity, other.wireIdentity);
172 }
173
174
175
176
177
178
179
180
181 @Override
182 public String toString()
183 {
184 return wireIdentity + OPEN_BRACE
185 + (isIntegerWireFormat() ? "int" : "string") + CLOSE_BRACE;
186 }
187
188 }