Skip to content

Commit 1d88266

Browse files
author
Zihlu Wang
authored
Merge pull request #45 from OnixByte/develop
v1.7.0
2 parents 27761c0 + 9664d34 commit 1d88266

File tree

17 files changed

+255
-116
lines changed

17 files changed

+255
-116
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
same "printed page" as the copyright notice for easier
188188
identification within third-party archives.
189189

190-
Copyright 2023 CodeCrafters (CN)
190+
Copyright 2023-2024 OnixByte (HK)
191191

192192
Licensed under the Apache License, Version 2.0 (the "License");
193193
you may not use this file except in compliance with the License.

devkit-core/src/main/java/com/onixbyte/devkit/core/exceptions/NotImplementedException.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
package com.onixbyte.devkit.core.exceptions;
1919

2020
/**
21-
* The {@code NotImplementedException} class is a custom runtime exception
22-
* that represents a situation where a particular method or functionality is
23-
* not implemented or is currently unavailable in the codebase.
21+
* The {@code NotImplementedException} class is a custom runtime exception that represents a situation where a
22+
* particular method or functionality is not implemented or is currently unavailable in the codebase.
2423
* <p>
25-
* This exception is typically thrown when developers need to indicate that a
26-
* specific part of the code is incomplete or requires further implementation.
27-
* It serves as a placeholder to highlight unfinished sections of the
28-
* application during development and testing phases.
24+
* This exception is typically thrown when developers need to indicate that a specific part of the code is incomplete
25+
* or requires further implementation. It serves as a placeholder to highlight unfinished sections of the application
26+
* during development and testing phases.
2927
* <p>
3028
* Usage Example:
3129
* <pre>
@@ -54,15 +52,13 @@
5452
public class NotImplementedException extends RuntimeException {
5553

5654
/**
57-
* Creates a new {@code NotImplementedException} with no specific error
58-
* message.
55+
* Creates a new {@code NotImplementedException} with no specific error message.
5956
*/
6057
public NotImplementedException() {
6158
}
6259

6360
/**
64-
* Creates a new {@code NotImplementedException} with the provided error
65-
* message.
61+
* Creates a new {@code NotImplementedException} with the provided error message.
6662
*
6763
* @param message the error message associated with this exception
6864
*/
@@ -71,8 +67,7 @@ public NotImplementedException(String message) {
7167
}
7268

7369
/**
74-
* Creates a new {@code NotImplementedException} with the specified error
75-
* message and a cause for this exception.
70+
* Creates a new {@code NotImplementedException} with the specified error message and a cause for this exception.
7671
*
7772
* @param message the error message associated with this exception
7873
* @param cause the cause of this exception
@@ -91,8 +86,8 @@ public NotImplementedException(Throwable cause) {
9186
}
9287

9388
/**
94-
* Creates a new {@code NotImplementedException} with the specified error
95-
* message, cause, suppression flag, and stack trace writable flag.
89+
* Creates a new {@code NotImplementedException} with the specified error message, cause, suppression flag, and
90+
* stack trace writable flag.
9691
*
9792
* @param message the error message associated with this
9893
* exception

devkit-core/src/main/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
~ Copyright (C) 2023 CodeCraftersCN.
3+
~ Copyright (C) 2023-2024 OnixByte.
44
~
55
~ Licensed under the Apache License, Version 2.0 (the "License");
66
~ you may not use this file except in compliance with the License.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2024-2024 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.devkit.core.exceptions;
19+
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.junit.jupiter.api.Test;
22+
23+
@Slf4j
24+
public class NotImplementationExceptionTest {
25+
26+
@Test
27+
public void testExceptionWithEmptyConstructor() {
28+
try {
29+
throw new NotImplementedException();
30+
} catch (NotImplementedException e) {
31+
log.error("NotImplementedException: ", e);
32+
}
33+
}
34+
35+
@Test
36+
public void testExceptionWithStringConstructor() {
37+
try {
38+
throw new NotImplementedException("This function is not implemented yet, please contact developer for further information.");
39+
} catch (NotImplementedException e) {
40+
log.error("NotImplementedException: ", e);
41+
}
42+
}
43+
44+
}

devkit-utils/src/main/java/com/onixbyte/devkit/utils/AesUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static String decrypt(String data, String secret) {
119119
}
120120

121121
/**
122-
* Generates 16 characters-long random secret.
122+
* Generates 16-character random secret.
123123
*
124124
* @return the generated secure secret
125125
*/

devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Please see documentation for more information.
3232
*
3333
* @author zihluwang
34-
* @version 1.4.2
34+
* @version 1.7.0
3535
* @since 1.0.0
3636
*/
3737
@Slf4j
@@ -40,81 +40,31 @@ public final class MapUtil {
4040
/**
4141
* Converts an object to a map by mapping the field names to their corresponding values.
4242
*
43-
* @param <T> the type of the object
44-
* @param entity the object to be converted to a map
45-
* @param adapters adapts the entity for mapping to a map
43+
* @param <T> the type of the object
44+
* @param entity the object to be converted to a map
45+
* @param adapter adapts the entity for mapping to a map
4646
* @return a map representing the fields and their values of the object
4747
*/
48-
public static <T> Map<String, Object> objectToMap(T entity,
49-
Map<String, ObjectMapAdapter<T, ?>> adapters) {
50-
var resultMap = new HashMap<String, Object>();
51-
adapters.forEach((fieldName, adapter) -> resultMap.put(fieldName, adapter.fetch(entity)));
52-
return resultMap;
48+
public static <T> Map<String, Object> objectToMap(T entity, ObjectMapAdapter<T> adapter) {
49+
return adapter.toMap(entity);
5350
}
5451

5552
/**
5653
* Converts a map to an object of the specified type by setting the field values using the
5754
* map entries.
5855
*
5956
* @param objectMap the map representing the fields and their values
60-
* @param entity an empty entity of the target class
61-
* @param adapters the adapters to execute the setter for the entity
57+
* @param adapter the adapter to execute the setter for the entity
6258
* @param <T> the type of the object to be created
6359
* @return an object of the specified type with the field values set from the map
6460
*/
65-
public static <T> T mapToObject(Map<String, Object> objectMap,
66-
T entity,
67-
Map<String, ObjectMapAdapter<T, ?>> adapters) {
68-
adapters.forEach((fieldName, adapter) -> Optional.ofNullable(objectMap)
69-
.map((data) -> data.get(fieldName))
70-
.ifPresent((fieldValue) -> adapter.setValue(entity, fieldValue)));
71-
return entity;
61+
public static <T> T mapToObject(Map<String, Object> objectMap, ObjectMapAdapter<T> adapter) {
62+
return adapter.toObject(objectMap);
7263
}
7364

7465
/**
75-
* Retrieves the value of a field from an object using reflection.
76-
*
77-
* @param <E> the type of the entity
78-
* @param <T> the type of the field value
79-
* @param entity the object from which to retrieve the field value
80-
* @param adapter the adapter to execute the getter
81-
* @return the value of the field in the object, or null if the field does not exist or cannot
82-
* be accessed
66+
* Private constructor prevent class being instantiated.
8367
*/
84-
public static <E, T> T getFieldValue(E entity, ObjectMapAdapter<E, T> adapter) {
85-
return adapter.fetch(entity);
86-
}
87-
88-
/**
89-
* Sets the value of a field in an object using reflection.
90-
*
91-
* @param <E> the type of the entity
92-
* @param <T> the type of the field value
93-
* @param entity the object in which to set the field value
94-
* @param adapter the adapter to execute the setter
95-
* @param fieldValue the value to be set
96-
*/
97-
public static <E, T> void setFieldValue(E entity,
98-
ObjectMapAdapter<E, T> adapter,
99-
Object fieldValue) {
100-
adapter.setValue(entity, fieldValue);
101-
}
102-
103-
/**
104-
* Casts the specified value to the required type with Optional.
105-
*
106-
* @param value the value to be cast
107-
* @param requiredType the type to which the value should be cast
108-
* @param <T> the type to which the value should be cast
109-
* @return the cast value, or {@code null} if the value is not an instance of the requiredType
110-
*/
111-
public static <T> T cast(Object value, Class<T> requiredType) {
112-
return Optional.ofNullable(requiredType)
113-
.filter((clazz) -> clazz.isInstance(value))
114-
.map((clazz) -> clazz.cast(value))
115-
.orElse(null);
116-
}
117-
11868
private MapUtil() {
11969
}
12070
}

devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,32 @@
1717

1818
package com.onixbyte.devkit.utils;
1919

20-
import java.util.function.BiConsumer;
21-
import java.util.function.Function;
20+
import java.util.Map;
2221

2322
/**
2423
* Adapts an Object to a Map, making conversion between Map and Object much more safe.
2524
*
26-
* @param <E> entity type
2725
* @param <T> field type
2826
* @author zihluwang
29-
* @version 1.4.2
27+
* @version 1.7.0
3028
* @since 1.4.2
3129
*/
32-
public class ObjectMapAdapter<E, T> {
33-
34-
private final Function<E, T> getter;
35-
36-
private final BiConsumer<E, Object> setter;
37-
38-
/**
39-
* Create an adapter.
40-
*
41-
* @param getter the getter of the field
42-
* @param setter the setter of the field
43-
*/
44-
public ObjectMapAdapter(Function<E, T> getter, BiConsumer<E, Object> setter) {
45-
this.getter = getter;
46-
this.setter = setter;
47-
}
30+
public interface ObjectMapAdapter<T> {
4831

4932
/**
50-
* Get data from the entity.
33+
* Convert an object to a map.
5134
*
52-
* @param entity the source of the data
53-
* @return the data
35+
* @param element the element that will be converted to Map
36+
* @return a Map that is converted from the element
5437
*/
55-
public T fetch(E entity) {
56-
return getter.apply(entity);
57-
}
38+
Map<String, Object> toMap(T element);
5839

5940
/**
60-
* Set value to the entity.
41+
* Convert a Map to an object.
6142
*
62-
* @param entity the target of the data
63-
* @param value the value
43+
* @param map the map that will be converted to Object
44+
* @return the object that is converted from the Map
6445
*/
65-
public void setValue(E entity, Object value) {
66-
setter.accept(entity, value);
67-
}
46+
T toObject(Map<String, Object> map);
6847

6948
}

devkit-utils/src/main/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
~ Copyright (C) 2023 CodeCraftersCN.
3+
~ Copyright (C) 2023-2024 OnixByte.
44
~
55
~ Licensed under the Apache License, Version 2.0 (the "License");
66
~ you may not use this file except in compliance with the License.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2024-2024 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.devkit.utils;
19+
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Test;
23+
24+
@Slf4j
25+
public class TestAesUtil {
26+
27+
@Test
28+
public void testGenerateRandomSecret() {
29+
log.info("Secret is {}", AesUtil.generateRandomSecret());
30+
}
31+
32+
@Test
33+
public void testEncrypt() {
34+
var secret = "43f72073956d4c81";
35+
36+
Assertions.assertEquals("IbbYZu8GtMruBURfMBVy/w==", AesUtil.encrypt("Hello World", secret));
37+
Assertions.assertEquals("1eVA7oQpTIhI7jc+6cdkmg==", AesUtil.encrypt("OnixByte", secret));
38+
Assertions.assertEquals("fk6oNRJK8a+Pz7zVwtlD0UQocq5c3GkRuem0Z6jdAN8=", AesUtil.encrypt("Welcome to use JDevKit!", secret));
39+
Assertions.assertEquals("dqzGjawNcQdBpXJWk/08UQ==", AesUtil.encrypt("127.0.0.1", secret));
40+
Assertions.assertEquals("uwQQI60yAGL91q9jCDgoeA==", AesUtil.encrypt("root", secret));
41+
}
42+
43+
@Test
44+
public void testDecrypt() {
45+
var secret = "43f72073956d4c81";
46+
47+
Assertions.assertEquals("Hello World", AesUtil.decrypt("IbbYZu8GtMruBURfMBVy/w==", secret));
48+
Assertions.assertEquals("OnixByte", AesUtil.decrypt("1eVA7oQpTIhI7jc+6cdkmg==", secret));
49+
Assertions.assertEquals("Welcome to use JDevKit!", AesUtil.decrypt("fk6oNRJK8a+Pz7zVwtlD0UQocq5c3GkRuem0Z6jdAN8=", secret));
50+
Assertions.assertEquals("127.0.0.1", AesUtil.decrypt("dqzGjawNcQdBpXJWk/08UQ==", secret));
51+
Assertions.assertEquals("root", AesUtil.decrypt("uwQQI60yAGL91q9jCDgoeA==", secret));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)