Skip to content

Commit a2a3740

Browse files
committed
Adding partial changes to support message format in EIDs
1 parent 08e334a commit a2a3740

File tree

10 files changed

+501
-130
lines changed

10 files changed

+501
-130
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<configuration>
156156
<compilerArgs>
157157
<arg>-Werror</arg>
158+
<arg>-Xlint:-deprecation</arg>
158159
<arg>-Xlint:all</arg>
159160
</compilerArgs>
160161
</configuration>

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package pl.wavesoftware.eid.exceptions;
1717

18+
import javax.annotation.Nonnull;
19+
import javax.annotation.Nullable;
1820
import java.io.Serializable;
19-
import static java.lang.Math.abs;
2021
import java.util.ArrayList;
2122
import java.util.List;
2223
import java.util.Random;
23-
import javax.annotation.Nullable;
24+
25+
import static java.lang.Math.abs;
2426

2527
/**
2628
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
@@ -93,7 +95,6 @@ public Eid(String id) {
9395
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>, or if given format was
9496
* null
9597
*/
96-
@SuppressWarnings("UnusedReturnValue")
9798
public static String setMessageFormat(String format) {
9899
validateFormat(format, MESSAGE_FORMAT_NUM_SPEC);
99100
String oldFormat = Eid.messageFormat;
@@ -157,6 +158,23 @@ public static String setRefFormat(String refFormat) {
157158
return previously;
158159
}
159160

161+
/**
162+
* Makes a log message from this EID object
163+
* <p>
164+
* <p>This method is for convenience of usage of EID in logging. You can use it like this:
165+
* <p>
166+
* <pre>
167+
* log.debug(new Eid("20151025:202129").makeLogMessage("A request: %s", request));
168+
* </pre>
169+
* @param logMessageFormat a log message format as accepted by {@link String#format(String, Object...)}
170+
* @param parameters a parameters for logMessageFormat to by passed to {@link String#format(String, Object...)}
171+
* @return a formatted message
172+
*/
173+
public String makeLogMessage(@Nonnull String logMessageFormat, @Nonnull Object... parameters) {
174+
String message = String.format(logMessageFormat, parameters);
175+
return String.format(getMessageFormat(), this.toString(), message);
176+
}
177+
160178
@Override
161179
public String toString() {
162180
if ("".equals(ref)) {
@@ -238,10 +256,9 @@ public String generateUniqId() {
238256
long first = abs(random.nextLong() + 1);
239257
int second = abs(random.nextInt(Integer.MAX_VALUE));
240258
int calc = (int) (first + second);
241-
return Integer.toString(calc, BASE36);
259+
return Integer.toString(abs(calc), BASE36);
242260
}
243261

244-
@SuppressWarnings("squid:S2245")
245262
private Random getUnsecuredFastRandom() {
246263
return new Random(System.currentTimeMillis());
247264
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public EidIllegalArgumentException(Eid id) {
7575
super(id);
7676
}
7777

78+
/**
79+
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
80+
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
81+
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
82+
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
83+
*/
84+
public EidIllegalArgumentException(Eid id, String messageFormat, Object... parameters) {
85+
super(id, messageFormat, parameters);
86+
}
87+
7888
/**
7989
* @inheritdoc
8090
* @return {@link IllegalArgumentException} class

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public EidIllegalStateException(Eid id) {
7575
super(id);
7676
}
7777

78+
/**
79+
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
80+
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
81+
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
82+
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
83+
*/
84+
public EidIllegalStateException(Eid id, String messageFormat, Object... parameters) {
85+
super(id, messageFormat, parameters);
86+
}
87+
7888
/**
7989
* @inheritdoc
8090
* @return {@link IllegalStateException} class

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public EidIndexOutOfBoundsException(Eid id) {
7575
super(id);
7676
}
7777

78+
/**
79+
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
80+
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
81+
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
82+
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
83+
*/
84+
public EidIndexOutOfBoundsException(Eid id, String messageFormat, Object... parameters) {
85+
super(id, messageFormat, parameters);
86+
}
87+
7888
/**
7989
* @inheritdoc
8090
* @return {@link IndexOutOfBoundsException} class

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public EidNullPointerException(Eid id) {
7575
super(id);
7676
}
7777

78+
/**
79+
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
80+
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
81+
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
82+
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
83+
*/
84+
public EidNullPointerException(Eid id, String messageFormat, Object... parameters) {
85+
super(id, messageFormat, parameters);
86+
}
87+
7888
/**
7989
* @inheritdoc
8090
* @return {@link NullPointerException} class

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package pl.wavesoftware.eid.exceptions;
1717

1818
import javax.annotation.Nullable;
19+
import java.util.Locale;
1920

2021
/**
2122
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
@@ -62,7 +63,6 @@ public EidRuntimeException(String eid, String ref) {
6263
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt>
6364
* value is permitted, and indicates that the cause is nonexistent or unknown.)
6465
*/
65-
@SuppressWarnings("WeakerAccess")
6666
public EidRuntimeException(String eid, Throwable cause) {
6767
this(new Eid(eid), cause);
6868
}
@@ -75,7 +75,6 @@ public EidRuntimeException(String eid, Throwable cause) {
7575
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> value is
7676
* permitted, and indicates that the cause is nonexistent or unknown.)
7777
*/
78-
@SuppressWarnings("WeakerAccess")
7978
public EidRuntimeException(String eid, String ref, Throwable cause) {
8079
this(new Eid(eid, ref), cause);
8180
}
@@ -85,7 +84,6 @@ public EidRuntimeException(String eid, String ref, Throwable cause) {
8584
*
8685
* @param id exception ID
8786
*/
88-
@SuppressWarnings("WeakerAccess")
8987
public EidRuntimeException(Eid id) {
9088
super(id.toString());
9189
eid = id;
@@ -94,14 +92,33 @@ public EidRuntimeException(Eid id) {
9492
/**
9593
* Constructs a new runtime exception with the specified Eid object and cause
9694
* <p>
97-
* The detail message is computed as <tt>String.format(messageFormat, id.toString(), message(cause)</tt>
95+
* The detail message is computed as <tt>String.format(Eid.getMessageFormat(), id.toString(), message(cause))</tt>
9896
*
9997
* @param id exception ID
10098
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> value is
10199
* permitted, and indicates that the cause is nonexistent or unknown.)
102100
*/
103101
public EidRuntimeException(Eid id, Throwable cause) {
104-
super(String.format(Eid.getMessageFormat(), id.toString(), message(cause)), cause);
102+
super(String.format(
103+
Locale.ENGLISH, Eid.getMessageFormat(), id.toString(), message(cause)
104+
), cause);
105+
eid = id;
106+
}
107+
108+
/**
109+
* Constructs a new runtime exception with specified Eid object and custom message
110+
* <p>
111+
* The detail message is computed as:
112+
* <pre>String.format(Eid.getMessageFormat(), id.toString(), String.format(messageFormat, parameters))</pre>
113+
* @param id exception ID
114+
* @param messageFormat message format in form of {@link String#format(String, Object...)}
115+
* @param parameters parameters in form of {@link String#format(String, Object...)}
116+
*/
117+
public EidRuntimeException(Eid id, String messageFormat, Object... parameters) {
118+
super(String.format(
119+
Locale.ENGLISH, Eid.getMessageFormat(), id.toString(),
120+
String.format(Locale.ENGLISH, messageFormat, parameters)
121+
));
105122
eid = id;
106123
}
107124

0 commit comments

Comments
 (0)