BAEL-587 Guide to Bimap (#995)
* BAL-36 File size api in java and apache commons IO * BAEL-282 grep in java - fixes after code review * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor library * BAEL-519 Added support for disruptor * BAEL-519 Moved all supporting classes to main source * BAEL-519 Moved all supporting classes to main source * BAEL-519 Moved asserts and test classes in test folder. * BAEL-519 moved test related producer and consumer to src. * BAEL-587 Initial commit for BiMap support. * BAEL-587 Added new tests * BAEL-587 Added new tests. * BAEL-519 Removed Disruptor link from readme file. * BAEL-587 Reverted Disruptor changes * BAEL-587 reverted changes
This commit is contained in:
parent
666803cf60
commit
d5acd709e1
|
@ -358,7 +358,7 @@
|
|||
<logback.version>1.1.7</logback.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<bouncycastle.version>1.55</bouncycastle.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
package com.baeldung.guava;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.EnumHashBiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
|
||||
public class GuavaBiMapTest {
|
||||
@Test
|
||||
public void whenQueryByValue_shouldReturnKey() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = HashBiMap.create();
|
||||
personCountryHeadBiMap.put("Modi", "India");
|
||||
personCountryHeadBiMap.put("Obama", "USA");
|
||||
personCountryHeadBiMap.put("Putin", "USSR");
|
||||
|
||||
final String countryHeadName = personCountryHeadBiMap.inverse().get("India");
|
||||
|
||||
assertEquals("Modi", countryHeadName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateBiMapFromExistingMap_shouldReturnKey() {
|
||||
final Map<String, String> personCountryHeadMap = new HashMap<>();
|
||||
personCountryHeadMap.put("Modi", "India");
|
||||
personCountryHeadMap.put("Obama", "USA");
|
||||
personCountryHeadMap.put("Putin", "USSR");
|
||||
final BiMap<String, String> personCountryHeadBiMap = HashBiMap.create(personCountryHeadMap);
|
||||
|
||||
final String countryHeadName = personCountryHeadBiMap.inverse().get("India");
|
||||
|
||||
assertEquals("Modi", countryHeadName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenQueryByKey_shouldReturnValue() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = HashBiMap.create();
|
||||
|
||||
personCountryHeadBiMap.put("Modi", "India");
|
||||
personCountryHeadBiMap.put("Obama", "USA");
|
||||
personCountryHeadBiMap.put("Putin", "USSR");
|
||||
|
||||
assertEquals("USA", personCountryHeadBiMap.get("Obama"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenSameValueIsBeingPresent_shouldThrowException() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = HashBiMap.create();
|
||||
|
||||
personCountryHeadBiMap.put("Modi", "India");
|
||||
personCountryHeadBiMap.put("Obama", "USA");
|
||||
personCountryHeadBiMap.put("Putin", "USSR");
|
||||
personCountryHeadBiMap.put("Trump", "USA");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameValueIsBeingPresent_whenForcePutIsUsed_shouldCompleteSuccessfully() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = HashBiMap.create();
|
||||
|
||||
personCountryHeadBiMap.put("Modi", "India");
|
||||
personCountryHeadBiMap.put("Obama", "USA");
|
||||
personCountryHeadBiMap.put("Putin", "USSR");
|
||||
personCountryHeadBiMap.forcePut("Trump", "USA");
|
||||
|
||||
assertEquals("USA", personCountryHeadBiMap.get("Trump"));
|
||||
assertEquals("Trump", personCountryHeadBiMap.inverse().get("USA"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSameKeyIsBeingPresent_shouldReplaceAlreadyPresent() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = HashBiMap.create();
|
||||
|
||||
personCountryHeadBiMap.put("Modi", "India");
|
||||
personCountryHeadBiMap.put("Obama", "USA");
|
||||
personCountryHeadBiMap.put("Putin", "USSR");
|
||||
personCountryHeadBiMap.put("Obama", "HongKong");
|
||||
|
||||
assertEquals("HongKong", personCountryHeadBiMap.get("Obama"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingImmutableBiMap_shouldAllowPutSuccessfully() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = new ImmutableBiMap.Builder<String, String>().put("Modi", "India").put("Obama", "USA").put("Putin", "USSR").build();
|
||||
|
||||
assertEquals("USA", personCountryHeadBiMap.get("Obama"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenUsingImmutableBiMap_shouldNotAllowRemove() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = new ImmutableBiMap.Builder<String, String>().put("Modi", "India").put("Obama", "USA").put("Putin", "USSR").build();
|
||||
|
||||
personCountryHeadBiMap.remove("Modi");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenUsingImmutableBiMap_shouldNotAllowPut() {
|
||||
final BiMap<String, String> personCountryHeadBiMap = new ImmutableBiMap.Builder<String, String>().put("Modi", "India").put("Obama", "USA").put("Putin", "USSR").build();
|
||||
|
||||
personCountryHeadBiMap.put("Trump", "USA");
|
||||
}
|
||||
|
||||
private enum Operation {
|
||||
ADD, SUBTRACT, MULTIPLY, DIVIDE
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEnumAsKeyInMap_shouldReplaceAlreadyPresent() {
|
||||
final BiMap<Operation, String> operationStringBiMap = EnumHashBiMap.create(Operation.class);
|
||||
|
||||
operationStringBiMap.put(Operation.ADD, "Add");
|
||||
operationStringBiMap.put(Operation.SUBTRACT, "Subtract");
|
||||
operationStringBiMap.put(Operation.MULTIPLY, "Multiply");
|
||||
operationStringBiMap.put(Operation.DIVIDE, "Divide");
|
||||
|
||||
assertEquals("Divide", operationStringBiMap.get(Operation.DIVIDE));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue