Skip to content

Commit f62c44f

Browse files
committed
Performance releated upgrades
1 parent d259b11 commit f62c44f

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

  • src/main/java/pl/wavesoftware/eid/exceptions

src/main/java/pl/wavesoftware/eid/exceptions/Eid.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ public class Eid implements Serializable {
6262

6363
private final String ref;
6464

65-
private final String uniq;
65+
private final Future<String> futureUniqueId;
66+
67+
private final Future<String> repr;
6668

6769
/**
6870
* Constructor
@@ -71,9 +73,15 @@ public class Eid implements Serializable {
7173
* @param ref an optional reference
7274
*/
7375
public Eid(String id, @Nullable String ref) {
74-
uniq = uniqIdGenerator.generateUniqId();
76+
futureUniqueId = new StdFuture<String>() {
77+
@Override
78+
protected String produce() {
79+
return uniqIdGenerator.generateUniqId();
80+
}
81+
};
7582
this.id = id;
7683
this.ref = ref == null ? "" : ref;
84+
repr = new ToStringRepr();
7785
}
7886

7987
/**
@@ -177,10 +185,7 @@ public String makeLogMessage(@Nonnull String logMessageFormat, @Nonnull Object..
177185

178186
@Override
179187
public String toString() {
180-
if ("".equals(ref)) {
181-
return String.format(format, id, uniq);
182-
}
183-
return String.format(refFormat, id, ref, uniq);
188+
return repr.get();
184189
}
185190

186191
/**
@@ -207,7 +212,7 @@ public String getRef() {
207212
* @return a unique string
208213
*/
209214
public String getUniq() {
210-
return uniq;
215+
return futureUniqueId.get();
211216
}
212217

213218
private static void validateFormat(String format, int numSpecifiers) {
@@ -241,6 +246,33 @@ public interface UniqIdGenerator {
241246
String generateUniqId();
242247
}
243248

249+
private interface Future<T> {
250+
T get();
251+
}
252+
253+
private static abstract class StdFuture<T> implements Future<T> {
254+
private T future;
255+
protected abstract T produce();
256+
@Override
257+
public T get() {
258+
if (future == null) {
259+
future = produce();
260+
}
261+
return future;
262+
}
263+
}
264+
265+
private class ToStringRepr extends StdFuture<String> {
266+
267+
@Override
268+
protected String produce() {
269+
if ("".equals(ref)) {
270+
return String.format(format, id, futureUniqueId.get());
271+
}
272+
return String.format(refFormat, id, ref, futureUniqueId.get());
273+
}
274+
}
275+
244276
private static final class StdUniqIdGenerator implements UniqIdGenerator {
245277

246278
private static final int BASE36 = 36;

0 commit comments

Comments
 (0)