content: package com.alibaba.tesla.tkgone.server.common;
  
  import com.alibaba.fastjson.JSONObject;
  
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  import java.util.Collection;
  import java.util.SortedMap;
  import java.util.TreeMap;
  
  /**
   * @author yangjinghua
   */
  public class ConsistentHash<T> {
  
      private final HashFunction hashFunction = new HashFunction();
      private final int numberOfReplicas = 1;
      private final SortedMap<Long, T> circle = new TreeMap<>();
  
      public ConsistentHash(Collection<T> nodes) {
          for (T node : nodes) {
              add(node);
          }
      }
  
      public void add(T node) {
          for (int i = 0; i < numberOfReplicas; i++) {
              circle.put(hashFunction.hash(node.toString() + i), node);
          }
      }
  
      public void remove(T node) {
          for (int i = 0; i < numberOfReplicas; i++) {
              circle.remove(hashFunction.hash(node.toString() + i));
          }
      }
  
      public T get(Object key) {
          if (circle.isEmpty()) {
              return null;
          }
          long hash = hashFunction.hash(JSONObject.toJSONString(key));
          if (!circle.containsKey(hash)) {
              SortedMap<Long, T> tailMap = circle.tailMap(hash);
              hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
          }
          return circle.get(hash);
      }
  
      public long getSize() {
          return circle.size();
      }
  
      public static class HashFunction {
          private MessageDigest md5 = null;
  
          public long hash(String key) {
              if (md5 == null) {
                  try {
                      md5 = MessageDigest.getInstance("MD5");
                  } catch (NoSuchAlgorithmException e) {
                      throw new IllegalStateException("no md5 algorythm found");
                  }
              }
  
              md5.reset();
              md5.update(key.getBytes());
              byte[] bKey = md5.digest();
              long res = ((long) (bKey[3] & 0xFF) << 24) | ((long) (bKey[2] & 0xFF) << 16) | ((long) (bKey[1] & 0xFF) << 8)
                  | (long) (bKey[0] & 0xFF);
              return res & 0xffffffffL;
          }
  
      }
  
  }
  