1515 */
1616package pl .wavesoftware .eid .exceptions ;
1717
18- import javax .annotation .Nonnull ;
1918import javax .annotation .Nullable ;
2019import java .io .Serializable ;
2120import java .util .ArrayList ;
3029 * <p>
3130 * Exception identifier for all Eid Runtime Exceptions.
3231 *
33- * @author Krzysztof Suszyński < krzysztof.suszynski@wavesoftware.pl>
32+ * @author <a href="mailto: krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a >
3433 */
3534public class Eid implements Serializable {
3635
@@ -50,6 +49,8 @@ public class Eid implements Serializable {
5049
5150 private static final int MESSAGE_FORMAT_NUM_SPEC = 2 ;
5251
52+ private static final String EMPTY_REF = "" ;
53+
5354 private static String messageFormat = DEFAULT_MESSAGE_FORMAT ;
5455
5556 private static UniqIdGenerator uniqIdGenerator = DEFAULT_UNIQ_ID_GENERATOR ;
@@ -62,7 +63,7 @@ public class Eid implements Serializable {
6263
6364 private final String ref ;
6465
65- private final Future < String > futureUniqueId ;
66+ private String uniqueId ;
6667
6768 /**
6869 * Constructor
@@ -71,9 +72,8 @@ public class Eid implements Serializable {
7172 * @param ref an optional reference
7273 */
7374 public Eid (String id , @ Nullable String ref ) {
74- futureUniqueId = new UniqFuture ();
7575 this .id = id ;
76- this .ref = ref == null ? "" : ref ;
76+ this .ref = ref == null ? EMPTY_REF : ref ;
7777 }
7878
7979 /**
@@ -82,7 +82,8 @@ public Eid(String id, @Nullable String ref) {
8282 * @param id the exception id, must be unique developer inserted string, from date
8383 */
8484 public Eid (String id ) {
85- this (id , null );
85+ this .id = id ;
86+ this .ref = EMPTY_REF ;
8687 }
8788
8889 /**
@@ -161,26 +162,25 @@ public static String setRefFormat(String refFormat) {
161162 /**
162163 * Makes a log message from this EID object
163164 * <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:
166166 * <pre>
167167 * log.debug(new Eid("20151025:202129").makeLogMessage("A request: %s", request));
168168 * </pre>
169169 * @param logMessageFormat a log message format as accepted by {@link String#format(String, Object...)}
170170 * @param parameters a parameters for logMessageFormat to by passed to {@link String#format(String, Object...)}
171171 * @return a formatted message
172172 */
173- public String makeLogMessage (@ Nonnull String logMessageFormat , @ Nonnull Object ... parameters ) {
173+ public String makeLogMessage (String logMessageFormat , Object ... parameters ) {
174174 String message = String .format (logMessageFormat , parameters );
175175 return String .format (getMessageFormat (), this .toString (), message );
176176 }
177177
178178 @ Override
179179 public String toString () {
180180 if ("" .equals (ref )) {
181- return String .format (format , id , futureUniqueId . get ());
181+ return String .format (format , id , getUniq ());
182182 }
183- return String .format (refFormat , id , ref , futureUniqueId . get ());
183+ return String .format (refFormat , id , ref , getUniq ());
184184 }
185185
186186 /**
@@ -207,7 +207,10 @@ public String getRef() {
207207 * @return a unique string
208208 */
209209 public String getUniq () {
210- return futureUniqueId .get ();
210+ if (uniqueId == null ) {
211+ uniqueId = uniqIdGenerator .generateUniqId ();
212+ }
213+ return uniqueId ;
211214 }
212215
213216 private static void validateFormat (String format , int numSpecifiers ) {
@@ -241,23 +244,6 @@ public interface UniqIdGenerator {
241244 String generateUniqId ();
242245 }
243246
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-
261247 private static final class StdUniqIdGenerator implements UniqIdGenerator {
262248
263249 private static final int BASE36 = 36 ;
0 commit comments