content: package core;
  
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.util.Collection;
  import java.util.SortedMap;
  import java.util.TreeMap;
  import java.util.function.Function;
  
  public class ConsistentHash<T> {
  
      private final Function<String, Long> hashFunction;
      private final int numberOfReplicas;
      private final SortedMap<Long, T> circle = new TreeMap<>();
  
      public ConsistentHash(Collection<T> nodes, int numberOfReplicas) {
          this(nodes, numberOfReplicas, new MD5Hash());
      }
  
      public ConsistentHash(Collection<T> nodes, int numberOfReplicas, Function<String, Long> hashFunction) {
          this.hashFunction = hashFunction;
          this.numberOfReplicas = numberOfReplicas;
  
          for (T node : nodes) {
              add(node);
          }
      }
  
      public void add(T node) {
          for (int i = 0; i < numberOfReplicas; i++) {
              circle.put(hashFunction.apply(node.toString() + i), node);
          }
      }
  
      public void remove(T node) {
          for (int i = 0; i < numberOfReplicas; i++) {
              circle.remove(hashFunction.apply(node.toString() + i));
          }
      }
  
      public T get(String key) {
          if (circle.isEmpty()) {
              return null;
          }
          long hash = hashFunction.apply(key);
          if (!circle.containsKey(hash)) {
              SortedMap<Long, T> tailMap = circle.tailMap(hash);
              hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
          }
          return circle.get(hash);
      }
  
  
      private static class MD5Hash implements Function<String, Long> {
  
          MessageDigest instance;
  
          public MD5Hash() {
              try {
                  instance = MessageDigest.getInstance("MD5");
              } catch (NoSuchAlgorithmException e) {
                  // ignore
              }
          }
  
          @Override
          public Long apply(String key) {
              instance.reset();
              instance.update(key.getBytes());
              byte[] digest = instance.digest();
  
              long h = 0;
              for (int i = 0; i < 4; i++) {
                  h <<= 8;
                  h |= ((int) digest[i]) & 0xFF;
              }
              return h;
          }
  
      }
  
  }