Skip to content

Commit

Permalink
Adding partial changes to support message format in EIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Nov 17, 2015
1 parent 08e334a commit a2a3740
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 130 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<configuration>
<compilerArgs>
<arg>-Werror</arg>
<arg>-Xlint:-deprecation</arg>
<arg>-Xlint:all</arg>
</compilerArgs>
</configuration>
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/pl/wavesoftware/eid/exceptions/Eid.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package pl.wavesoftware.eid.exceptions;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import static java.lang.Math.abs;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.annotation.Nullable;

import static java.lang.Math.abs;

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

/**
* Makes a log message from this EID object
* <p>
* <p>This method is for convenience of usage of EID in logging. You can use it like this:
* <p>
* <pre>
* log.debug(new Eid("20151025:202129").makeLogMessage("A request: %s", request));
* </pre>
* @param logMessageFormat a log message format as accepted by {@link String#format(String, Object...)}
* @param parameters a parameters for logMessageFormat to by passed to {@link String#format(String, Object...)}
* @return a formatted message
*/
public String makeLogMessage(@Nonnull String logMessageFormat, @Nonnull Object... parameters) {
String message = String.format(logMessageFormat, parameters);
return String.format(getMessageFormat(), this.toString(), message);
}

@Override
public String toString() {
if ("".equals(ref)) {
Expand Down Expand Up @@ -238,10 +256,9 @@ public String generateUniqId() {
long first = abs(random.nextLong() + 1);
int second = abs(random.nextInt(Integer.MAX_VALUE));
int calc = (int) (first + second);
return Integer.toString(calc, BASE36);
return Integer.toString(abs(calc), BASE36);
}

@SuppressWarnings("squid:S2245")
private Random getUnsecuredFastRandom() {
return new Random(System.currentTimeMillis());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public EidIllegalArgumentException(Eid id) {
super(id);
}

/**
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
*/
public EidIllegalArgumentException(Eid id, String messageFormat, Object... parameters) {
super(id, messageFormat, parameters);
}

/**
* @inheritdoc
* @return {@link IllegalArgumentException} class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public EidIllegalStateException(Eid id) {
super(id);
}

/**
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
*/
public EidIllegalStateException(Eid id, String messageFormat, Object... parameters) {
super(id, messageFormat, parameters);
}

/**
* @inheritdoc
* @return {@link IllegalStateException} class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public EidIndexOutOfBoundsException(Eid id) {
super(id);
}

/**
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
*/
public EidIndexOutOfBoundsException(Eid id, String messageFormat, Object... parameters) {
super(id, messageFormat, parameters);
}

/**
* @inheritdoc
* @return {@link IndexOutOfBoundsException} class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public EidNullPointerException(Eid id) {
super(id);
}

/**
* @see EidRuntimeException#EidRuntimeException(Eid, String, Object...)
* @param id see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param messageFormat see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
* @param parameters see description on {@link EidRuntimeException#EidRuntimeException(Eid, String, Object...)}
*/
public EidNullPointerException(Eid id, String messageFormat, Object... parameters) {
super(id, messageFormat, parameters);
}

/**
* @inheritdoc
* @return {@link NullPointerException} class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package pl.wavesoftware.eid.exceptions;

import javax.annotation.Nullable;
import java.util.Locale;

/**
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
Expand Down Expand Up @@ -62,7 +63,6 @@ public EidRuntimeException(String eid, String ref) {
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt>
* value is permitted, and indicates that the cause is nonexistent or unknown.)
*/
@SuppressWarnings("WeakerAccess")
public EidRuntimeException(String eid, Throwable cause) {
this(new Eid(eid), cause);
}
Expand All @@ -75,7 +75,6 @@ public EidRuntimeException(String eid, Throwable cause) {
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or unknown.)
*/
@SuppressWarnings("WeakerAccess")
public EidRuntimeException(String eid, String ref, Throwable cause) {
this(new Eid(eid, ref), cause);
}
Expand All @@ -85,7 +84,6 @@ public EidRuntimeException(String eid, String ref, Throwable cause) {
*
* @param id exception ID
*/
@SuppressWarnings("WeakerAccess")
public EidRuntimeException(Eid id) {
super(id.toString());
eid = id;
Expand All @@ -94,14 +92,33 @@ public EidRuntimeException(Eid id) {
/**
* Constructs a new runtime exception with the specified Eid object and cause
* <p>
* The detail message is computed as <tt>String.format(messageFormat, id.toString(), message(cause)</tt>
* The detail message is computed as <tt>String.format(Eid.getMessageFormat(), id.toString(), message(cause))</tt>
*
* @param id exception ID
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or unknown.)
*/
public EidRuntimeException(Eid id, Throwable cause) {
super(String.format(Eid.getMessageFormat(), id.toString(), message(cause)), cause);
super(String.format(
Locale.ENGLISH, Eid.getMessageFormat(), id.toString(), message(cause)
), cause);
eid = id;
}

/**
* Constructs a new runtime exception with specified Eid object and custom message
* <p>
* The detail message is computed as:
* <pre>String.format(Eid.getMessageFormat(), id.toString(), String.format(messageFormat, parameters))</pre>
* @param id exception ID
* @param messageFormat message format in form of {@link String#format(String, Object...)}
* @param parameters parameters in form of {@link String#format(String, Object...)}
*/
public EidRuntimeException(Eid id, String messageFormat, Object... parameters) {
super(String.format(
Locale.ENGLISH, Eid.getMessageFormat(), id.toString(),
String.format(Locale.ENGLISH, messageFormat, parameters)
));
eid = id;
}

Expand Down
Loading

0 comments on commit a2a3740

Please sign in to comment.