public void testBiMapEntrySetIteratorRemove() { BiMap<Integer, String> map = HashBiMap.create(); map.put(1, "one"); Set<Entry<Integer, String>> entries = map.entrySet(); Iterator<Entry<Integer, String>> iterator = entries.iterator(); Entry<Integer, String> entry = iterator.next(); entry.setValue("two"); // changes the iterator's current entry value assertEquals("two", map.get(1)); assertEquals(Integer.valueOf(1), map.inverse().get("two")); iterator.remove(); // removes the updated entry assertTrue(map.isEmpty()); }
public static String shortenCfName(String longName) throws PermanentBackendException { final String s; if (SHORT_CF_NAME_MAP.containsKey(longName)) { s = SHORT_CF_NAME_MAP.get(longName); Preconditions.checkNotNull(s); logger.debug("Substituted default CF name \"{}\" with short form \"{}\" to reduce HBase KeyValue size", longName, s); } else { if (SHORT_CF_NAME_MAP.containsValue(longName)) { String fmt = "Must use CF long-form name \"%s\" instead of the short-form name \"%s\" when configured with %s=true"; String msg = String.format(fmt, SHORT_CF_NAME_MAP.inverse().get(longName), longName, SHORT_CF_NAMES.getName()); throw new PermanentBackendException(msg); } s = longName; logger.debug("Kept default CF name \"{}\" because it has no associated short form", s); } return s; }
/** See {@link SynchronizedBiMapTest} for more tests. */ public void testSynchronizedBiMap() { BiMap<String, Integer> bimap = HashBiMap.create(); bimap.put("one", 1); BiMap<String, Integer> sync = Maps.synchronizedBiMap(bimap); bimap.put("two", 2); sync.put("three", 3); assertEquals(ImmutableSet.of(1, 2, 3), bimap.inverse().keySet()); assertEquals(ImmutableSet.of(1, 2, 3), sync.inverse().keySet()); }
@Override public int enumerateLabel(String label) { if (!labelEnumeration.containsKey(label)) { labelEnumeration.put(label, nextLabel); nextLabel++; } return labelEnumeration.get(label); }
public void testInverseInsertionOrderAfterInverseForcePut() { BiMap<String, Integer> map = HashBiMap.create(); map.put("foo", 1); map.put("bar", 2); map.put("quux", 3); map.inverse().forcePut(1, "quux"); assertThat(map.inverse().entrySet()) .containsExactly(Maps.immutableEntry(2, "bar"), Maps.immutableEntry(1, "quux")) .inOrder(); }
@SuppressWarnings("unchecked") @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES}) @CollectionSize.Require(ONE) public void testForcePutNullValuePresent() { initMapWithNullValue(); getMap().forcePut(k1(), null); expectContents(Helpers.mapEntry(k1(), (V) null)); assertFalse(getMap().containsKey(k0())); assertTrue(getMap().containsKey(k1())); assertTrue(getMap().inverse().containsKey(null)); assertNull(getMap().get(k1())); assertEquals(k1(), getMap().inverse().get(null)); assertEquals(1, getMap().size()); }
public void testUnmodifiableBiMap() { BiMap<Integer, String> mod = HashBiMap.create(); mod.put(1, "one"); mod.put(2, "two"); mod.put(3, "three"); assertSame(unmod.inverse(), unmod.inverse()); assertSame(unmod, unmod.inverse().inverse()); mod.put(4, "four"); assertEquals(true, unmod.get(4).equals("four")); assertEquals(true, unmod.inverse().get("four").equals(4)); unmod.put(4, "four"); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { unmod.forcePut(4, "four"); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { unmod.putAll(Collections.singletonMap(4, "four")); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { BiMap<String, Number> inverse = unmod.inverse(); try { inverse.put("four", 4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) {
@VisibleForTesting static List<String> getUniqueMetrics(List<TimelineObjectHolder<String, DataSegment>> timelineSegments) { final BiMap<String, Integer> uniqueMetrics = HashBiMap.create(); // Here, we try to retain the order of metrics as they were specified. Metrics are extracted from the recent // segments to olders. // timelineSegments are sorted in order of interval int index = 0; for (TimelineObjectHolder<String, DataSegment> timelineHolder : Lists.reverse(timelineSegments)) { for (PartitionChunk<DataSegment> chunk : timelineHolder.getObject()) { for (String metric : chunk.getObject().getMetrics()) { if (!uniqueMetrics.containsKey(metric)) { uniqueMetrics.put(metric, index++); } } } } final BiMap<Integer, String> orderedMetrics = uniqueMetrics.inverse(); return IntStream.range(0, orderedMetrics.size()) .mapToObj(orderedMetrics::get) .collect(Collectors.toList()); } }
@Override protected void expectContents(Collection<Entry<K, V>> expected) { super.expectContents(expected); List<Entry<V, K>> reversedEntries = new ArrayList<>(); for (Entry<K, V> entry : expected) { reversedEntries.add(reverseEntry(entry)); } Helpers.assertEqualIgnoringOrder(getMap().inverse().entrySet(), reversedEntries); for (Entry<K, V> entry : expected) { assertEquals( "Wrong key for value " + entry.getValue(), entry.getKey(), getMap().inverse().get(entry.getValue())); } }
@Override public synchronized <V> void registerClass(int registrationNo, Class<V> datatype, AttributeSerializer<V> serializer) { Preconditions.checkArgument(registrationNo >= 0 && registrationNo < MAX_REGISTRATION_NO, "Registration number" + " out of range [0,%s]: %s", MAX_REGISTRATION_NO, registrationNo); if (datatype == HashMap.class) { final Integer hashMapRegNo = registrations.inverse().get(normalizeDataType(HashMap.class)); if (hashMapRegNo != null) { // Remove the default HashMap serializer so we can replace it with the custom one registrations.remove(hashMapRegNo); handlers.remove(datatype); } } registerClassInternal(CLASS_REGISTRATION_OFFSET + registrationNo, datatype, serializer); }
private int getDataTypeRegistration(Class datatype) { Integer registrationNo = registrations.inverse().get(normalizeDataType(datatype)); Preconditions.checkArgument(registrationNo!=null,"Datatype is not supported by database since no serializer has been registered: %s",datatype); assert registrationNo>0; return registrationNo; }
public void testIdentityKeySetIteratorRemove() { BiMap<Integer, String> bimap = new AbstractBiMap<Integer, String>( new IdentityHashMap<Integer, String>(), new IdentityHashMap<String, Integer>()) {}; bimap.put(1, "one"); bimap.put(2, "two"); bimap.put(3, "three"); Iterator<Integer> iterator = bimap.keySet().iterator(); iterator.next(); iterator.next(); iterator.remove(); iterator.next(); iterator.remove(); assertEquals(1, bimap.size()); assertEquals(1, bimap.inverse().size()); }
private static <X, Y> Y convert(BiMap<X, Y> bimap, X input) { Y output = bimap.get(input); checkArgument(output != null, "No non-null mapping present for input: %s", input); return output; }
public void testCreate() { EnumBiMap<Currency, Country> bimap = EnumBiMap.create(Currency.class, Country.class); assertTrue(bimap.isEmpty()); assertEquals("{}", bimap.toString()); assertEquals(HashBiMap.create(), bimap); bimap.put(Currency.DOLLAR, Country.CANADA); assertEquals(Country.CANADA, bimap.get(Currency.DOLLAR)); assertEquals(Currency.DOLLAR, bimap.inverse().get(Country.CANADA)); }
public synchronized <V> void registerClassInternal(int registrationNo, Class<? extends V> datatype, AttributeSerializer<V> serializer) { Preconditions.checkArgument(registrationNo>0); //must be bigger than 0 since 0 is used to indicate null values Preconditions.checkNotNull(datatype); Preconditions.checkArgument(!handlers.containsKey(datatype), "DataType has already been registered: %s", datatype); Preconditions.checkArgument(!registrations.containsKey(registrationNo), "A datatype has already been registered for no: %s",registrationNo); Preconditions.checkNotNull(serializer,"Need to provide a serializer for datatype: %s",datatype); registrations.put(registrationNo, datatype); if (serializer instanceof SerializerInjected) ((SerializerInjected)serializer).setSerializer(this); handlers.put(datatype, serializer); }
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { Object mutex = new Object(); BiMap<String, String> result = HashBiMap.create(); for (Entry<String, String> entry : entries) { checkArgument(!result.containsKey(entry.getKey())); result.put(entry.getKey(), entry.getValue()); } return Maps.synchronizedBiMap(result); } }
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { BiMap<String, String> bimap = HashBiMap.create(entries.length); for (Entry<String, String> entry : entries) { checkArgument(!bimap.containsKey(entry.getKey())); bimap.put(entry.getKey(), entry.getValue()); } return Maps.unmodifiableBiMap(bimap); } })
@Override public Set<E> edgesConnecting(N node) { return new EdgesConnecting<E>(((BiMap<E, N>) incidentEdgeMap).inverse(), node); } }