Skip to content

Commit bac4170

Browse files
committed
Lazy refactoring
1 parent 078b700 commit bac4170

17 files changed

Lines changed: 444 additions & 197 deletions

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import pl.wavesoftware.eid.configuration.Binding;
1919
import pl.wavesoftware.eid.configuration.Configurator;
2020
import pl.wavesoftware.eid.configuration.Validator;
21+
import pl.wavesoftware.eid.impl.SerializableSupplier;
22+
import pl.wavesoftware.eid.impl.Supplier;
2123

2224
import javax.annotation.Nullable;
2325
import java.io.Serializable;
@@ -112,7 +114,18 @@ public class Eid implements Serializable {
112114
private transient String id;
113115
@Nullable
114116
private transient String ref;
115-
private String uniqueId;
117+
private final SerializableSupplier<String> uniqueId = MODULE
118+
.getBinding()
119+
.lazy(new Supplier<String>() {
120+
@Override
121+
public String get() {
122+
return MODULE.getBinding()
123+
.getConfigurationSystem()
124+
.getConfiguration()
125+
.getIdGenerator()
126+
.generateUniqId();
127+
}
128+
});
116129

117130
/**
118131
* Constructor a single value of exception ID.
@@ -191,10 +204,7 @@ public boolean hasRef() {
191204
* @return a unique string
192205
*/
193206
public String getUnique() {
194-
if (uniqueId == null) {
195-
uniqueId = doGetUniqueId();
196-
}
197-
return uniqueId;
207+
return uniqueId.get();
198208
}
199209

200210
/**
@@ -227,17 +237,6 @@ public String toString() {
227237
.format(this);
228238
}
229239

230-
private synchronized String doGetUniqueId() {
231-
if (uniqueId == null) {
232-
return MODULE.getBinding()
233-
.getConfigurationSystem()
234-
.getConfiguration()
235-
.getIdGenerator()
236-
.generateUniqId();
237-
}
238-
return uniqueId;
239-
}
240-
241240
/*
242241
Suppress warnings id here for null check. Users can pass null event if it's
243242
forbidden.

src/main/java/pl/wavesoftware/eid/configuration/Binding.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
import pl.wavesoftware.eid.Eid;
2020
import pl.wavesoftware.eid.EidMessage;
21+
import pl.wavesoftware.eid.impl.SerializableSupplier;
22+
import pl.wavesoftware.eid.impl.Supplier;
23+
24+
import java.io.Serializable;
2125

2226
/**
2327
* A binding of Eid library. This interface can be used to completely
@@ -64,4 +68,13 @@ EidMessage createEidMessage(
6468
CharSequence messageTemplate,
6569
Object[] templateArguments
6670
);
71+
72+
/**
73+
* Creates a lazy supplier of a given supplier of serializable value
74+
*
75+
* @param supplier a supplier of serializable value
76+
* @param <T> a serializable type
77+
* @return a lazy, serializable, supplier
78+
*/
79+
<T extends Serializable> SerializableSupplier<T> lazy(Supplier<T> supplier);
6780
}

src/main/java/pl/wavesoftware/eid/impl/BindingImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
import pl.wavesoftware.eid.configuration.Binding;
2222
import pl.wavesoftware.eid.configuration.ConfigurationSystem;
2323

24+
import java.io.Serializable;
25+
2426
/**
27+
* A default binding implementation.
28+
*
2529
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
2630
* @since 2.0.0
2731
*/
@@ -47,4 +51,9 @@ public EidMessage createEidMessage(
4751
templateArguments
4852
);
4953
}
54+
55+
@Override
56+
public <T extends Serializable> SerializableSupplier<T> lazy(Supplier<T> supplier) {
57+
return SerializableLazy.of(supplier);
58+
}
5059
}

