コード例 #1
0
  /**
   * Store an end-point to host ID mapping. Each ID must be unique, and cannot be changed after the
   * fact.
   *
   * @param hostId
   * @param endpoint
   */
  public void updateHostId(UUID hostId, InetAddress endpoint) {
    assert hostId != null;
    assert endpoint != null;

    lock.writeLock().lock();
    try {
      InetAddress storedEp = endpointToHostIdMap.inverse().get(hostId);
      if (storedEp != null) {
        if (!storedEp.equals(endpoint) && (FailureDetector.instance.isAlive(storedEp))) {
          throw new RuntimeException(
              String.format(
                  "Host ID collision between active endpoint %s and %s (id=%s)",
                  storedEp, endpoint, hostId));
        }
      }

      UUID storedId = endpointToHostIdMap.get(endpoint);
      if ((storedId != null) && (!storedId.equals(hostId)))
        logger.warn("Changing {}'s host ID from {} to {}", endpoint, storedId, hostId);

      endpointToHostIdMap.forcePut(endpoint, hostId);
    } finally {
      lock.writeLock().unlock();
    }
  }
コード例 #2
0
 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());
 }