@Override protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) { return populate(new EnumMap<AnEnum, String>(AnEnum.class), entries); } })
@SuppressWarnings("unchecked") @Override <K extends K0, V> Map<K, Collection<V>> createMap() { // K must actually be K0, since enums are effectively final // (their subclasses are inaccessible) return (Map<K, Collection<V>>) new EnumMap<K0, Collection<V>>(keyClass); } };
/** * Creates an {@code EnumMap} with the same mappings as the specified map. * * <p><b>Note for Java 7 and later:</b> this method is now unnecessary and should be treated as * deprecated. Instead, use the {@code EnumMap} constructor directly, taking advantage of the new * <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>. * * @param map the map from which to initialize this {@code EnumMap} * @return a new {@code EnumMap} initialized with the mappings from {@code map} * @throws IllegalArgumentException if {@code m} is not an {@code EnumMap} instance and contains * no mappings */ public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap(Map<K, ? extends V> map) { return new EnumMap<>(map); }
/** * Creates an {@code EnumMap} instance. * * @param type the key type for this map * @return a new, empty {@code EnumMap} */ public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap(Class<K> type) { return new EnumMap<>(checkNotNull(type)); }
@Override protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) { EnumMap<AnEnum, String> map = new EnumMap<>(AnEnum.class); for (Entry<AnEnum, String> entry : entries) { map.put(entry.getKey(), entry.getValue()); } return ImmutableMap.copyOf(map); }
private EnumHashBiMap(Class<K> keyType) { super( WellBehavedMap.wrap(new EnumMap<K, V>(keyType)), Maps.<V, K>newHashMapWithExpectedSize(keyType.getEnumConstants().length)); this.keyType = keyType; }
void put(K key, V value) { if (map == null) { map = new EnumMap<>(key.getDeclaringClass()); } map.merge(key, value, mergeFunction); }
private static <K extends Enum<K>, V> ImmutableMap<K, V> copyOfEnumMap( EnumMap<K, ? extends V> original) { EnumMap<K, V> copy = new EnumMap<>(original); for (Entry<?, ?> entry : copy.entrySet()) { checkEntryNotNull(entry.getKey(), entry.getValue()); } return ImmutableEnumMap.asImmutable(copy); }
public JodaTimeFormatterRegistrar() { this.factories = new EnumMap<>(Type.class); for (Type type : Type.values()) { this.factories.put(type, new DateTimeFormatterFactory()); } }
@SuppressWarnings("unchecked") // reading fields populated by writeObject @GwtIncompatible // java.io.ObjectInputStream private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); keyType = (Class<K>) stream.readObject(); valueType = (Class<V>) stream.readObject(); setDelegates( WellBehavedMap.wrap(new EnumMap<K, V>(keyType)), WellBehavedMap.wrap(new EnumMap<V, K>(valueType))); Serialization.populateMap(this, stream); }
@SuppressWarnings("unchecked") // reading field populated by writeObject @GwtIncompatible // java.io.ObjectInputStream private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); keyType = (Class<K>) stream.readObject(); setDelegates( WellBehavedMap.wrap(new EnumMap<K, V>(keyType)), new HashMap<V, K>(keyType.getEnumConstants().length * 3 / 2)); Serialization.populateMap(this, stream); }
public FontMap() { super(FontStyle.class); // create the enum map for each FontSize for (FontStyle style : FontStyle.values()) { put(style, new EnumMap<FontSize, T>(FontSize.class)); } } }
public void testCopyOfEnumMap() { EnumMap<AnEnum, String> map = new EnumMap<>(AnEnum.class); map.put(AnEnum.B, "foo"); map.put(AnEnum.C, "bar"); assertTrue(ImmutableMap.copyOf(map) instanceof ImmutableEnumMap); }
@Test public void createApproximateMapFromNonEmptyEnumMap() { EnumMap<Color, String> enumMap = new EnumMap<>(Color.class); enumMap.put(Color.BLUE, "blue"); Map<Color, String> colors = createApproximateMap(enumMap, 2); assertThat(colors.size(), is(0)); }
public void testEntry_setValue() { WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(new EnumMap<Foo, Integer>(Foo.class)); map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3)); for (Entry<Foo, Integer> entry : map.entrySet()) { entry.setValue(entry.getValue() + 5); } assertEquals(ImmutableMap.of(Foo.X, 6, Foo.Y, 7, Foo.Z, 8), map); }
@Test public void createApproximateMapFromEmptyEnumMap() { Map<Color, String> colors = createApproximateMap(new EnumMap<Color, String>(Color.class), 2); assertThat(colors.size(), is(0)); }
@Test public void testStringToEnumMap() throws Exception { conversionService.addConverterFactory(new StringToEnumConverterFactory()); Map<String, Integer> source = new HashMap<>(); source.put("A", 1); source.put("C", 2); EnumMap<MyEnum, Integer> result = new EnumMap<>(MyEnum.class); result.put(MyEnum.A, 1); result.put(MyEnum.C, 2); assertEquals(result, conversionService.convert(source, TypeDescriptor.forObject(source), new TypeDescriptor(getClass().getField("enumMap")))); }
public void testEntrySet_contain() { WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(new EnumMap<Foo, Integer>(Foo.class)); map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3)); // testing with the exact entry assertTrue(map.entrySet().contains(Maps.immutableEntry(Foo.X, 1))); assertTrue(map.entrySet().contains(Maps.immutableEntry(Foo.Y, new Integer(2)))); // testing an entry with a contained key, but not the same value assertFalse(map.entrySet().contains(Maps.immutableEntry(Foo.X, 5))); // testing a non-existent key assertFalse(map.entrySet().contains(Maps.immutableEntry(Foo.T, 0))); }
public void testEntrySet_remove() { WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(new EnumMap<Foo, Integer>(Foo.class)); map.putAll(ImmutableMap.of(Foo.X, 1, Foo.Y, 2, Foo.Z, 3)); Set<Entry<Foo, Integer>> entrySet = map.entrySet(); // removing an existing entry, verifying consistency Entry<Foo, Integer> entry = Maps.immutableEntry(Foo.Y, 2); assertTrue(entrySet.remove(entry)); assertFalse(map.containsKey(Foo.Y)); assertNull(map.get(Foo.Y)); assertFalse(entrySet.contains(entry)); // we didn't have that entry, not removed assertFalse(entrySet.remove(Maps.immutableEntry(Foo.T, 4))); // we didn't have that entry, only <Z, 3>, must not remove assertFalse(entrySet.remove(Maps.immutableEntry(Foo.Z, 5))); } }