CacheKey.java

  1. /*
  2.  *    Copyright 2009-2021 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.cache;

  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.StringJoiner;

  21. import org.apache.ibatis.reflection.ArrayUtil;

  22. /**
  23.  * @author Clinton Begin
  24.  */
  25. public class CacheKey implements Cloneable, Serializable {

  26.   private static final long serialVersionUID = 1146682552656046210L;

  27.   public static final CacheKey NULL_CACHE_KEY = new CacheKey() {

  28.     @Override
  29.     public void update(Object object) {
  30.       throw new CacheException("Not allowed to update a null cache key instance.");
  31.     }

  32.     @Override
  33.     public void updateAll(Object[] objects) {
  34.       throw new CacheException("Not allowed to update a null cache key instance.");
  35.     }
  36.   };

  37.   private static final int DEFAULT_MULTIPLIER = 37;
  38.   private static final int DEFAULT_HASHCODE = 17;

  39.   private final int multiplier;
  40.   private int hashcode;
  41.   private long checksum;
  42.   private int count;
  43.   // 8/21/2017 - Sonarlint flags this as needing to be marked transient. While true if content is not serializable, this
  44.   // is not always true and thus should not be marked transient.
  45.   private List<Object> updateList;

  46.   public CacheKey() {
  47.     this.hashcode = DEFAULT_HASHCODE;
  48.     this.multiplier = DEFAULT_MULTIPLIER;
  49.     this.count = 0;
  50.     this.updateList = new ArrayList<>();
  51.   }

  52.   public CacheKey(Object[] objects) {
  53.     this();
  54.     updateAll(objects);
  55.   }

  56.   public int getUpdateCount() {
  57.     return updateList.size();
  58.   }

  59.   public void update(Object object) {
  60.     int baseHashCode = object == null ? 1 : ArrayUtil.hashCode(object);

  61.     count++;
  62.     checksum += baseHashCode;
  63.     baseHashCode *= count;

  64.     hashcode = multiplier * hashcode + baseHashCode;

  65.     updateList.add(object);
  66.   }

  67.   public void updateAll(Object[] objects) {
  68.     for (Object o : objects) {
  69.       update(o);
  70.     }
  71.   }

  72.   @Override
  73.   public boolean equals(Object object) {
  74.     if (this == object) {
  75.       return true;
  76.     }
  77.     if (!(object instanceof CacheKey)) {
  78.       return false;
  79.     }

  80.     final CacheKey cacheKey = (CacheKey) object;

  81.     if (hashcode != cacheKey.hashcode) {
  82.       return false;
  83.     }
  84.     if (checksum != cacheKey.checksum) {
  85.       return false;
  86.     }
  87.     if (count != cacheKey.count) {
  88.       return false;
  89.     }

  90.     for (int i = 0; i < updateList.size(); i++) {
  91.       Object thisObject = updateList.get(i);
  92.       Object thatObject = cacheKey.updateList.get(i);
  93.       if (!ArrayUtil.equals(thisObject, thatObject)) {
  94.         return false;
  95.       }
  96.     }
  97.     return true;
  98.   }

  99.   @Override
  100.   public int hashCode() {
  101.     return hashcode;
  102.   }

  103.   @Override
  104.   public String toString() {
  105.     StringJoiner returnValue = new StringJoiner(":");
  106.     returnValue.add(String.valueOf(hashcode));
  107.     returnValue.add(String.valueOf(checksum));
  108.     updateList.stream().map(ArrayUtil::toString).forEach(returnValue::add);
  109.     return returnValue.toString();
  110.   }

  111.   @Override
  112.   public CacheKey clone() throws CloneNotSupportedException {
  113.     CacheKey clonedCacheKey = (CacheKey) super.clone();
  114.     clonedCacheKey.updateList = new ArrayList<>(updateList);
  115.     return clonedCacheKey;
  116.   }

  117. }