public <V> V get(K id, TypeToken<V> type) { Key<K, V> key = Key.get(id, type); //noinspection unchecked return (V) map.get(key); }
public static <K, V> Key<K, V> get(K id, TypeToken<V> clazz) { return new Key<>(id, clazz); }
public <V> V get(K id, Class<V> clazz) { Key<K, V> key = Key.get(id, clazz); //noinspection unchecked return (V) map.get(key); }
public static <K, V> Key<K, V> get(K id, Class<V> clazz) { return new Key<>(id, clazz); }
public <V1, V2 extends V1> void put(K id, Class<V1> clazz, V2 instance) { Key<K, V1> key = Key.get(id, clazz); map.put(key, instance); }
public <V1, V2 extends V1> void put(K id, TypeToken<V1> clazz, V2 instance) { Key<K, V1> key = Key.get(id, clazz); map.put(key, instance); }
@Test(groups = "fast") public void testSuperType() throws Exception { Key<String, Object> objectKey = Key.get("x", Object.class); Key<String, Number> numberKey = Key.get("x", Number.class); Key<String, Float> floatKey = Key.get("x", Float.class); asssertSuperTestResults(objectKey, numberKey, floatKey); }
@Test(groups = "fast") public void testSuperUsingTypeToken() throws Exception { // version that use the type token directly Key<String, Object> objectKey = Key.get("x", TypeToken.of(Object.class)); Key<String, Number> numberKey = Key.get("x", TypeToken.of(Number.class)); Key<String, Float> floatKey = Key.get("x", TypeToken.of(Float.class)); asssertSuperTestResults(objectKey, numberKey, floatKey); }
@Test(groups = "fast") public void testTypeTokenAndClass() throws Exception { Key<String, Integer> classKey = Key.get("baz", Integer.class); Key<String, Integer> typeKey = Key.get("baz", TypeToken.of(Integer.class)); stringMap1.put(classKey, Integer.MAX_VALUE); Assert.assertEquals(stringMap1.get(typeKey).longValue(), Integer.MAX_VALUE); Assert.assertEquals(stringMap1.get(classKey).longValue(), Integer.MAX_VALUE); stringMap1.put(typeKey, Integer.MIN_VALUE); Assert.assertEquals(stringMap1.get(typeKey).longValue(), Integer.MIN_VALUE); Assert.assertEquals(stringMap1.get(classKey).longValue(), Integer.MIN_VALUE); }
@Test(groups = "fast") public void testGenerics() throws Exception { TypeToken<List<String>> stringTypeToken = new TypeToken<List<String>>() {}; List<String> stringList = Piles.<String>copyOf( ImmutableList.<String>builder() .add("x") .add("y") .add("z") .add("1") .build().iterator() ); stringMap1.put("fuu", stringTypeToken, stringList); Key myKey = Key.get("fuu", stringTypeToken); Assert.assertEquals(stringMap1.get(myKey), stringList); Assert.assertEquals(stringMap1.get("fuu", stringTypeToken), stringList); }
@BeforeMethod(alwaysRun = true) public void setUp() throws Exception { stringMap1 = new MixedTypeMap<>(); stringMap2 = new MixedTypeMap<>(); key1 = "fuu"; value2 = new Pair<>(200L, 800L); key2 = Key.get("fuu", Pair.class); key3 = Key.get("c-map", CounterMap.class); value3 = new CounterMap<>(); value3.addAndGet("a", 1); value3.addAndGet("b", 2); value3.addAndGet("c", 3); stringMap1.put(key1, Long.class, 100L); stringMap1.put(key2, value2); stringMap1.put(key3, value3); }
@Test(groups = "fast") public void testSuperType() throws Exception { Key<String, Object> objectKey = Key.get("x", Object.class); Key<String, Number> numberKey = Key.get("x", Number.class); Key<String, Float> floatKey = Key.get("x", Float.class); asssertSuperTestResults(objectKey, numberKey, floatKey); }
@Test(groups = "fast") public void testSuperUsingTypeToken() throws Exception { // version that use the type token directly Key<String, Object> objectKey = Key.get("x", TypeToken.of(Object.class)); Key<String, Number> numberKey = Key.get("x", TypeToken.of(Number.class)); Key<String, Float> floatKey = Key.get("x", TypeToken.of(Float.class)); asssertSuperTestResults(objectKey, numberKey, floatKey); }
@Test(groups = "fast") public void testTypeTokenAndClass() throws Exception { Key<String, Integer> classKey = Key.get("baz", Integer.class); Key<String, Integer> typeKey = Key.get("baz", TypeToken.of(Integer.class)); stringMap1.put(classKey, Integer.MAX_VALUE); Assert.assertEquals(stringMap1.get(typeKey).longValue(), Integer.MAX_VALUE); Assert.assertEquals(stringMap1.get(classKey).longValue(), Integer.MAX_VALUE); stringMap1.put(typeKey, Integer.MIN_VALUE); Assert.assertEquals(stringMap1.get(typeKey).longValue(), Integer.MIN_VALUE); Assert.assertEquals(stringMap1.get(classKey).longValue(), Integer.MIN_VALUE); }
@Test(groups = "fast") public void testGenerics() throws Exception { TypeToken<List<String>> stringTypeToken = new TypeToken<List<String>>() {}; List<String> stringList = Piles.<String>copyOf( ImmutableList.<String>builder() .add("x") .add("y") .add("z") .add("1") .build().iterator() ); stringMap1.put("fuu", stringTypeToken, stringList); Key myKey = Key.get("fuu", stringTypeToken); Assert.assertEquals(stringMap1.get(myKey), stringList); Assert.assertEquals(stringMap1.get("fuu", stringTypeToken), stringList); }
@BeforeMethod(alwaysRun = true) public void setUp() throws Exception { stringMap1 = new MixedTypeMap<>(); stringMap2 = new MixedTypeMap<>(); key1 = "fuu"; value2 = new Pair<>(200L, 800L); key2 = Key.get("fuu", Pair.class); key3 = Key.get("c-map", CounterMap.class); value3 = new CounterMap<>(); value3.addAndGet("a", 1); value3.addAndGet("b", 2); value3.addAndGet("c", 3); stringMap1.put(key1, Long.class, 100L); stringMap1.put(key2, value2); stringMap1.put(key3, value3); }