Skip to content

Commit

Permalink
Add TwoWayMap; Add ArrayUtil.contains()
Browse files Browse the repository at this point in the history
  • Loading branch information
jianwu committed Sep 7, 2022
1 parent 0a6fa12 commit bb7db42
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
5 changes: 4 additions & 1 deletion core/src/main/java/org/jsonex/core/util/ArrayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public static <T> int indexOf(@Nullable T[] array, T e) {
return -1;
}

public static <T> boolean contains(@Nullable T[] array, T e) {
return indexOf(array, e) >= 0;
}

public static <TSrc, TDest> TDest[] map(@Nullable TSrc[] source, Function<? super TSrc, ? extends TDest> func, TDest[] dest) {
return mapWithIndex(source, (s, idx) -> func.apply(s), dest);
}
Expand Down Expand Up @@ -77,5 +81,4 @@ public static <V> int indexOf(V[] source, Predicate<? super V> pred) {
return i;
return -1;
}

}
4 changes: 4 additions & 0 deletions core/src/main/java/org/jsonex/core/util/MapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public MapBuilder() {}
public MapBuilder(K key, V val) { put(key, val); }
public Map<K, V> build() { return map; }

@Deprecated // Use of instead
public static <K, V> MapBuilder<K, V> mapOf(K key, V val) {
return new MapBuilder<>(key, val);
}
public static <K, V> MapBuilder<K, V> of(K key, V val) {
return new MapBuilder<>(key, val);
}
}
28 changes: 28 additions & 0 deletions core/src/main/java/org/jsonex/core/util/TwoWayMap.java
Original file line number Diff line number Diff line change
@@ -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<K, V> {
private final Map<K, V> map = new HashMap<>();
private final Map<V, K> reverseMap = new HashMap<>();
@Setter private K defaultKey;
@Setter private V defaultValue;

public static <K, V> TwoWayMap<K, V> of(K key, V value) { return new TwoWayMap<K, V>().put(key, value); }

public TwoWayMap<K, V> 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); }
}
4 changes: 2 additions & 2 deletions core/src/test/java/org/jsonex/core/util/ArrayUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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]));
}
}
17 changes: 17 additions & 0 deletions core/src/test/java/org/jsonex/core/util/TwoWayMapTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit bb7db42

Please sign in to comment.