BAEL-642 ClassToInstanceMap example (#1767)

* BAEL-642 ClassToInstanceMap example added

* BAEL-642 Improved example for ClassToInstanceMap

* Reverted change in pom.xml

* BAEL-642 Move the ClassToInstanceMap examples to guava module as unit tests
This commit is contained in:
Mateusz Mrozewski 2017-05-04 00:47:34 +04:00 committed by Zeger Hendrikse
parent 05807aebed
commit a623ddbf45
2 changed files with 71 additions and 1 deletions

View File

@ -0,0 +1,70 @@
package org.baeldung.guava;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.common.collect.MutableClassToInstanceMap;
import org.junit.Test;
import static org.junit.Assert.*;
public class ClassToInstanceMapTests {
@Test
public void createEmptyImmutableMap() {
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.of();
assertTrue(map.isEmpty());
}
@Test
public void createEmptyMutableMap() {
ClassToInstanceMap<Action> map = MutableClassToInstanceMap.create();
assertTrue(map.isEmpty());
}
@Test
public void createSingleEntryMap() {
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.of(Save.class, new Save());
assertEquals(1, map.size());
}
@Test
public void createMapWithBuilder() {
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.<Action>builder()
.put(Save.class, new Save())
.put(Open.class, new Open())
.build();
assertEquals(2, map.size());
}
@Test
public void shouldReturnElement() {
ClassToInstanceMap<Action> map = ImmutableClassToInstanceMap.of(Save.class, new Save());
Action action = map.get(Save.class);
assertTrue(action instanceof Save);
// Use getInstance to avoid casting
Save save = map.getInstance(Save.class);
}
@Test
public void shouldPutElement() {
ClassToInstanceMap<Action> map = MutableClassToInstanceMap.create();
map.put(Save.class, new Save());
// Put again to get previous value returned
Action action = map.put(Save.class, new Save());
assertTrue(action instanceof Save);
// Use putInstance to avoid casting
Save save = map.putInstance(Save.class, new Save());
}
}
abstract class Action {
}
class Save extends Action {
}
class Open extends Action {
}
class Delete extends Action {
}

View File

@ -9,7 +9,7 @@ import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
public class GauavaStreamsTests {
public class GuavaStreamsTests {
List<Integer> numbers;