From b4d59597b2babf22525b32ec6f27075779f52e6f Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sun, 16 Jun 2024 20:21:01 +0700 Subject: [PATCH] :recycle: refactor: refactor codebase #3 --- .../groovy/org/unify4j/common/Auth4j.java | 2 +- .../org/unify4j/common/Collection4j.java | 25 ++++++++++++++++ .../main/groovy/org/unify4j/model/c/Pair.java | 30 +++++++++++++++++++ .../{onlyrd => rd}/AbstractAuthClass.java | 2 +- 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 plugin/src/main/groovy/org/unify4j/model/c/Pair.java rename plugin/src/main/groovy/org/unify4j/model/{onlyrd => rd}/AbstractAuthClass.java (98%) diff --git a/plugin/src/main/groovy/org/unify4j/common/Auth4j.java b/plugin/src/main/groovy/org/unify4j/common/Auth4j.java index bdbadb6..cd07098 100644 --- a/plugin/src/main/groovy/org/unify4j/common/Auth4j.java +++ b/plugin/src/main/groovy/org/unify4j/common/Auth4j.java @@ -3,7 +3,7 @@ import org.unify4j.model.builder.HttpWrapBuilder; import org.unify4j.model.c.HttpHeaders; import org.unify4j.model.enums.AuthType; -import org.unify4j.model.onlyrd.AbstractAuthClass; +import org.unify4j.model.rd.AbstractAuthClass; import org.unify4j.model.response.WrapResponse; import java.util.Base64; diff --git a/plugin/src/main/groovy/org/unify4j/common/Collection4j.java b/plugin/src/main/groovy/org/unify4j/common/Collection4j.java index 22a5076..9ffa6cb 100644 --- a/plugin/src/main/groovy/org/unify4j/common/Collection4j.java +++ b/plugin/src/main/groovy/org/unify4j/common/Collection4j.java @@ -2,9 +2,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.unify4j.model.c.Pair; import java.lang.reflect.Array; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -649,6 +651,29 @@ public static String toString(Collection collections, String delimiter) { return builder.substring(delimiter.length()); } + /** + * Creates an unmodifiable map from an array of Pair objects. + *

+ * This method takes a variable number of Pair objects and constructs + * a thread-safe map (using ConcurrentHashMap) from these pairs. The + * resulting map is then wrapped with Collections.unmodifiableMap to + * ensure it cannot be modified after creation. + * + * @param the type of keys maintained by the returned map + * @param the type of mapped values + * @param pairs an array of Pair objects from which the map will be created + * @return an unmodifiable map containing the key-value pairs from the pairs array + * @throws NullPointerException if any of the keys or values in the pairs are null + */ + @SafeVarargs + public static Map mapOf(Pair... pairs) { + Map map = new ConcurrentHashMap<>(pairs.length); + for (Pair pair : pairs) { + map.put(pair.getKey(), pair.getValue()); + } + return Collections.unmodifiableMap(map); + } + /** * Throws UnsupportedOperationException if the list is not of type ArrayList. * diff --git a/plugin/src/main/groovy/org/unify4j/model/c/Pair.java b/plugin/src/main/groovy/org/unify4j/model/c/Pair.java new file mode 100644 index 0000000..e65eee8 --- /dev/null +++ b/plugin/src/main/groovy/org/unify4j/model/c/Pair.java @@ -0,0 +1,30 @@ +package org.unify4j.model.c; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import java.io.Serializable; + +@SuppressWarnings({"ClassCanBeRecord"}) +@JsonIgnoreProperties(ignoreUnknown = true) +public class Pair implements Serializable { + public Pair(K key, V value) { + super(); + this.key = key; + this.value = value; + } + + private final K key; + private final V value; + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + public static Pair of(K key, V value) { + return new Pair<>(key, value); + } +} \ No newline at end of file diff --git a/plugin/src/main/groovy/org/unify4j/model/onlyrd/AbstractAuthClass.java b/plugin/src/main/groovy/org/unify4j/model/rd/AbstractAuthClass.java similarity index 98% rename from plugin/src/main/groovy/org/unify4j/model/onlyrd/AbstractAuthClass.java rename to plugin/src/main/groovy/org/unify4j/model/rd/AbstractAuthClass.java index 8407a25..9ef4bc6 100644 --- a/plugin/src/main/groovy/org/unify4j/model/onlyrd/AbstractAuthClass.java +++ b/plugin/src/main/groovy/org/unify4j/model/rd/AbstractAuthClass.java @@ -1,4 +1,4 @@ -package org.unify4j.model.onlyrd; +package org.unify4j.model.rd; import org.unify4j.model.enums.AuthType; import org.unify4j.model.response.WrapResponse;