diff --git a/core/src/main/java/org/jsonex/core/util/ArrayUtil.java b/core/src/main/java/org/jsonex/core/util/ArrayUtil.java index 10d27a5..6e690c9 100644 --- a/core/src/main/java/org/jsonex/core/util/ArrayUtil.java +++ b/core/src/main/java/org/jsonex/core/util/ArrayUtil.java @@ -19,6 +19,10 @@ public static int indexOf(@Nullable T[] array, T e) { return -1; } + public static boolean contains(@Nullable T[] array, T e) { + return indexOf(array, e) >= 0; + } + public static TDest[] map(@Nullable TSrc[] source, Function func, TDest[] dest) { return mapWithIndex(source, (s, idx) -> func.apply(s), dest); } @@ -77,5 +81,4 @@ public static int indexOf(V[] source, Predicate pred) { return i; return -1; } - } diff --git a/core/src/main/java/org/jsonex/core/util/MapBuilder.java b/core/src/main/java/org/jsonex/core/util/MapBuilder.java index bf85464..ab4d6bd 100644 --- a/core/src/main/java/org/jsonex/core/util/MapBuilder.java +++ b/core/src/main/java/org/jsonex/core/util/MapBuilder.java @@ -28,7 +28,11 @@ public MapBuilder() {} public MapBuilder(K key, V val) { put(key, val); } public Map build() { return map; } + @Deprecated // Use of instead public static MapBuilder mapOf(K key, V val) { return new MapBuilder<>(key, val); } + public static MapBuilder of(K key, V val) { + return new MapBuilder<>(key, val); + } } \ No newline at end of file diff --git a/core/src/main/java/org/jsonex/core/util/TwoWayMap.java b/core/src/main/java/org/jsonex/core/util/TwoWayMap.java new file mode 100644 index 0000000..0a68f22 --- /dev/null +++ b/core/src/main/java/org/jsonex/core/util/TwoWayMap.java @@ -0,0 +1,28 @@ +package org.jsonex.core.util; + +import lombok.Setter; + +import java.util.HashMap; +import java.util.Map; + +/** + * A two-way map which allow map values from key to value or reverse. This class provides default value if no match found + * and support chained put method. + */ +public class TwoWayMap { + private final Map map = new HashMap<>(); + private final Map reverseMap = new HashMap<>(); + @Setter private K defaultKey; + @Setter private V defaultValue; + + public static TwoWayMap of(K key, V value) { return new TwoWayMap().put(key, value); } + + public TwoWayMap put(K key, V value) { + map.put(key, value); + reverseMap.put(value, key); + return this; + } + + public V get(K key) { return key == null ? null : map.getOrDefault(key, defaultValue); } + public K getKey(V value) { return value == null ? null : reverseMap.getOrDefault(value, defaultKey); } +} \ No newline at end of file diff --git a/core/src/test/java/org/jsonex/core/util/ArrayUtilTest.java b/core/src/test/java/org/jsonex/core/util/ArrayUtilTest.java index d8f64e5..94af658 100644 --- a/core/src/test/java/org/jsonex/core/util/ArrayUtilTest.java +++ b/core/src/test/java/org/jsonex/core/util/ArrayUtilTest.java @@ -8,8 +8,7 @@ import static org.jsonex.core.type.Operator.eq; import static org.jsonex.core.util.ListUtilTest.TestCls.F_TYPE; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; public class ArrayUtilTest { private TestCls[] buildArray() { @@ -43,5 +42,6 @@ private TestCls[] buildArray() { assertEquals(Optional.empty(), ArrayUtil.first(list, eq(F_TYPE, 3))); assertEquals(Optional.empty(), ArrayUtil.first(null, eq(F_TYPE, 3))); assertEquals(-1, ArrayUtil.indexOf(null, eq(F_TYPE, 2))); + assertTrue(ArrayUtil.contains(list, list[2])); } } diff --git a/core/src/test/java/org/jsonex/core/util/TwoWayMapTest.java b/core/src/test/java/org/jsonex/core/util/TwoWayMapTest.java new file mode 100644 index 0000000..432c970 --- /dev/null +++ b/core/src/test/java/org/jsonex/core/util/TwoWayMapTest.java @@ -0,0 +1,17 @@ +package org.jsonex.core.util; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class TwoWayMapTest { + @Test public void testGet() { + TwoWayMap map = TwoWayMap.of("a", "A").put("b", "B").setDefaultKey("+").setDefaultValue("-"); + assertEquals("A", map.get("a")); + assertEquals("b", map.getKey("B")); + assertNull(map.getKey(null)); + assertEquals("+", map.getKey("C")); + assertEquals("-", map.get("c")); + } +}