Skip to content

Commit f752eaa

Browse files
committed
refactoring and test covering
1 parent 0a53612 commit f752eaa

5 files changed

Lines changed: 161 additions & 95 deletions

File tree

rlib-common/src/main/java/com/ss/rlib/common/Monitoring.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*
1111
* @author JavaSaBr
1212
*/
13+
@Deprecated(forRemoval = true)
1314
public final class Monitoring {
1415

1516
@NotNull

rlib-common/src/main/java/com/ss/rlib/common/util/VarTable.java

Lines changed: 83 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.ss.rlib.common.util.dictionary.ObjectDictionary;
99
import org.jetbrains.annotations.NotNull;
1010
import org.jetbrains.annotations.Nullable;
11-
import org.w3c.dom.Node;
1211

1312
/**
1413
* The utility class to contain properties of different types.
@@ -43,7 +42,7 @@ public void clear() {
4342
* @param key the key.
4443
* @param <T> the value's type.
4544
* @return the value.
46-
* @throws IllegalArgumentException if the value is not exist.
45+
* @throws IllegalArgumentException if the value is null.
4746
*/
4847
public <T> @NotNull T get(@NotNull String key) {
4948

@@ -53,44 +52,45 @@ public void clear() {
5352
return result;
5453
}
5554

56-
throw new IllegalArgumentException("not found " + key);
55+
throw new IllegalArgumentException("Not found value for the key: " + key);
5756
}
5857

5958
/**
60-
* Get the value by the key.
59+
* Get a value by a key.
6160
*
62-
* @param <T> the type parameter
61+
* @param <T> the value's type.
6362
* @param key the key.
6463
* @param type the type.
6564
* @return the value.
65+
* @throws IllegalArgumentException if the value is null or has wrong type.
6666
*/
67-
public <T> @NotNull T get(@NotNull final String key, @NotNull final Class<T> type) {
67+
public <T> @NotNull T get(@NotNull String key, @NotNull Class<T> type) {
6868

69-
final Object object = values.get(key);
69+
var object = values.get(key);
7070

7171
if (object == null) {
72-
throw new IllegalArgumentException();
72+
throw new IllegalArgumentException("Not found value for the key: " + key);
7373
} else if (type.isInstance(object)) {
7474
return type.cast(object);
7575
}
7676

77-
throw new IllegalArgumentException("not found " + key);
77+
throw new IllegalArgumentException(
78+
"Value: " + object + " has another type than requested: " + type + " by the key: " + key);
7879
}
7980

8081
/**
81-
* Get the value by the key.
82+
* Get a value by a key.
8283
*
83-
* @param <T> the type parameter
84-
* @param <E> the type parameter
84+
* @param <T> the value's type.
85+
* @param <E> the default value's type.
8586
* @param key the key.
8687
* @param type the type.
8788
* @param def the default value.
88-
* @return the value.
89+
* @return the value or default if the value is null or has wrong type.
8990
*/
90-
public <T, E extends T> @NotNull T get(@NotNull final String key, @NotNull final Class<T> type,
91-
@NotNull final E def) {
91+
public <T, E extends T> @NotNull T get(@NotNull String key, @NotNull Class<T> type, @NotNull E def) {
9292

93-
final Object object = values.get(key);
93+
var object = values.get(key);
9494

9595
if (object == null) {
9696
return def;
@@ -102,149 +102,143 @@ public void clear() {
102102
}
103103

104104
/**
105-
* Get the value by the key.
105+
* Get a value by a key.
106106
*
107-
* @param <T> the type parameter
108-
* @param <E> the type parameter
107+
* @param <T> the value's type.
108+
* @param <E> the default value's type.
109109
* @param key the key.
110110
* @param type the type.
111111
* @param def the default value.
112-
* @return the value.
112+
* @return the value or default if the value not found.
113+
* @throws IllegalArgumentException if the value has wrong type.
113114
*/
114-
public <T, E extends T> @NotNull T getNullable(@NotNull final String key, @NotNull final Class<T> type,
115-
@Nullable final E def) {
115+
public <T, E extends T> @Nullable T getNullable(@NotNull String key, @NotNull Class<T> type, @Nullable E def) {
116116

117-
final Object object = values.get(key);
117+
var object = values.get(key);
118118

119119
if (object == null) {
120120
return def;
121121
} else if (type.isInstance(object)) {
122122
return type.cast(object);
123123
}
124124

125-
return def;
125+
throw new IllegalArgumentException(
126+
"Value: " + object + " has another type than requested: " + type + " by the key: " + key);
126127
}
127128

128129
/**
129-
* Get the value by the key.
130+
* Get a value by a key.
130131
*
131-
* @param <T> the type parameter
132+
* @param <T> the value's type.
132133
* @param key the key.
133134
* @param def the default value.
134-
* @return the value.
135+
* @return the value or default if the value is null.
135136
*/
136-
public <T> @NotNull T get(@NotNull final String key, @NotNull final T def) {
137+
public <T> @NotNull T get(@NotNull String key, @NotNull T def) {
137138

138-
final Object object = values.get(key);
139-
if (object == null) return def;
139+
var object = values.get(key);
140140

141-
final Class<?> type = def.getClass();
141+
if (object == null) {
142+
return def;
143+
}
144+
145+
var type = def.getClass();
142146

143147
if (type.isInstance(object)) {
144148
return notNull(ClassUtils.unsafeCast(object));
145149
}
146150

147-
return def;
151+
throw new IllegalArgumentException(
152+
"Value: " + object + " has another type than requested: " + type + " by the key: " + key);
148153
}
149154

150155
/**
151-
* Get the value by the key.
156+
* Get a value by a key.
152157
*
153-
* @param <T> the type parameter
158+
* @param <T> the value's type.
154159
* @param key the key.
155160
* @param def the default value.
156-
* @return the value.
161+
* @return the value or default if the value is not found.
157162
*/
158-
public <T> @NotNull T getNullable(@NotNull final String key, @Nullable final T def) {
159-
160-
final Object object = values.get(key);
161-
if (object == null || def == null) return def;
163+
public <T> @Nullable T getNullable(@NotNull String key, @Nullable T def) {
162164

163-
final Class<?> type = def.getClass();
165+
var object = values.get(key);
164166

165-
if (type.isInstance(object)) {
166-
return notNull(ClassUtils.unsafeCast(object));
167+
if (object == null) {
168+
return def;
167169
}
168170

169-
return def;
170-
}
171+
if (def != null) {
171172

172-
/**
173-
* Get an array by the key.
174-
*
175-
* @param <T> the type parameter
176-
* @param key the key.
177-
* @param type the type.
178-
* @return the array.
179-
*/
180-
public <T> @NotNull T[] getArray(@NotNull final String key, @NotNull final Class<T[]> type) {
173+
var type = def.getClass();
181174

182-
final Object object = values.get(key);
175+
if (type.isInstance(object)) {
176+
return ClassUtils.unsafeCast(object);
177+
}
183178

184-
if (object == null) {
185-
throw new IllegalArgumentException("not found " + key);
186-
} else if (type.isInstance(object)) {
187-
return notNull(ClassUtils.unsafeCast(object));
188-
}
179+
return def;
189180

190-
throw new IllegalArgumentException("not found " + key);
181+
} else {
182+
return ClassUtils.unsafeCast(object);
183+
}
191184
}
192185

193186
/**
194-
* Get an array of the key.
187+
* Get an array of a key.
195188
*
196-
* @param <T> the type parameter
189+
* @param <T> the array element's type.
197190
* @param key the key.
198-
* @param type the type.
191+
* @param type the array's type.
199192
* @param def the default array.
200193
* @return the array.
201194
*/
202195
@SafeVarargs
203-
public final <T> @NotNull T[] getArray(@NotNull final String key, @NotNull final Class<T[]> type,
204-
@NotNull final T... def) {
196+
public final <T> @NotNull T[] getArray(@NotNull String key, @NotNull Class<T[]> type, @NotNull T... def) {
205197

206-
final Object object = values.get(key);
198+
var object = values.get(key);
207199

208200
if (object == null) {
209201
return def;
210202
} else if (type.isInstance(object)) {
211-
return notNull(ClassUtils.unsafeCast(object));
203+
return ClassUtils.unsafeNNCast(object);
212204
}
213205

214-
throw new IllegalArgumentException("not found " + key);
206+
throw new IllegalArgumentException(
207+
"Value: " + object + " has another type than requested: " + type + " by the key: " + key);
215208
}
216209

217210
/**
218-
* Get a boolean value by the key.
211+
* Get a boolean value by a key.
219212
*
220213
* @param key the key.
221214
* @return the value.
222215
*/
223-
public boolean getBoolean(@NotNull final String key) {
216+
public boolean getBoolean(@NotNull String key) {
224217

225-
final Object object = values.get(key);
218+
var object = values.get(key);
226219

227220
if (object == null) {
228-
throw new IllegalArgumentException("not found " + key);
221+
throw new IllegalArgumentException("Not found value for the key: " + key);
229222
} else if (object instanceof Boolean) {
230223
return (Boolean) object;
231224
} else if (object instanceof String) {
232225
return Boolean.parseBoolean(object.toString());
233226
}
234227

235-
throw new IllegalArgumentException("not found " + key);
228+
throw new IllegalArgumentException(
229+
"Value: " + object + " cannot be converted to boolean by the key: " + key);
236230
}
237231

238232
/**
239-
* Get a boolean value by the key.
233+
* Get a boolean value by a key.
240234
*
241235
* @param key the key.
242236
* @param def the default value.
243237
* @return the value.
244238
*/
245-
public boolean getBoolean(@NotNull final String key, final boolean def) {
239+
public boolean getBoolean(@NotNull String key, boolean def) {
246240

247-
final Object object = values.get(key);
241+
var object = values.get(key);
248242

249243
if (object == null) {
250244
return def;
@@ -254,41 +248,43 @@ public boolean getBoolean(@NotNull final String key, final boolean def) {
254248
return Boolean.parseBoolean(object.toString());
255249
}
256250

257-
return def;
251+
throw new IllegalArgumentException(
252+
"Value: " + object + " cannot be converted to boolean by the key: " + key);
258253
}
259254

260255
/**
261-
* Get a boolean array by the key.
256+
* Get a boolean array by a key.
262257
*
263258
* @param key the key.
264259
* @param regex the regex to split if a value is string.
265260
* @return the boolean array.
266261
*/
267-
public @NotNull boolean[] getBooleanArray(@NotNull final String key, @NotNull final String regex) {
262+
public @NotNull boolean[] getBooleanArray(@NotNull String key, @NotNull String regex) {
268263

269-
final Object object = values.get(key);
264+
var object = values.get(key);
270265

271266
if (object == null) {
272-
throw new IllegalArgumentException("not found " + key);
267+
throw new IllegalArgumentException("Not found value for the key: " + key);
273268
} else if (object instanceof boolean[]) {
274269
return (boolean[]) object;
275270
} else if (object instanceof String) {
276271
return parseBooleanArray(regex, object);
277272
}
278273

279-
throw new IllegalArgumentException("not found " + key);
274+
throw new IllegalArgumentException(
275+
"Value: " + object + " cannot be converted to boolean array by the key: " + key);
280276
}
281277

282-
private boolean[] parseBooleanArray(@NotNull final String regex, @NotNull final Object object) {
278+
private boolean[] parseBooleanArray(@NotNull String regex, @NotNull Object object) {
283279

284-
final String[] strings = object.toString().split(regex);
285-
final boolean[] result = new boolean[strings.length];
280+
var strings = object.toString().split(regex);
281+
var booleans = new boolean[strings.length];
286282

287283
for (int i = 0, length = strings.length; i < length; i++) {
288-
result[i] = Boolean.parseBoolean(strings[i]);
284+
booleans[i] = Boolean.parseBoolean(strings[i].trim());
289285
}
290286

291-
return result;
287+
return booleans;
292288
}
293289

294290
/**

rlib-common/src/main/java/com/ss/rlib/common/util/XmlUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*
1111
* @author JavaSaBR
1212
*/
13+
@Deprecated(forRemoval = true)
1314
public class XmlUtils {
1415

1516
/**

0 commit comments

Comments
 (0)