From 8f6dfce4b361123a5bfc0d69b21c19c9152c097c Mon Sep 17 00:00:00 2001 From: zihluwang Date: Thu, 26 Dec 2024 20:14:09 +0800 Subject: [PATCH 1/9] docs: updated README file --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 3003fda..5d240f5 100644 --- a/LICENSE +++ b/LICENSE @@ -187,7 +187,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2023 CodeCrafters (CN) + Copyright 2023-2024 OnixByte (HK) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 2d3a0745f525ef71ff83b7a0359ed60b2a1e18fb Mon Sep 17 00:00:00 2001 From: zihluwang Date: Thu, 26 Dec 2024 20:15:38 +0800 Subject: [PATCH 2/9] feat: implement map-object conversion with a safer method If you still want to use the unsafe version, please use com.onixbyte:map-util-unsafe. --- .../com/onixbyte/devkit/utils/MapUtil.java | 70 +++---------------- .../devkit/utils/ObjectMapAdapter.java | 43 +++--------- 2 files changed, 21 insertions(+), 92 deletions(-) diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java index 195020c..a81e8cf 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java +++ b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/MapUtil.java @@ -31,7 +31,7 @@ import java.util.Optional; * Please see documentation for more information. * * @author zihluwang - * @version 1.4.2 + * @version 1.7.0 * @since 1.0.0 */ @Slf4j @@ -40,16 +40,13 @@ public final class MapUtil { /** * Converts an object to a map by mapping the field names to their corresponding values. * - * @param the type of the object - * @param entity the object to be converted to a map - * @param adapters adapts the entity for mapping to a map + * @param the type of the object + * @param entity the object to be converted to a map + * @param adapter adapts the entity for mapping to a map * @return a map representing the fields and their values of the object */ - public static Map objectToMap(T entity, - Map> adapters) { - var resultMap = new HashMap(); - adapters.forEach((fieldName, adapter) -> resultMap.put(fieldName, adapter.fetch(entity))); - return resultMap; + public static Map objectToMap(T entity, ObjectMapAdapter adapter) { + return adapter.toMap(entity); } /** @@ -57,64 +54,17 @@ public final class MapUtil { * map entries. * * @param objectMap the map representing the fields and their values - * @param entity an empty entity of the target class - * @param adapters the adapters to execute the setter for the entity + * @param adapter the adapter to execute the setter for the entity * @param the type of the object to be created * @return an object of the specified type with the field values set from the map */ - public static T mapToObject(Map objectMap, - T entity, - Map> adapters) { - adapters.forEach((fieldName, adapter) -> Optional.ofNullable(objectMap) - .map((data) -> data.get(fieldName)) - .ifPresent((fieldValue) -> adapter.setValue(entity, fieldValue))); - return entity; + public static T mapToObject(Map objectMap, ObjectMapAdapter adapter) { + return adapter.toObject(objectMap); } /** - * Retrieves the value of a field from an object using reflection. - * - * @param the type of the entity - * @param the type of the field value - * @param entity the object from which to retrieve the field value - * @param adapter the adapter to execute the getter - * @return the value of the field in the object, or null if the field does not exist or cannot - * be accessed + * Private constructor prevent class being instantiated. */ - public static T getFieldValue(E entity, ObjectMapAdapter adapter) { - return adapter.fetch(entity); - } - - /** - * Sets the value of a field in an object using reflection. - * - * @param the type of the entity - * @param the type of the field value - * @param entity the object in which to set the field value - * @param adapter the adapter to execute the setter - * @param fieldValue the value to be set - */ - public static void setFieldValue(E entity, - ObjectMapAdapter adapter, - Object fieldValue) { - adapter.setValue(entity, fieldValue); - } - - /** - * Casts the specified value to the required type with Optional. - * - * @param value the value to be cast - * @param requiredType the type to which the value should be cast - * @param the type to which the value should be cast - * @return the cast value, or {@code null} if the value is not an instance of the requiredType - */ - public static T cast(Object value, Class requiredType) { - return Optional.ofNullable(requiredType) - .filter((clazz) -> clazz.isInstance(value)) - .map((clazz) -> clazz.cast(value)) - .orElse(null); - } - private MapUtil() { } } diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java index 863aa60..49d51b1 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java +++ b/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ObjectMapAdapter.java @@ -17,53 +17,32 @@ package com.onixbyte.devkit.utils; -import java.util.function.BiConsumer; -import java.util.function.Function; +import java.util.Map; /** * Adapts an Object to a Map, making conversion between Map and Object much more safe. * - * @param entity type * @param field type * @author zihluwang - * @version 1.4.2 + * @version 1.7.0 * @since 1.4.2 */ -public class ObjectMapAdapter { - - private final Function getter; - - private final BiConsumer setter; +public interface ObjectMapAdapter { /** - * Create an adapter. + * Convert an object to a map. * - * @param getter the getter of the field - * @param setter the setter of the field + * @param element the element that will be converted to Map + * @return a Map that is converted from the element */ - public ObjectMapAdapter(Function getter, BiConsumer setter) { - this.getter = getter; - this.setter = setter; - } + Map toMap(T element); /** - * Get data from the entity. + * Convert a Map to an object. * - * @param entity the source of the data - * @return the data + * @param map the map that will be converted to Object + * @return the object that is converted from the Map */ - public T fetch(E entity) { - return getter.apply(entity); - } - - /** - * Set value to the entity. - * - * @param entity the target of the data - * @param value the value - */ - public void setValue(E entity, Object value) { - setter.accept(entity, value); - } + T toObject(Map map); } From ade73ae8e3fd77816ed78fedb2af8e38c0e6781c Mon Sep 17 00:00:00 2001 From: zihluwang Date: Thu, 26 Dec 2024 20:16:00 +0800 Subject: [PATCH 3/9] style: reformatted javadocs --- .../exceptions/NotImplementedException.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/devkit-core/src/main/java/com/onixbyte/devkit/core/exceptions/NotImplementedException.java b/devkit-core/src/main/java/com/onixbyte/devkit/core/exceptions/NotImplementedException.java index 3d6a060..3c88b7c 100644 --- a/devkit-core/src/main/java/com/onixbyte/devkit/core/exceptions/NotImplementedException.java +++ b/devkit-core/src/main/java/com/onixbyte/devkit/core/exceptions/NotImplementedException.java @@ -18,14 +18,12 @@ package com.onixbyte.devkit.core.exceptions; /** - * The {@code NotImplementedException} class is a custom runtime exception - * that represents a situation where a particular method or functionality is - * not implemented or is currently unavailable in the codebase. + * The {@code NotImplementedException} class is a custom runtime exception that represents a situation where a + * particular method or functionality is not implemented or is currently unavailable in the codebase. *

- * This exception is typically thrown when developers need to indicate that a - * specific part of the code is incomplete or requires further implementation. - * It serves as a placeholder to highlight unfinished sections of the - * application during development and testing phases. + * This exception is typically thrown when developers need to indicate that a specific part of the code is incomplete + * or requires further implementation. It serves as a placeholder to highlight unfinished sections of the application + * during development and testing phases. *

* Usage Example: *

@@ -54,15 +52,13 @@ package com.onixbyte.devkit.core.exceptions;
 public class NotImplementedException extends RuntimeException {
 
     /**
-     * Creates a new {@code NotImplementedException} with no specific error
-     * message.
+     * Creates a new {@code NotImplementedException} with no specific error message.
      */
     public NotImplementedException() {
     }
 
     /**
-     * Creates a new {@code NotImplementedException} with the provided error
-     * message.
+     * Creates a new {@code NotImplementedException} with the provided error message.
      *
      * @param message the error message associated with this exception
      */
@@ -71,8 +67,7 @@ public class NotImplementedException extends RuntimeException {
     }
 
     /**
-     * Creates a new {@code NotImplementedException} with the specified error
-     * message and a cause for this exception.
+     * Creates a new {@code NotImplementedException} with the specified error message and a cause for this exception.
      *
      * @param message the error message associated with this exception
      * @param cause   the cause of this exception
@@ -91,8 +86,8 @@ public class NotImplementedException extends RuntimeException {
     }
 
     /**
-     * Creates a new {@code NotImplementedException} with the specified error
-     * message, cause, suppression flag, and stack trace writable flag.
+     * Creates a new {@code NotImplementedException} with the specified error message, cause, suppression flag, and
+     * stack trace writable flag.
      *
      * @param message            the error message associated with this
      *                           exception

From 7e01574472e1376bd1d52224c08820f73469dd83 Mon Sep 17 00:00:00 2001
From: zihluwang 
Date: Thu, 26 Dec 2024 22:00:25 +0800
Subject: [PATCH 4/9] feat: added logback configuration for local test uses

---
 .../src/main/resources/logback.xml            | 32 +++++++++++++++++++
 num4j/src/main/resources/logback.xml          | 32 +++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 map-util-unsafe/src/main/resources/logback.xml
 create mode 100644 num4j/src/main/resources/logback.xml

diff --git a/map-util-unsafe/src/main/resources/logback.xml b/map-util-unsafe/src/main/resources/logback.xml
new file mode 100644
index 0000000..1cf5a50
--- /dev/null
+++ b/map-util-unsafe/src/main/resources/logback.xml
@@ -0,0 +1,32 @@
+
+
+
+
+    
+    
+
+    
+    
+        
+            ${COLOURFUL_OUTPUT}
+        
+    
+    
+        
+    
+
\ No newline at end of file
diff --git a/num4j/src/main/resources/logback.xml b/num4j/src/main/resources/logback.xml
new file mode 100644
index 0000000..1cf5a50
--- /dev/null
+++ b/num4j/src/main/resources/logback.xml
@@ -0,0 +1,32 @@
+
+
+
+
+    
+    
+
+    
+    
+        
+            ${COLOURFUL_OUTPUT}
+        
+    
+    
+        
+    
+
\ No newline at end of file

From f587230072313ac86ebd8bb033bad55ccf88e510 Mon Sep 17 00:00:00 2001
From: zihluwang 
Date: Thu, 26 Dec 2024 22:22:51 +0800
Subject: [PATCH 5/9] style: updated copyright

---
 devkit-core/src/main/resources/logback.xml                      | 2 +-
 devkit-utils/src/main/resources/logback.xml                     | 2 +-
 guid/src/main/resources/logback.xml                             | 2 +-
 .../src/main/resources/logback.xml                              | 2 +-
 simple-jwt-authzero/src/main/resources/logback.xml              | 2 +-
 simple-jwt-facade/src/main/resources/logback.xml                | 2 +-
 simple-jwt-spring-boot-starter/src/main/resources/logback.xml   | 2 +-
 7 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/devkit-core/src/main/resources/logback.xml b/devkit-core/src/main/resources/logback.xml
index f229a7e..6d26440 100644
--- a/devkit-core/src/main/resources/logback.xml
+++ b/devkit-core/src/main/resources/logback.xml
@@ -1,6 +1,6 @@