Skip to content

Commit 61cefeb

Browse files
committed
More Quality fixes from SonarQube
1 parent cbceb4a commit 61cefeb

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public class Eid implements Serializable {
4646

4747
private static final int REF_FORMAT_NUM_SPEC = 3;
4848

49-
static String messageFormat = DEFAULT_MESSAGE_FORMAT;
49+
private static final int MESSAGE_FORMAT_NUM_SPEC = 2;
50+
51+
private static String messageFormat = DEFAULT_MESSAGE_FORMAT;
5052

5153
private static UniqIdGenerator uniqIdGenerator = DEFAULT_UNIQ_ID_GENERATOR;
5254

@@ -88,16 +90,25 @@ public Eid(String id) {
8890
*
8991
* @param format a format that will be used, must be non-null and contain two format specifiers <tt>"%s"</tt>
9092
* @return previously used format
91-
* @throws NullPointerException if given format was null
92-
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>
93+
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>, or if given format was
94+
* null
9395
*/
9496
public static String setMessageFormat(String format) {
95-
validateFormat(format, 2);
97+
validateFormat(format, MESSAGE_FORMAT_NUM_SPEC);
9698
String oldFormat = Eid.messageFormat;
9799
Eid.messageFormat = format;
98100
return oldFormat;
99101
}
100102

103+
/**
104+
* Gets actually set message format
105+
*
106+
* @return actually set message format
107+
*/
108+
public static String getMessageFormat() {
109+
return messageFormat;
110+
}
111+
101112
/**
102113
* Sets the actual unique ID generator that will be used to generate IDs for all Eid objects. It will return previously used
103114
* generator.
@@ -108,7 +119,7 @@ public static String setMessageFormat(String format) {
108119
*/
109120
public static UniqIdGenerator setUniqIdGenerator(UniqIdGenerator uniqIdGenerator) {
110121
if (uniqIdGenerator == null) {
111-
throw new IllegalArgumentException(new NullPointerException("Unique ID generator can't be null, but given one"));
122+
throw new IllegalArgumentException("Unique ID generator can't be null, but given one");
112123
}
113124
UniqIdGenerator previous = Eid.uniqIdGenerator;
114125
Eid.uniqIdGenerator = uniqIdGenerator;
@@ -135,7 +146,6 @@ public static String setFormat(String format) {
135146
*
136147
* @param refFormat a format compliant with {@link String#format(String, Object...)} with 3 object arguments
137148
* @return a previously used format
138-
* @throws NullPointerException if given format was null
139149
* @throws IllegalArgumentException if given format hasn't got tree format specifiers <tt>"%s"</tt>, or if given format was
140150
* null
141151
*/
@@ -181,9 +191,9 @@ public String getUniq() {
181191
return uniq;
182192
}
183193

184-
static void validateFormat(String format, int numSpecifiers) throws NullPointerException, IllegalArgumentException {
194+
static void validateFormat(String format, int numSpecifiers) throws IllegalArgumentException {
185195
if (format == null) {
186-
throw new IllegalArgumentException(new NullPointerException("Format can't be null, but just recieved one"));
196+
throw new IllegalArgumentException("Format can't be null, but just recieved one");
187197
}
188198
List<String> specifiers = new ArrayList<>();
189199
for (int i = 0; i < numSpecifiers; i++) {
@@ -230,10 +240,9 @@ public String generateUniqId() {
230240
return Integer.toString(calc, BASE36);
231241
}
232242

243+
@SuppressWarnings("squid:S2245")
233244
private Random getUnsecureFastRandom() {
234-
@SuppressWarnings("squid:S2245")
235-
Random ret = new Random(System.currentTimeMillis());
236-
return ret;
245+
return new Random(System.currentTimeMillis());
237246
}
238247

239248
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public EidRuntimeException(Eid id) {
9696
* permitted, and indicates that the cause is nonexistent or unknown.)
9797
*/
9898
public EidRuntimeException(Eid id, Throwable cause) {
99-
super(String.format(Eid.messageFormat, id.toString(), message(cause)), cause);
99+
super(String.format(Eid.getMessageFormat(), id.toString(), message(cause)), cause);
100100
eid = id;
101101
}
102102

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static void checkArgument(@Nonnull boolean expression, @Nonnull Eid eid)
147147
* @param expression a boolean expression
148148
* @param eid the exception message to use if the check fails; will be converted to a string using
149149
* {@link String#valueOf(Object)}
150-
* @throws IllegalStateException if {@code expression} is false
150+
* @throws EidIllegalStateException if {@code expression} is false
151151
*/
152152
public static void checkState(@Nonnull boolean expression, @Nonnull String eid) {
153153
String checkedEid = checkNotNull(eid);
@@ -163,7 +163,7 @@ public static void checkState(@Nonnull boolean expression, @Nonnull String eid)
163163
* @param expression a boolean expression
164164
* @param eid the exception message to use if the check fails; will be converted to a string using
165165
* {@link String#valueOf(Object)}
166-
* @throws IllegalStateException if {@code expression} is false
166+
* @throws EidIllegalStateException if {@code expression} is false
167167
*/
168168
public static void checkState(@Nonnull boolean expression, @Nonnull Eid eid) {
169169
Eid checkedEid = checkNotNull(eid);
@@ -180,7 +180,7 @@ public static void checkState(@Nonnull boolean expression, @Nonnull Eid eid) {
180180
* @param eid the exception message to use if the check fails; will be converted to a string using
181181
* {@link String#valueOf(Object)}
182182
* @return the non-null reference that was validated
183-
* @throws NullPointerException if {@code reference} is null
183+
* @throws EidNullPointerException if {@code reference} is null
184184
*/
185185
@Nonnull
186186
public static <T> T checkNotNull(@Nullable T reference, @Nonnull String eid) {
@@ -199,7 +199,7 @@ public static <T> T checkNotNull(@Nullable T reference, @Nonnull String eid) {
199199
* @param eid the exception message to use if the check fails; will be converted to a string using
200200
* {@link String#valueOf(Object)}
201201
* @return the non-null reference that was validated
202-
* @throws NullPointerException if {@code reference} is null
202+
* @throws EidNullPointerException if {@code reference} is null
203203
*/
204204
@Nonnull
205205
public static <T> T checkNotNull(@Nullable T reference, @Nonnull Eid eid) {
@@ -218,12 +218,15 @@ public static <T> T checkNotNull(@Nullable T reference, @Nonnull Eid eid) {
218218
* @param size the size of that array, list or string
219219
* @param eid the text to use to describe this index in an error message
220220
* @return the value of {@code index}
221-
* @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
222-
* @throws IllegalArgumentException if {@code size} is negative
221+
* @throws EidIndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
222+
* @throws EidIllegalArgumentException if {@code size} is negative
223223
*/
224224
@Nonnull
225225
public static int checkElementIndex(int index, int size, @Nonnull String eid) {
226226
String checkedEid = checkNotNull(eid);
227+
if (size < 0) {
228+
throw new EidIllegalArgumentException(new Eid(checkedEid));
229+
}
227230
// Carefully optimized for execution by hotspot (explanatory comment above)
228231
if (index < 0 || index > size) {
229232
throw new EidIndexOutOfBoundsException(new Eid(checkedEid));
@@ -239,12 +242,15 @@ public static int checkElementIndex(int index, int size, @Nonnull String eid) {
239242
* @param size the size of that array, list or string
240243
* @param eid the text to use to describe this index in an error message
241244
* @return the value of {@code index}
242-
* @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
243-
* @throws IllegalArgumentException if {@code size} is negative
245+
* @throws EidIndexOutOfBoundsException if {@code index} is negative or is not less than {@code size}
246+
* @throws EidIllegalArgumentException if {@code size} is negative
244247
*/
245248
@Nonnull
246249
public static int checkElementIndex(int index, int size, @Nonnull Eid eid) {
247250
Eid checkedEid = checkNotNull(eid);
251+
if (size < 0) {
252+
throw new EidIllegalArgumentException(checkedEid);
253+
}
248254
// Carefully optimized for execution by hotspot (explanatory comment above)
249255
if (index < 0 || index > size) {
250256
throw new EidIndexOutOfBoundsException(checkedEid);
@@ -272,6 +278,7 @@ public static int checkElementIndex(int index, int size, @Nonnull Eid eid) {
272278
* @param code code to be executed within a try-catch block
273279
* @param eid uniq developer identifier from date for ex.: "20150716:123200"
274280
* @return A block of code return type, if exception is not thrown
281+
* @throws EidRuntimeException if code block thrown any exception, which in that case is wrapped in EidRuntimeException
275282
*/
276283
@Nullable
277284
@SuppressWarnings({
@@ -307,6 +314,7 @@ public static <R> R tryToExecute(@Nonnull RiskyCode<R> code, @Nonnull String eid
307314
* @param code code to be executed within a try-catch block
308315
* @param eid uniq developer identifier from date for ex.: "20150716:123200"
309316
* @return A block of code return type, if exception is not thrown
317+
* @throws EidRuntimeException if code block thrown any exception, which in that case is wrapped in EidRuntimeException
310318
*/
311319
@Nullable
312320
@SuppressWarnings({
@@ -344,7 +352,7 @@ public interface RiskyCode<R> {
344352

345353
private static <T> T checkNotNull(@Nullable T reference) {
346354
if (reference == null) {
347-
throw new IllegalArgumentException(new NullPointerException("Pass not-null Eid to EidPreconditions first!"));
355+
throw new IllegalArgumentException("Pass not-null Eid to EidPreconditions first!");
348356
}
349357
return reference;
350358
}

0 commit comments

Comments
 (0)