Refine search
@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); }
/** * Wraps the given map into a {@code WellBehavedEntriesMap}, which intercepts its {@code * entrySet()} method by taking the {@code Set<K> keySet()} and transforming it to {@code * Set<Entry<K, V>>}. All other invocations are delegated as-is. */ static <K, V> WellBehavedMap<K, V> wrap(Map<K, V> delegate) { return new WellBehavedMap<>(delegate); }
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))); } }
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); }
/** * @serialData the {@code Class<E>} for the enum type, the number of distinct * elements, the first element, its count, the second element, its * count, and so on */ @GwtIncompatible("java.io.ObjectInputStream") private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); @SuppressWarnings("unchecked") // reading data stored by writeObject Class<E> localType = (Class<E>) stream.readObject(); type = localType; setBackingMap(WellBehavedMap.wrap(new EnumMap<E, Count>(type))); Serialization.populateMultiset(this, stream); }
/** * @serialData the {@code Class<E>} for the enum type, the number of distinct * elements, the first element, its count, the second element, its * count, and so on */ @GwtIncompatible("java.io.ObjectInputStream") private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); @SuppressWarnings("unchecked") // reading data stored by writeObject Class<E> localType = (Class<E>) stream.readObject(); type = localType; setBackingMap(WellBehavedMap.wrap(new EnumMap<E, Count>(type))); Serialization.populateMultiset(this, stream); }
private EnumHashBiMap(Class<K> keyType) { super( WellBehavedMap.wrap(new EnumMap<K, V>(keyType)), Maps.<V, K>newHashMapWithExpectedSize(keyType.getEnumConstants().length)); this.keyType = keyType; }
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))); }
@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); }
/** * @serialData the {@code Class<E>} for the enum type, the number of distinct * elements, the first element, its count, the second element, its * count, and so on */ @GwtIncompatible("java.io.ObjectInputStream") private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); @SuppressWarnings("unchecked") // reading data stored by writeObject Class<E> localType = (Class<E>) stream.readObject(); type = localType; setBackingMap(WellBehavedMap.wrap(new EnumMap<E, Count>(type))); Serialization.populateMultiset(this, stream); }
/** * Wraps the given map into a {@code WellBehavedEntriesMap}, which intercepts its {@code * entrySet()} method by taking the {@code Set<K> keySet()} and transforming it to {@code * Set<Entry<K, V>>}. All other invocations are delegated as-is. */ static <K, V> WellBehavedMap<K, V> wrap(Map<K, V> delegate) { return new WellBehavedMap<>(delegate); }
public void testEntriesAreMutableAndConsistent() { WellBehavedMap<Foo, Integer> map = WellBehavedMap.wrap(new EnumMap<Foo, Integer>(Foo.class)); map.putAll(ImmutableMap.of(Foo.X, 1)); Entry<Foo, Integer> entry1 = Iterables.getOnlyElement(map.entrySet()); Entry<Foo, Integer> entry2 = Iterables.getOnlyElement(map.entrySet()); // the entries are constructed and forgotten, thus different assertNotSame(entry1, entry2); Set<Entry<Foo, Integer>> entrySet = map.entrySet(); assertTrue(entrySet.contains(entry1)); assertTrue(entrySet.contains(entry2)); // mutating entry entry1.setValue(2); // entry2 is also modified assertEquals(entry1.getValue(), entry2.getValue()); // and both are still contained in the set assertTrue(entrySet.contains(entry1)); assertTrue(entrySet.contains(entry2)); }
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<Map.Entry<Foo, Integer>> entrySet = map.entrySet(); // removing an existing entry, verifying consistency Map.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))); } }
@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); }
/** * @serialData the {@code Class<E>} for the enum type, the number of distinct * elements, the first element, its count, the second element, its * count, and so on */ @GwtIncompatible("java.io.ObjectInputStream") private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); @SuppressWarnings("unchecked") // reading data stored by writeObject Class<E> localType = (Class<E>) stream.readObject(); type = localType; setBackingMap(WellBehavedMap.wrap(new EnumMap<E, Count>(type))); Serialization.populateMultiset(this, stream); }
/** * Wraps the given map into a {@code WellBehavedEntriesMap}, which intercepts its {@code * entrySet()} method by taking the {@code Set<K> keySet()} and transforming it to {@code * Set<Entry<K, V>>}. All other invocations are delegated as-is. */ static <K, V> WellBehavedMap<K, V> wrap(Map<K, V> delegate) { return new WellBehavedMap<>(delegate); }
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 (Map.Entry<Foo, Integer> entry : map.entrySet()) { entry.setValue(entry.getValue() + 5); } assertEquals(ImmutableMap.of(Foo.X, 6, Foo.Y, 7, Foo.Z, 8), map); }