Skip to content

Commit bb79c12

Browse files
committed
refactor: Optimised codes.
Move all private or protected to the last of files.
1 parent d593c83 commit bb79c12

File tree

10 files changed

+296
-283
lines changed

10 files changed

+296
-283
lines changed

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/AesUtil.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@
4444
@Slf4j
4545
public final class AesUtil {
4646

47-
private AesUtil() {
48-
}
49-
50-
private static final String AES = "AES";
51-
52-
private static final String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
53-
5447
/**
5548
* Encrypts the data using the AES algorithm with the given secret.
5649
*
@@ -133,4 +126,20 @@ public static String generateRandomSecret() {
133126
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
134127
}
135128

129+
/**
130+
* Private constructor will protect this class from being instantiated.
131+
*/
132+
private AesUtil() {
133+
}
134+
135+
/**
136+
* The algorithm AES.
137+
*/
138+
private static final String AES = "AES";
139+
140+
/**
141+
* The algorithm AES/CBC/PKCS5Padding.
142+
*/
143+
private static final String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
144+
136145
}

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/Base64Util.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@
5656
*/
5757
public final class Base64Util {
5858

59-
private static Base64.Encoder encoder;
60-
61-
private static Base64.Decoder decoder;
62-
63-
private static Base64.Encoder urlEncoder;
64-
65-
private static Base64.Decoder urlDecoder;
66-
6759
/**
6860
* Ensure that there is only one Base64 Encoder.
6961
*
@@ -210,4 +202,12 @@ public static String decodeUrlComponents(String value) {
210202
return decodeUrlComponents(value, StandardCharsets.UTF_8);
211203
}
212204

205+
private static Base64.Encoder encoder;
206+
207+
private static Base64.Decoder decoder;
208+
209+
private static Base64.Encoder urlEncoder;
210+
211+
private static Base64.Decoder urlDecoder;
212+
213213
}

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/BranchUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@
7777
*/
7878
public final class BranchUtil<T> {
7979

80-
/**
81-
* The final result of the boolean expression.
82-
*/
83-
private final boolean result;
84-
8580
/**
8681
* Create a {@code BranchUtil} instance.
8782
*
@@ -239,4 +234,9 @@ public void handle(Runnable ifHandler) {
239234
handle(ifHandler, null);
240235
}
241236

237+
/**
238+
* The final result of the boolean expression.
239+
*/
240+
private final boolean result;
241+
242242
}

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/ChainedCalcUtil.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@
9595
@Getter
9696
public final class ChainedCalcUtil {
9797

98-
/**
99-
* -- GETTER --
100-
* Returns the current value as a BigDecimal.
101-
*/
102-
private BigDecimal value;
103-
10498
/**
10599
* Creates a {@code ChainedCalcUtil} instance with the specified initial
106100
* value.
@@ -359,4 +353,10 @@ private BigDecimal convertBigDecimal(Object value, Integer scale) {
359353
return res;
360354
}
361355

356+
/**
357+
* -- GETTER --
358+
* Returns the current value as a BigDecimal.
359+
*/
360+
private BigDecimal value;
361+
362362
}

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/HashUtil.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -69,48 +69,6 @@
6969
*/
7070
public final class HashUtil {
7171

72-
/**
73-
* Private constructor to prevent instantiation
74-
*/
75-
private HashUtil() {
76-
}
77-
78-
/**
79-
* Calculates the hash value of the specified string using the specified
80-
* algorithm and charset.
81-
*
82-
* @param method the hash algorithm to use
83-
* @param value the string to calculate the hash value for
84-
* @param charset the charset to use for encoding the string (default is
85-
* UTF-8 if null)
86-
* @return the hash value as a hexadecimal string, or an empty string if
87-
* the algorithm is not available
88-
* @throws RuntimeException if an unknown algorithm name is provided
89-
* (should not occur under controlled usage)
90-
*/
91-
private static String hash(String method, String value, Charset charset) {
92-
try {
93-
var messageDigest = MessageDigest.getInstance(method);
94-
messageDigest.update(value.getBytes(charset));
95-
var bytes = messageDigest.digest();
96-
var builder = new StringBuilder();
97-
98-
for (var b : bytes) {
99-
var str = Integer.toHexString(b & 0xff);
100-
if (str.length() == 1) {
101-
builder.append(0);
102-
}
103-
builder.append(str);
104-
}
105-
106-
return builder.toString();
107-
} catch (NoSuchAlgorithmException ignored) {
108-
// This should not occur under controlled usage
109-
// Only trusted algorithms are allowed
110-
return "";
111-
}
112-
}
113-
11472
/**
11573
* Calculates the MD2 hash value of the specified string using the given
11674
* charset.
@@ -286,4 +244,46 @@ public static String sha512(String value) {
286244
return hash("SHA-512", value, StandardCharsets.UTF_8);
287245
}
288246

247+
/**
248+
* Private constructor will protect this class from being instantiated.
249+
*/
250+
private HashUtil() {
251+
}
252+
253+
/**
254+
* Calculates the hash value of the specified string using the specified
255+
* algorithm and charset.
256+
*
257+
* @param method the hash algorithm to use
258+
* @param value the string to calculate the hash value for
259+
* @param charset the charset to use for encoding the string (default is
260+
* UTF-8 if null)
261+
* @return the hash value as a hexadecimal string, or an empty string if
262+
* the algorithm is not available
263+
* @throws RuntimeException if an unknown algorithm name is provided
264+
* (should not occur under controlled usage)
265+
*/
266+
private static String hash(String method, String value, Charset charset) {
267+
try {
268+
var messageDigest = MessageDigest.getInstance(method);
269+
messageDigest.update(value.getBytes(charset));
270+
var bytes = messageDigest.digest();
271+
var builder = new StringBuilder();
272+
273+
for (var b : bytes) {
274+
var str = Integer.toHexString(b & 0xff);
275+
if (str.length() == 1) {
276+
builder.append(0);
277+
}
278+
builder.append(str);
279+
}
280+
281+
return builder.toString();
282+
} catch (NoSuchAlgorithmException ignored) {
283+
// This should not occur under controlled usage
284+
// Only trusted algorithms are allowed
285+
return "";
286+
}
287+
}
288+
289289
}

devkit-utils/src/main/java/cn/org/codecrafters/devkit/utils/MapUtil.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@
3737
@Slf4j
3838
public final class MapUtil {
3939

40-
/**
41-
* Private constructor to prevent instantiation of MapUtil.
42-
*/
43-
private MapUtil() {
44-
}
45-
4640
/**
4741
* Converts an object to a map by mapping the field names to their
4842
* corresponding values.
@@ -219,4 +213,10 @@ private static String defaultObject(Object obj) {
219213
return String.valueOf(obj);
220214
}
221215
}
216+
217+
/**
218+
* Private constructor will protect this class from being instantiated.
219+
*/
220+
private MapUtil() {
221+
}
222222
}

guid/src/main/java/cn/org/codecrafters/guid/SnowflakeGuidCreator.java renamed to guid/src/main/java/cn/org/codecrafters/guid/impl/SnowflakeGuidCreator.java

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18-
package cn.org.codecrafters.guid;
18+
package cn.org.codecrafters.guid.impl;
1919

20+
import cn.org.codecrafters.guid.GuidCreator;
2021
import cn.org.codecrafters.guid.exceptions.TimingException;
2122

2223
import java.time.LocalDateTime;
2324
import java.time.ZoneId;
24-
import java.time.ZoneOffset;
2525

2626
/**
2727
* The {@code SnowflakeGuidCreator} generates unique identifiers using the
@@ -49,48 +49,6 @@
4949
*/
5050
public final class SnowflakeGuidCreator implements GuidCreator<Long> {
5151

52-
/**
53-
* Default custom epoch.
54-
*
55-
* @value 2015-01-01T00:00:00Z
56-
*/
57-
private static final long DEFAULT_CUSTOM_EPOCH = 1_420_070_400_000L;
58-
59-
/**
60-
* The start epoch timestamp to generate IDs from.
61-
*/
62-
private final long startEpoch;
63-
64-
/**
65-
* The number of bits reserved for the worker ID.
66-
*/
67-
private final long workerIdBits = 5L;
68-
69-
/**
70-
* The number of bits reserved for the data centre ID.
71-
*/
72-
private final long dataCentreIdBits = 5L;
73-
74-
/**
75-
* The worker ID assigned to this generator.
76-
*/
77-
private final long workerId;
78-
79-
/**
80-
* The data centre ID assigned to this generator.
81-
*/
82-
private final long dataCentreId;
83-
84-
/**
85-
* The current sequence number.
86-
*/
87-
private long sequence = 0L;
88-
89-
/**
90-
* The timestamp of the last generated ID.
91-
*/
92-
private long lastTimestamp = -1L;
93-
9452
/**
9553
* Constructs a SnowflakeGuidGenerator with the default start epoch and
9654
* custom worker ID, data centre ID.
@@ -204,5 +162,48 @@ private long awaitToNextMillis(long lastTimestamp) {
204162
private long currentTimestamp() {
205163
return LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
206164
}
165+
166+
/**
167+
* Default custom epoch.
168+
*
169+
* @value 2015-01-01T00:00:00Z
170+
*/
171+
private static final long DEFAULT_CUSTOM_EPOCH = 1_420_070_400_000L;
172+
173+
/**
174+
* The start epoch timestamp to generate IDs from.
175+
*/
176+
private final long startEpoch;
177+
178+
/**
179+
* The number of bits reserved for the worker ID.
180+
*/
181+
private final long workerIdBits = 5L;
182+
183+
/**
184+
* The number of bits reserved for the data centre ID.
185+
*/
186+
private final long dataCentreIdBits = 5L;
187+
188+
/**
189+
* The worker ID assigned to this generator.
190+
*/
191+
private final long workerId;
192+
193+
/**
194+
* The data centre ID assigned to this generator.
195+
*/
196+
private final long dataCentreId;
197+
198+
/**
199+
* The current sequence number.
200+
*/
201+
private long sequence = 0L;
202+
203+
/**
204+
* The timestamp of the last generated ID.
205+
*/
206+
private long lastTimestamp = -1L;
207+
207208
}
208209

property-guard-spring-boot-starter/src/main/java/cn/org/codecrafters/propertyguard/autoconfiguration/PropertyGuard.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@
7070
*/
7171
public class PropertyGuard implements EnvironmentPostProcessor {
7272

73-
private final String PREFIX = "pg";
74-
7573
/**
7674
* Process the encryption environment variables.
7775
*
@@ -108,4 +106,6 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
108106
}
109107
}
110108
}
109+
110+
private static final String PREFIX = "pg";
111111
}

0 commit comments

Comments
 (0)