15
15
*/
16
16
package pl .wavesoftware .eid .exceptions ;
17
17
18
- import javax .annotation .Nonnull ;
19
18
import javax .annotation .Nullable ;
20
19
import java .io .Serializable ;
21
20
import java .util .ArrayList ;
30
29
* <p>
31
30
* Exception identifier for all Eid Runtime Exceptions.
32
31
*
33
- * @author Krzysztof Suszyński < [email protected] >
32
+ * @author <a href="mailto: [email protected] ">Krzysztof Suszynski</a >
34
33
*/
35
34
public class Eid implements Serializable {
36
35
@@ -50,6 +49,8 @@ public class Eid implements Serializable {
50
49
51
50
private static final int MESSAGE_FORMAT_NUM_SPEC = 2 ;
52
51
52
+ private static final String EMPTY_REF = "" ;
53
+
53
54
private static String messageFormat = DEFAULT_MESSAGE_FORMAT ;
54
55
55
56
private static UniqIdGenerator uniqIdGenerator = DEFAULT_UNIQ_ID_GENERATOR ;
@@ -62,7 +63,7 @@ public class Eid implements Serializable {
62
63
63
64
private final String ref ;
64
65
65
- private final Future < String > futureUniqueId ;
66
+ private String uniqueId ;
66
67
67
68
/**
68
69
* Constructor
@@ -71,9 +72,8 @@ public class Eid implements Serializable {
71
72
* @param ref an optional reference
72
73
*/
73
74
public Eid (String id , @ Nullable String ref ) {
74
- futureUniqueId = new UniqFuture ();
75
75
this .id = id ;
76
- this .ref = ref == null ? "" : ref ;
76
+ this .ref = ref == null ? EMPTY_REF : ref ;
77
77
}
78
78
79
79
/**
@@ -82,7 +82,8 @@ public Eid(String id, @Nullable String ref) {
82
82
* @param id the exception id, must be unique developer inserted string, from date
83
83
*/
84
84
public Eid (String id ) {
85
- this (id , null );
85
+ this .id = id ;
86
+ this .ref = EMPTY_REF ;
86
87
}
87
88
88
89
/**
@@ -161,26 +162,25 @@ public static String setRefFormat(String refFormat) {
161
162
/**
162
163
* Makes a log message from this EID object
163
164
* <p>
164
- * <p>This method is for convenience of usage of EID in logging. You can use it like this:
165
- * <p>
165
+ * This method is for convenience of usage of EID in logging. You can use it like this:
166
166
* <pre>
167
167
* log.debug(new Eid("20151025:202129").makeLogMessage("A request: %s", request));
168
168
* </pre>
169
169
* @param logMessageFormat a log message format as accepted by {@link String#format(String, Object...)}
170
170
* @param parameters a parameters for logMessageFormat to by passed to {@link String#format(String, Object...)}
171
171
* @return a formatted message
172
172
*/
173
- public String makeLogMessage (@ Nonnull String logMessageFormat , @ Nonnull Object ... parameters ) {
173
+ public String makeLogMessage (String logMessageFormat , Object ... parameters ) {
174
174
String message = String .format (logMessageFormat , parameters );
175
175
return String .format (getMessageFormat (), this .toString (), message );
176
176
}
177
177
178
178
@ Override
179
179
public String toString () {
180
180
if ("" .equals (ref )) {
181
- return String .format (format , id , futureUniqueId . get ());
181
+ return String .format (format , id , getUniq ());
182
182
}
183
- return String .format (refFormat , id , ref , futureUniqueId . get ());
183
+ return String .format (refFormat , id , ref , getUniq ());
184
184
}
185
185
186
186
/**
@@ -207,7 +207,10 @@ public String getRef() {
207
207
* @return a unique string
208
208
*/
209
209
public String getUniq () {
210
- return futureUniqueId .get ();
210
+ if (uniqueId == null ) {
211
+ uniqueId = uniqIdGenerator .generateUniqId ();
212
+ }
213
+ return uniqueId ;
211
214
}
212
215
213
216
private static void validateFormat (String format , int numSpecifiers ) {
@@ -241,23 +244,6 @@ public interface UniqIdGenerator {
241
244
String generateUniqId ();
242
245
}
243
246
244
- private interface Future <T extends Serializable > extends Serializable {
245
- T get ();
246
- }
247
-
248
- private static final class UniqFuture implements Future <String > {
249
- private static final long serialVersionUID = 20160325113314L ;
250
- private String future ;
251
- private UniqFuture () {}
252
- @ Override
253
- public String get () {
254
- if (future == null ) {
255
- future = uniqIdGenerator .generateUniqId ();
256
- }
257
- return future ;
258
- }
259
- }
260
-
261
247
private static final class StdUniqIdGenerator implements UniqIdGenerator {
262
248
263
249
private static final int BASE36 = 36 ;
0 commit comments