src/main/java/pl/wavesoftware/eid/impl/ConfigurationSystemImpl.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,54 +29,46 @@
2929
*/
3030
final class ConfigurationSystemImpl implements ConfigurationSystem {
3131

32-
private volatile MutableConfiguration configuration;
32+
private Lazy<MutableConfiguration> configuration =
33+
Lazy.of(new MutableConfigurationSupplier());
3334

3435
ConfigurationSystemImpl() {
3536
// nothing here
3637
}
3738

3839
@Override
3940
public Configuration getConfiguration() {
40-
if (configuration == null) {
41-
configuration = getMutableConfiguration();
42-
}
43-
return configuration;
41+
return configuration.get();
4442
}
4543

46-
/**
47-
* Configures an Eid library programmatically.
48-
*
49-
* @param configurator a configurator to use to configure Eid library
50-
* @return a reference to a configurator that can be used to restore
51-
* previous configuration
52-
*/
5344
@Override
5445
public Configurator configure(Configurator configurator) {
55-
// ensure system configuration are loaded
56-
getConfiguration();
57-
MutableConfiguration configuredSettings = configuration;
58-
configuration = new ConfigurationImpl(configuredSettings);
59-
configurator.configure(configuration);
60-
return new RestoreConfigurator(configuredSettings);
46+
MutableConfiguration configured = configuration.get();
47+
MutableConfiguration mutable = new ConfigurationImpl(configured);
48+
configurator.configure(mutable);
49+
configuration = Lazy.of(mutable);
50+
return new RestoreConfigurator(configured);
6151
}
6252

63-
private synchronized MutableConfiguration getMutableConfiguration() {
64-
if (configuration == null) {
53+
private static final class MutableConfigurationSupplier
54+
implements Supplier<MutableConfiguration> {
55+
56+
@Override
57+
public MutableConfiguration get() {
6558
return loadConfiguration();
6659
}
67-
return configuration;
68-
}
6960

70-
private static MutableConfiguration loadConfiguration() {
71-
MutableConfiguration mutableConfiguration = new ConfigurationImpl();
72-
new DefaultConfigurator().configure(mutableConfiguration);
73-
ServiceLoader<Configurator> configurators =
74-
ServiceLoader.load(Configurator.class);
61+
private static MutableConfiguration loadConfiguration() {
62+
MutableConfiguration mutableConfiguration = new ConfigurationImpl();
63+
new DefaultConfigurator().configure(mutableConfiguration);
64+
ServiceLoader<Configurator> configurators =
65+
ServiceLoader.load(Configurator.class);
7566

76-
for (Configurator configurator : configurators) {
77-
configurator.configure(mutableConfiguration);
67+
for (Configurator configurator : configurators) {
68+
configurator.configure(mutableConfiguration);
69+
}
70+
return mutableConfiguration;
7871
}
79-
return mutableConfiguration;
8072
}
8173

8274
private static final class RestoreConfigurator

src/main/java/pl/wavesoftware/eid/impl/EidTextRepresentation.java

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import pl.wavesoftware.eid.EidContainer;
2121
import pl.wavesoftware.eid.configuration.Configuration;
2222

23-
import java.io.IOException;
24-
import java.io.ObjectOutputStream;
2523
import java.io.Serializable;
2624

2725
/**
@@ -33,17 +31,22 @@ final class EidTextRepresentation implements EidContainer, Serializable {
3331

3432
private final Eid eid;
3533
private final TextMessage textMessage;
36-
private transient Configuration configuration;
37-
private String actual;
34+
private final SerializableLazy<String> actual;
3835

3936
EidTextRepresentation(
40-
Eid eid,
41-
TextMessage textMessage,
42-
Configuration configuration
37+
final Eid eid,
38+
final TextMessage textMessage,
39+
final Configuration configuration
4340
) {
4441
this.eid = eid;
4542
this.textMessage = textMessage;
46-
this.configuration = configuration;
43+
this.actual = SerializableLazy.of(new Supplier<String>() {
44+
@Override
45+
public String get() {
46+
return configuration.getFormatter()
47+
.format(eid, textMessage.get());
48+
}
49+
});
4750
}
4851

4952
@Override
@@ -56,30 +59,6 @@ TextMessage getTextMessage() {
5659
}
5760

5861
String get() {
59-
if (actual == null) {
60-
actual = doGet();
61-
configuration = null;
62-
}
63-
return actual;
64-
}
65-
66-
private synchronized String doGet() {
67-
if (actual == null) {
68-
return configuration.getFormatter()
69-
.format(eid, textMessage.get());
70-
}
71-
return actual;
72-
}
73-
74-
/**
75-
* Ensures that the value is evaluated before serialization.
76-
*
77-
* @param stream An object serialization stream.
78-
* @throws java.io.IOException If an error occurs writing to the stream.
79-
*/
80-
private void writeObject(ObjectOutputStream stream) throws IOException {
81-
// evaluates the values if it isn't evaluated yet!
82-
get();
83-
stream.defaultWriteObject();
62+
return actual.get();
8463
}
8564
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2018 Wave Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.wavesoftware.eid.impl;
18+
19+
import javax.annotation.Nullable;
20+
21+
/**
22+
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
23+
* @since 2.0.0
24+
*/
25+
final class InternalChecks {
26+
private InternalChecks() {
27+
// nothing here
28+
}
29+
30+
@SuppressWarnings("squid:S1695")
31+
static <T> T checkNotNull(@Nullable T value, String message) {
32+
if (value == null) {
33+
throw new IllegalArgumentException(message);
34+
}
35+
return value;
36+
}
37+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2018 Wave Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.wavesoftware.eid.impl;
18+
19+
import static pl.wavesoftware.eid.impl.InternalChecks.checkNotNull;
20+
21+
22+
/**
23+
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
24+
* @since 2.0.0
25+
*/
26+
class Lazy<T> implements Supplier<T> {
27+
private Supplier<T> supplier;
28+
private T value;
29+
30+
Lazy(Supplier<T> supplier) {
31+
this.supplier = checkNotNull(supplier, "20181124:004511");
32+
}
33+
34+
protected Lazy() {
35+
// nothing
36+
}
37+
38+
private Lazy(T value) {
39+
this.value = value;
40+
}
41+
42+
static <R> Lazy<R> of(Supplier<R> supplier) {
43+
return new Lazy<R>(supplier);
44+
}
45+
46+
static <R> Lazy<R> of(R value) {
47+
return new Lazy<R>(value);
48+
}
49+
50+
@Override
51+
public T get() {
52+
if (supplier != null) {
53+
value = doGet();
54+
}
55+
return value;
56+
}
57+
58+
private synchronized T doGet() {
59+
if (supplier != null) {
60+
T calculated = supplier.get();
61+
supplier = null;
62+
return calculated;
63+
}
64+
return value;
65+
}
66+
}

0 commit comments

Comments
 (0)