@@ -36,9 +36,19 @@ public class Eid implements Serializable {
36
36
37
37
public static final String DEFAULT_REF_FORMAT = "[%s|%s]<%s>" ;
38
38
39
+ public static final String DEFAULT_MESSAGE_FORMAT = "%s => %s" ;
40
+
41
+ public static final UniqIdGenerator DEFAULT_UNIQ_ID_GENERATOR = new StdUniqIdGenerator ();
42
+
39
43
private static final long serialVersionUID = -9876432123423401L ;
40
44
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 ;
42
52
43
53
private static String format = DEFAULT_FORMAT ;
44
54
@@ -51,15 +61,70 @@ public class Eid implements Serializable {
51
61
private final String uniq ;
52
62
53
63
/**
54
- * Sets the actual format that will be used in {@link toString()} method. It will return previously used format.
64
+ * Constructor
55
65
*
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
58
91
* @throws NullPointerException if given format was null
59
92
* @throws IllegalArgumentException if given format hasn't got two format specifiers <tt>"%s"</tt>
60
93
*/
61
- public static String setFormat (String format ) throws NullPointerException , IllegalArgumentException {
94
+ public static String setMessageFormat (String format ) {
62
95
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 );
63
128
String prevoiusly = Eid .format ;
64
129
Eid .format = format ;
65
130
return prevoiusly ;
@@ -71,36 +136,16 @@ public static String setFormat(String format) throws NullPointerException, Illeg
71
136
* @param refFormat a format compliant with {@link String#format(String, Object...)} with 3 object arguments
72
137
* @return a previously used format
73
138
* @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
75
141
*/
76
142
public static String setRefFormat (String refFormat ) {
77
- validateFormat (refFormat , 3 );
143
+ validateFormat (refFormat , REF_FORMAT_NUM_SPEC );
78
144
String prevoiusly = Eid .refFormat ;
79
145
Eid .refFormat = refFormat ;
80
146
return prevoiusly ;
81
147
}
82
148
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
-
104
149
@ Override
105
150
public String toString () {
106
151
if ("" .equals (ref )) {
@@ -138,11 +183,11 @@ public String getUniq() {
138
183
139
184
static void validateFormat (String format , int numSpecifiers ) throws NullPointerException , IllegalArgumentException {
140
185
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" ) );
142
187
}
143
188
List <String > specifiers = new ArrayList <>();
144
189
for (int i = 0 ; i < numSpecifiers ; i ++) {
145
- specifiers .add (i + Integer . toString ( abs ( RANDOM . nextInt ()), 36 ) );
190
+ specifiers .add (i + "-test-id" );
146
191
}
147
192
String formated = String .format (format , specifiers .toArray ());
148
193
for (String specifier : specifiers ) {
@@ -153,4 +198,44 @@ static void validateFormat(String format, int numSpecifiers) throws NullPointerE
153
198
}
154
199
}
155
200
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
+
156
241
}
0 commit comments