Skip to content

Commit 078b700

Browse files
committed
Adding test to cover changes made in refactoring
1 parent 829e259 commit 078b700

19 files changed

Lines changed: 533 additions & 54 deletions

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ private static Binding chooseImplementation(Iterable<Binding> bindings) {
4545
best = bond;
4646
}
4747
}
48-
if (best == null) {
49-
throw new IllegalStateException("20181106:000523");
50-
}
48+
assert best != null : "20181106:000523";
5149
return best;
5250
}
5351

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

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

1919
import javax.annotation.Nullable;
2020
import java.util.Locale;
21+
import java.util.TimeZone;
2122

2223
/**
2324
* Represents a configuration of Eid library. To reconfigure use
@@ -58,4 +59,13 @@ public interface Configuration {
5859
*/
5960
@Nullable
6061
Locale getLocale();
62+
63+
/**
64+
* Gets a time zone to be used when formatting texts. Returns null if time
65+
* zone isn't set.
66+
*
67+
* @return a time zone to be used when formatting texts, or null
68+
*/
69+
@Nullable
70+
TimeZone getTimeZone();
6171
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package pl.wavesoftware.eid.configuration;
1818

1919
import java.util.Locale;
20+
import java.util.TimeZone;
2021

2122
/**
2223
* Use {@link Configurator} to configure Eid settings.
@@ -54,6 +55,17 @@ public interface ConfigurationBuilder {
5455
*/
5556
ConfigurationBuilder locale(Locale locale);
5657

