-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TwoWayMap; Add ArrayUtil.contains()
- Loading branch information
Showing
5 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core/src/test/java/org/jsonex/core/util/TwoWayMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |