public void testConstrainedBiMapLegal() { BiMap<String, Integer> map = new AbstractBiMap<String, Integer>( Maps.<String, Integer>newLinkedHashMap(), Maps.<Integer, String>newLinkedHashMap()) {}; BiMap<String, Integer> constrained = MapConstraints.constrainedBiMap(map, TEST_CONSTRAINT); map.put(TEST_KEY, TEST_VALUE); constrained.put("foo", 1); map.putAll(ImmutableMap.of("bar", 2)); constrained.putAll(ImmutableMap.of("baz", 3)); assertTrue(map.equals(constrained)); assertTrue(constrained.equals(map)); assertEquals(map.entrySet(), constrained.entrySet()); assertEquals(map.keySet(), constrained.keySet()); assertEquals(map.values(), constrained.values()); assertEquals(map.toString(), constrained.toString()); assertEquals(map.hashCode(), constrained.hashCode()); ASSERT .that(map.entrySet()) .has() .allOf( Maps.immutableEntry(TEST_KEY, TEST_VALUE), Maps.immutableEntry("foo", 1), Maps.immutableEntry("bar", 2), Maps.immutableEntry("baz", 3)) .inOrder(); }
@Test public void whenCreateBiMap_thenCreated() { final BiMap<String, Integer> words = HashBiMap.create(); words.put("First", 1); words.put("Second", 2); words.put("Third", 3); assertEquals(2, words.get("Second").intValue()); assertEquals("Third", words.inverse().get(3)); }
public void addBootstrapToken(Token token, InetAddress endpoint) { assert token != null; assert endpoint != null; lock.writeLock().lock(); try { InetAddress oldEndPoint = null; oldEndPoint = bootstrapTokens.get(token); if (oldEndPoint != null && !oldEndPoint.equals(endpoint)) throw new RuntimeException( "Bootstrap Token collision between " + oldEndPoint + " and " + endpoint + " (token " + token); oldEndPoint = tokenToEndPointMap.get(token); if (oldEndPoint != null && !oldEndPoint.equals(endpoint)) throw new RuntimeException( "Bootstrap Token collision between " + oldEndPoint + " and " + endpoint + " (token " + token); bootstrapTokens.inverse().remove(endpoint); bootstrapTokens.put(token, endpoint); } finally { lock.writeLock().unlock(); } }
@Override protected Map<String, Integer> makePopulatedMap() { BiMap<String, Integer> bimap = HashBiMap.create(); bimap.put("foo", 1); bimap.put("bar", 2); return Maps.unmodifiableBiMap(bimap); }
public void testPutWithAllowedKeyForbiddenValue() { BiMap<String, String> map = MapConstraints.constrainedBiMap(HashBiMap.<String, String>create(), TEST_CONSTRAINT); try { map.put("allowed", TEST_VALUE); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { // success } }
@Override protected BiMap<String, String> create(Entry<String, String>[] entries) { BiMap<String, String> bimap = MapConstraints.constrainedBiMap(HashBiMap.<String, String>create(), TEST_CONSTRAINT); for (Entry<String, String> entry : entries) { checkArgument(!bimap.containsKey(entry.getKey())); bimap.put(entry.getKey(), entry.getValue()); } return bimap; }
@SuppressWarnings("unchecked") @Override public BiMap<Country, Currency> create(Object... entries) { BiMap<Country, Currency> result = EnumBiMap.create(Country.class, Currency.class); for (Object object : entries) { Entry<Country, Currency> entry = (Entry<Country, Currency>) object; result.put(entry.getKey(), entry.getValue()); } return result; }
public void testConstrainedBiMapIllegal() { BiMap<String, Integer> map = new AbstractBiMap<String, Integer>( Maps.<String, Integer>newLinkedHashMap(), Maps.<Integer, String>newLinkedHashMap()) {}; BiMap<String, Integer> constrained = MapConstraints.constrainedBiMap(map, TEST_CONSTRAINT); try { constrained.put(TEST_KEY, TEST_VALUE); fail("TestKeyException expected"); } catch (TestKeyException expected) { } try { constrained.put("baz", TEST_VALUE); fail("TestValueException expected"); } catch (TestValueException expected) { } try { constrained.put(TEST_KEY, 3); fail("TestKeyException expected"); } catch (TestKeyException expected) { } try { constrained.putAll(ImmutableMap.of("baz", 3, TEST_KEY, 4)); fail("TestKeyException expected"); } catch (TestKeyException expected) { } try { constrained.forcePut(TEST_KEY, 3); fail("TestKeyException expected"); } catch (TestKeyException expected) { } try { constrained.inverse().forcePut(TEST_VALUE, "baz"); fail("TestValueException expected"); } catch (TestValueException expected) { } try { constrained.inverse().forcePut(3, TEST_KEY); fail("TestKeyException expected"); } catch (TestKeyException expected) { } assertEquals(Collections.emptySet(), map.entrySet()); assertEquals(Collections.emptySet(), constrained.entrySet()); }
public void updateNormalToken(Token token, InetAddress endpoint) { assert token != null; assert endpoint != null; lock.writeLock().lock(); try { bootstrapTokens.inverse().remove(endpoint); tokenToEndPointMap.inverse().remove(endpoint); if (!endpoint.equals(tokenToEndPointMap.put(token, endpoint))) { sortedTokens = sortTokens(); } leavingEndPoints.remove(endpoint); } finally { lock.writeLock().unlock(); } }
public void addOldCfIdMapping(Integer oldId, UUID newId) { if (oldId == null) return; oldCfIdMap.put(oldId, newId); }