Skip to content

Commit

Permalink
♻️ refactor: refactor codebase #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jun 16, 2024
1 parent eee2d15 commit b4d5959
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugin/src/main/groovy/org/unify4j/common/Auth4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions plugin/src/main/groovy/org/unify4j/common/Collection4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
* <p>
* 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 <KeyT> the type of keys maintained by the returned map
* @param <ValueT> 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 <KeyT, ValueT> Map<KeyT, ValueT> mapOf(Pair<KeyT, ValueT>... pairs) {
Map<KeyT, ValueT> map = new ConcurrentHashMap<>(pairs.length);
for (Pair<KeyT, ValueT> pair : pairs) {
map.put(pair.getKey(), pair.getValue());
}
return Collections.unmodifiableMap(map);
}

/**
* Throws UnsupportedOperationException if the list is not of type ArrayList.
*
Expand Down
30 changes: 30 additions & 0 deletions plugin/src/main/groovy/org/unify4j/model/c/Pair.java
Original file line number Diff line number Diff line change
@@ -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<K, V> 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 <K, V> Pair<K, V> of(K key, V value) {
return new Pair<>(key, value);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit b4d5959

Please sign in to comment.