58+
/**
59+
* Sets a time zone to be used when formatting texts. If not set, default
60+
* system time zone will be used (platform specific). See
61+
* {@link TimeZone#getDefault()}.
62+
*
63+
* @param zone a time zone to be used
64+
* @return a self reference for ease of use
65+
* @see TimeZone#getDefault()
66+
*/
67+
ConfigurationBuilder timezone(TimeZone zone);
68+
5769
/**
5870
* Configures a validator that will be called on each Eid number. By
5971
* default, there is no validator configured for maximum speed. Using this
@@ -63,4 +75,15 @@ public interface ConfigurationBuilder {
6375
* @return a self reference for ease of use
6476
*/
6577
ConfigurationBuilder validator(Validator validator);
78+
79+
/**
80+
* Gets an object that is a future configuration, to be used to cross
81+
* configure elements of the configuration.
82+
* <p>
83+
* It might throw exceptions if configuration isn't completed so use it
84+
* only if configuration is done.
85+
*
86+
* @return a future configuration
87+
*/
88+
Configuration getFutureConfiguration();
6689
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public EidMessage createEidMessage(
4242
) {
4343
return new DefaultEidMessage(
4444
eid,
45-
getConfigurationSystem().getConfiguration().getFormatter(),
45+
getConfigurationSystem().getConfiguration(),
4646
messageTemplate,
4747
templateArguments
4848
);

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616

1717
package pl.wavesoftware.eid.impl;
1818

19+
import pl.wavesoftware.eid.configuration.Configuration;
1920
import pl.wavesoftware.eid.configuration.ConfigurationBuilder;
2021
import pl.wavesoftware.eid.configuration.Formatter;
2122
import pl.wavesoftware.eid.configuration.UniqueIdGenerator;
2223
import pl.wavesoftware.eid.configuration.Validator;
2324

2425
import javax.annotation.Nullable;
2526
import java.util.Locale;
27+
import java.util.TimeZone;
2628

2729
/**
2830
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
29-
* @since 2018-10-29
31+
* @since 2.0.0
3032
*/
3133
final class ConfigurationImpl implements MutableConfiguration {
3234

@@ -36,6 +38,8 @@ final class ConfigurationImpl implements MutableConfiguration {
3638
private Validator validator;
3739
@Nullable
3840
private Locale locale;
41+
@Nullable
42+
private TimeZone zone;
3943

4044
ConfigurationImpl() {
4145
// nothing here
@@ -45,7 +49,8 @@ final class ConfigurationImpl implements MutableConfiguration {
4549
this.formatter = settings.getFormatter();
4650
this.generator = settings.getIdGenerator();
4751
this.validator = settings.getValidator();
48-
this.locale = settings.getLocale();
52+
this.locale = settings.getLocale();
53+
this.zone = settings.getTimeZone();
4954
}
5055

5156
@Override
@@ -66,12 +71,23 @@ public ConfigurationBuilder locale(Locale locale) {
6671
return this;
6772
}
6873

74+
@Override
75+
public ConfigurationBuilder timezone(TimeZone zone) {
76+
this.zone = zone;
77+
return this;
78+
}
79+
6980
@Override
7081
public ConfigurationBuilder validator(Validator validator) {
7182
this.validator = validator;
7283
return this;
7384
}
7485

86+
@Override
87+
public Configuration getFutureConfiguration() {
88+
return this;
89+
}
90+
7591
@Override
7692
public Formatter getFormatter() {
7793
return formatter;
@@ -93,4 +109,10 @@ public Validator getValidator() {
93109
public Locale getLocale() {
94110
return locale;
95111
}
112+
113+
@Nullable
114+
@Override
115+
public TimeZone getTimeZone() {
116+
return zone;
117+
}
96118
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class DefaultConfigurator implements Configurator {
2727
@Override
2828
public void configure(ConfigurationBuilder configuration) {
2929
configuration
30-
.formatter(new DefaultFormatter())
30+
.formatter(new DefaultFormatter(configuration.getFutureConfiguration()))
3131
.uniqueIdGenerator(new DefaultUniqueIdGenerator());
3232
}
3333
}

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

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

1919
import pl.wavesoftware.eid.Eid;
2020
import pl.wavesoftware.eid.EidMessage;
21-
import pl.wavesoftware.eid.configuration.Formatter;
21+
import pl.wavesoftware.eid.configuration.Configuration;
2222

2323
import java.io.Serializable;
2424

@@ -29,18 +29,18 @@
2929
final class DefaultEidMessage implements EidMessage, Serializable {
3030
private static final long serialVersionUID = 20181029192322L;
3131

32-
private final EidTextRepresntation represntation;
32+
private final EidTextRepresentation represntation;
3333

3434
DefaultEidMessage(
3535
Eid eid,
36-
Formatter formatter,
36+
Configuration configuration,
3737
CharSequence messageFormat,
3838
Object[] arguments
3939
) {
40-
this.represntation = new EidTextRepresntation(
40+
this.represntation = new EidTextRepresentation(
4141
eid,
42-
new TextMessage(messageFormat, arguments),
43-
formatter
42+
new TextMessage(configuration, messageFormat, arguments),
43+
configuration
4444
);
4545
}
4646

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

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,68 @@
1717
package pl.wavesoftware.eid.impl;
1818

1919
import pl.wavesoftware.eid.Eid;
20+
import pl.wavesoftware.eid.configuration.Configuration;
2021
import pl.wavesoftware.eid.configuration.Formatter;
2122

23+
import javax.annotation.Nullable;
24+
import java.util.Locale;
25+
2226
/**
2327
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
2428
* @since 2.0.0
2529
*/
2630
final class DefaultFormatter implements Formatter {
2731

28-
private static final String FORMAT = "[%s]<%s>";
32+
private static final String FORMAT = "[%s]<%s>";
2933
private static final String REF_FORMAT = "[%s|%s]<%s>";
3034
private static final String MSG_FORMAT = "%s => %s";
3135

36+
private final Configuration configuration;
37+
38+
DefaultFormatter(Configuration configuration) {
39+
this.configuration = configuration;
40+
}
41+
3242
@Override
3343
public String format(Eid eid) {
44+
String fmt;
45+
Object[] params;
3446
if (eid.getRef() == null) {
35-
return String.format(
36-
FORMAT,
47+
fmt = FORMAT;
48+
params = new Object[]{
3749
eid.getId(),
3850
eid.getUnique()
39-
);
51+
};
52+
} else {
53+
fmt = REF_FORMAT;
54+
params = new Object[]{
55+
eid.getId(),
56+
eid.getRef(),
57+
eid.getUnique()
58+
};
4059
}
41-
return String.format(
42-
REF_FORMAT,
43-
eid.getId(),
44-
eid.getRef(),
45-
eid.getUnique()
46-
);
60+
java.util.Formatter formatter = getStringFormatter()
61+
.format(fmt, params);
62+
return formatter.toString();
4763
}
4864

4965
@Override
5066
public String format(Eid eid, String message) {
51-
return String.format(
52-
MSG_FORMAT,
53-
format(eid),
54-
message
55-
);
67+
return getStringFormatter()
68+
.format(
69+
MSG_FORMAT,
70+
format(eid),
71+
message
72+
).toString();
73+
}
74+
75+
private java.util.Formatter getStringFormatter() {
76+
@Nullable
77+
Locale locale = configuration.getLocale();
78+
if (locale != null) {
79+
return new java.util.Formatter(locale);
80+
} else {
81+
return new java.util.Formatter();
82+
}
5683
}
5784
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020

2121
import java.util.Random;
2222

23-
import static java.lang.Math.abs;
24-
2523
/**
2624
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
2725
*/
2826
final class DefaultUniqueIdGenerator implements UniqueIdGenerator {
2927

3028
private static final int BASE36 = 36;
29+
private static final int MIN = 60466176;
3130

3231
private final Random random;
3332

@@ -37,10 +36,8 @@ final class DefaultUniqueIdGenerator implements UniqueIdGenerator {
3736

3837
@Override
3938
public String generateUniqId() {
40-
long first = abs(random.nextLong() + 1);
41-
int second = abs(random.nextInt(Integer.MAX_VALUE));
42-
int calc = (int) (first + second);
43-
return Integer.toString(abs(calc), BASE36);
39+
int calc = random.nextInt(Integer.MAX_VALUE - MIN) + MIN;
40+
return Integer.toString(calc, BASE36);
4441
}
4542

4643
/*

src/main/java/pl/wavesoftware/eid/impl/EidTextRepresntation.java renamed to src/main/java/pl/wavesoftware/eid/impl/EidTextRepresentation.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import pl.wavesoftware.eid.Eid;
2020
import pl.wavesoftware.eid.EidContainer;
21-
import pl.wavesoftware.eid.configuration.Formatter;
21+
import pl.wavesoftware.eid.configuration.Configuration;
2222

2323
import java.io.IOException;
2424
import java.io.ObjectOutputStream;
@@ -28,22 +28,22 @@
2828
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
2929
* @since 2.0.0
3030
*/
31-
final class EidTextRepresntation implements EidContainer, Serializable {
31+
final class EidTextRepresentation implements EidContainer, Serializable {
3232
private static final long serialVersionUID = 20181029231519L;
3333

3434
private final Eid eid;
3535
private final TextMessage textMessage;
36-
private transient Formatter formatter;
36+
private transient Configuration configuration;
3737
private String actual;
3838

39-
EidTextRepresntation(
39+
EidTextRepresentation(
4040
Eid eid,
4141
TextMessage textMessage,
42-
Formatter formatter
42+
Configuration configuration
4343
) {
4444
this.eid = eid;
4545
this.textMessage = textMessage;
46-
this.formatter = formatter;
46+
this.configuration = configuration;
4747
}
4848

4949
@Override
@@ -58,14 +58,15 @@ TextMessage getTextMessage() {
5858
String get() {
5959
if (actual == null) {
6060
actual = doGet();
61-
formatter = null;
61+
configuration = null;
6262
}
6363
return actual;
6464
}
6565

6666
private synchronized String doGet() {
6767
if (actual == null) {
68-
return formatter.format(eid, textMessage.get());
68+
return configuration.getFormatter()
69+
.format(eid, textMessage.get());
6970
}
7071
return actual;
7172
}

0 commit comments

Comments
 (0)