Skip to content

Commit cbceb4a

Browse files
committed
Quality fixes from SonarQube
1 parent efe5b8d commit cbceb4a

File tree

7 files changed

+198
-123
lines changed

7 files changed

+198
-123
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# EID Runtime Exceptions and Utilities
22

3-
[![Build Status](https://travis-ci.org/wavesoftware/java-eid-exceptions.svg?branch=master)](https://travis-ci.org/wavesoftware/java-eid-exceptions) [![Coverage Status](https://coveralls.io/repos/wavesoftware/java-eid-exceptions/badge.svg?branch=master&service=github)](https://coveralls.io/github/wavesoftware/java-eid-exceptions?branch=master) [![Maven Central](https://img.shields.io/maven-central/v/pl.wavesoftware/eid-exceptions.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22pl.wavesoftware%22%20AND%20a%3A%22eid-exceptions%22)
3+
[![Build Status](https://travis-ci.org/wavesoftware/java-eid-exceptions.svg?branch=master)](https://travis-ci.org/wavesoftware/java-eid-exceptions) [![Coverage Status](https://coveralls.io/repos/wavesoftware/java-eid-exceptions/badge.svg?branch=master&service=github)](https://coveralls.io/github/wavesoftware/java-eid-exceptions?branch=master) [![SonarQube Tech Debt](https://img.shields.io/sonar/http/sonar-ro.wavesoftware.pl/pl.wavesoftware:eid-exceptions/tech_debt.svg)](http://sonar-ro.wavesoftware.pl/dashboard/index/2600) [![Maven Central](https://img.shields.io/maven-central/v/pl.wavesoftware/eid-exceptions.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22pl.wavesoftware%22%20AND%20a%3A%22eid-exceptions%22)
44

55
This small library holds a set of Exceptions that implements idea of fast, reusable, error codes that can be simple thrown fast in case of unpredictable and unrecoverable application failure.
66

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

Lines changed: 115 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,19 @@ public class Eid implements Serializable {
3636

3737
public static final String DEFAULT_REF_FORMAT = "[%s|%s]<%s>";
3838

39+
public static final String DEFAULT_MESSAGE_FORMAT = "%s => %s";
40+
41+
public static final UniqIdGenerator DEFAULT_UNIQ_ID_GENERATOR = new StdUniqIdGenerator();
42+
3943
private static final long serialVersionUID = -9876432123423401L;
4044

41-
private static final Random RANDOM = new Random(System.currentTimeMillis());
45+
private static final int FORMAT_NUM_SPEC = 2;
46+
47+
private static final int REF_FORMAT_NUM_SPEC = 3;
48+
49+
static String messageFormat = DEFAULT_MESSAGE_FORMAT;
50+
51+
private static UniqIdGenerator uniqIdGenerator = DEFAULT_UNIQ_ID_GENERATOR;
4252

4353
private static String format = DEFAULT_FORMAT;
4454

@@ -51,15 +61,70 @@ public class Eid implements Serializable {
5161
private final String uniq;
5262

5363
/**
54-
* Sets the actual format that will be used in {@link toString()} method. It will return previously used format.
64+
* Constructor
5565
*
56-
* @param format a format compliant with {@link String#format(String, Object...)} with 2 object arguments
57-
* @return a previously used format
66+
* @param id the exception id, must be unique developer insereted string, from date
67+
* @param ref an optional reference
68+
*/
69+
public Eid(String id, @Nullable String ref) {
70+
uniq = uniqIdGenerator.generateUniqId();
71+
this.id = id;
72+
this.ref = ref == null ? "" : ref;
73+
}
74+
75+
/**
76+
* Constructor
77+
*
78+
* @param id the exception id, must be unique developer insereted string, from date
79+
*/
80+
public Eid(String id) {
81+
this(id, null);
82+
}
83+
84+
/**
85+
* Sets a format that will be used for all Eid exceptions when printing a detail message.
86+
* <p>
87+
* Format must be non-null and contain two format specifiers <tt>"%s"</tt>
88+
*
89+
* @param format a format that will be used, must be non-null and contain two format specifiers <tt>"%s"</tt>
90+
* @return previously used format
5891
* @throws NullPointerException if given format was null
5992
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>
6093
*/
61-
public static String setFormat(String format) throws NullPointerException, IllegalArgumentException {
94+
public static String setMessageFormat(String format) {
6295
validateFormat(format, 2);
96+
String oldFormat = Eid.messageFormat;
97+
Eid.messageFormat = format;
98+
return oldFormat;
99+
}
100+
101+
/**
102+
* Sets the actual unique ID generator that will be used to generate IDs for all Eid objects. It will return previously used
103+
* generator.
104+
*
105+
* @param uniqIdGenerator new instance of unique ID generator
106+
* @return a previously used unique ID generator
107+
* @throws IllegalArgumentException if given generator was null
108+
*/
109+
public static UniqIdGenerator setUniqIdGenerator(UniqIdGenerator uniqIdGenerator) {
110+
if (uniqIdGenerator == null) {
111+
throw new IllegalArgumentException(new NullPointerException("Unique ID generator can't be null, but given one"));
112+
}
113+
UniqIdGenerator previous = Eid.uniqIdGenerator;
114+
Eid.uniqIdGenerator = uniqIdGenerator;
115+
return previous;
116+
}
117+
118+
/**
119+
* Sets the actual format that will be used in {@link toString()} method. It will return previously used format.
120+
*
121+
* @param format a format compliant with {@link String#format(String, Object...)} with 2 object arguments
122+
* @return a previously used format
123+
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>, or if given format was
124+
* null
125+
*/
126+
public static String setFormat(String format) {
127+
validateFormat(format, FORMAT_NUM_SPEC);
63128
String prevoiusly = Eid.format;
64129
Eid.format = format;
65130
return prevoiusly;
@@ -71,36 +136,16 @@ public static String setFormat(String format) throws NullPointerException, Illeg
71136
* @param refFormat a format compliant with {@link String#format(String, Object...)} with 3 object arguments
72137
* @return a previously used format
73138
* @throws NullPointerException if given format was null
74-
* @throws IllegalArgumentException if given format hasn't got tree format specifiers <tt>"%s"</tt>
139+
* @throws IllegalArgumentException if given format hasn't got tree format specifiers <tt>"%s"</tt>, or if given format was
140+
* null
75141
*/
76142
public static String setRefFormat(String refFormat) {
77-
validateFormat(refFormat, 3);
143+
validateFormat(refFormat, REF_FORMAT_NUM_SPEC);
78144
String prevoiusly = Eid.refFormat;
79145
Eid.refFormat = refFormat;
80146
return prevoiusly;
81147
}
82148

83-
/**
84-
* Constructor
85-
*
86-
* @param id the exception id, must be unique developer insereted string, from date
87-
* @param ref an optional reference
88-
*/
89-
public Eid(String id, @Nullable String ref) {
90-
uniq = Integer.toString(abs(Long.valueOf(abs(RANDOM.nextLong()) + abs(RANDOM.nextInt())).intValue()), 36);
91-
this.id = id;
92-
this.ref = ref == null ? "" : ref;
93-
}
94-
95-
/**
96-
* Constructor
97-
*
98-
* @param id the exception id, must be unique developer insereted string, from date
99-
*/
100-
public Eid(String id) {
101-
this(id, null);
102-
}
103-
104149
@Override
105150
public String toString() {
106151
if ("".equals(ref)) {
@@ -138,11 +183,11 @@ public String getUniq() {
138183

139184
static void validateFormat(String format, int numSpecifiers) throws NullPointerException, IllegalArgumentException {
140185
if (format == null) {
141-
throw new NullPointerException("Format can't be null, but just recieved one");
186+
throw new IllegalArgumentException(new NullPointerException("Format can't be null, but just recieved one"));
142187
}
143188
List<String> specifiers = new ArrayList<>();
144189
for (int i = 0; i < numSpecifiers; i++) {
145-
specifiers.add(i + Integer.toString(abs(RANDOM.nextInt()), 36));
190+
specifiers.add(i + "-test-id");
146191
}
147192
String formated = String.format(format, specifiers.toArray());
148193
for (String specifier : specifiers) {
@@ -153,4 +198,44 @@ static void validateFormat(String format, int numSpecifiers) throws NullPointerE
153198
}
154199
}
155200

201+
/**
202+
* It is used to generate unique ID for each EID object. It's mustn't be secure becouse it just indicate EID object while
203+
* logging.
204+
*/
205+
public static interface UniqIdGenerator {
206+
207+
/**
208+
* Generates a uniq string ID
209+
*
210+
* @return a generated unique ID
211+
*/
212+
String generateUniqId();
213+
}
214+
215+
private static class StdUniqIdGenerator implements UniqIdGenerator {
216+
217+
private static final int BASE36 = 36;
218+
219+
private final Random random;
220+
221+
public StdUniqIdGenerator() {
222+
this.random = getUnsecureFastRandom();
223+
}
224+
225+
@Override
226+
public String generateUniqId() {
227+
long first = abs(random.nextLong());
228+
int second = abs(random.nextInt(Integer.MAX_VALUE));
229+
int calc = (int) (first + second);
230+
return Integer.toString(calc, BASE36);
231+
}
232+
233+
private Random getUnsecureFastRandom() {
234+
@SuppressWarnings("squid:S2245")
235+
Random ret = new Random(System.currentTimeMillis());
236+
return ret;
237+
}
238+
239+
}
240+
156241
}

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package pl.wavesoftware.eid.exceptions;
1717

18-
import static pl.wavesoftware.eid.exceptions.Eid.validateFormat;
19-
2018
/**
2119
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
2220
* of end user applications which will report Bugs in standarized error pages or post them to issue tracker.
@@ -37,10 +35,6 @@
3735
*/
3836
public class EidRuntimeException extends RuntimeException implements EidContainer {
3937

40-
public static final String DEFAULT_MESSAGE_FORMAT = "%s => %s";
41-
42-
private static String messageFormat = DEFAULT_MESSAGE_FORMAT;
43-
4438
private static final long serialVersionUID = -9876432123423587L;
4539

4640
private final Eid eid;
@@ -102,7 +96,7 @@ public EidRuntimeException(Eid id) {
10296
* permitted, and indicates that the cause is nonexistent or unknown.)
10397
*/
10498
public EidRuntimeException(Eid id, Throwable cause) {
105-
super(String.format(messageFormat, id.toString(), message(cause)), cause);
99+
super(String.format(Eid.messageFormat, id.toString(), message(cause)), cause);
106100
eid = id;
107101
}
108102

@@ -111,23 +105,6 @@ public Eid getEid() {
111105
return eid;
112106
}
113107

114-
/**
115-
* Sets a format that will be used for all Eid exceptions when printing a detail message.
116-
* <p>
117-
* Format must be non-null and contain two format specifiers <tt>"%s"</tt>
118-
*
119-
* @param format a format that will be used, must be non-null and contain two format specifiers <tt>"%s"</tt>
120-
* @return previously used format
121-
* @throws NullPointerException if given format was null
122-
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>
123-
*/
124-
public static String setMessageFormat(String format) throws NullPointerException, IllegalArgumentException {
125-
validateFormat(format, 2);
126-
String oldFormat = EidRuntimeException.messageFormat;
127-
EidRuntimeException.messageFormat = format;
128-
return oldFormat;
129-
}
130-
131108
/**
132109
* Returns a standard JDK class that this ones is base on. It doesn't mean this class extends that class.
133110
*

src/main/java/pl/wavesoftware/eid/utils/EidPreconditions.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public static <R> R tryToExecute(@Nonnull RiskyCode<R> code, @Nonnull String eid
282282
String checkedEid = checkNotNull(eid);
283283
try {
284284
return code.execute();
285-
} catch (Exception throwable) { // NOSONAR
285+
} catch (Exception throwable) {
286286
throw new EidRuntimeException(new Eid(checkedEid), throwable);
287287
}
288288
}
@@ -317,7 +317,7 @@ public static <R> R tryToExecute(@Nonnull RiskyCode<R> code, @Nonnull Eid eid) {
317317
Eid checkedEid = checkNotNull(eid);
318318
try {
319319
return code.execute();
320-
} catch (Exception throwable) { // NOSONAR
320+
} catch (Exception throwable) {
321321
throw new EidRuntimeException(checkedEid, throwable);
322322
}
323323
}
@@ -335,12 +335,16 @@ public interface RiskyCode<R> {
335335
* @return a object of client code
336336
* @throws Exception this exception sould be set to concrete one
337337
*/
338+
@SuppressWarnings({
339+
"pmd:SignatureDeclareThrowsException",
340+
"squid:S00112"
341+
})
338342
R execute() throws Exception;
339343
}
340344

341345
private static <T> T checkNotNull(@Nullable T reference) {
342346
if (reference == null) {
343-
throw new NullPointerException("Pass not-null Eid to EidPreconditions first!");
347+
throw new IllegalArgumentException(new NullPointerException("Pass not-null Eid to EidPreconditions first!"));
344348
}
345349
return reference;
346350
}

src/test/java/pl/wavesoftware/eid/exceptions/EidRuntimeExceptionTest.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)