CacheKey.java

  1. /*
  2.  *    Copyright 2009-2023 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.  *       https://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.     private static final long serialVersionUID = 1L;

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

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

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

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

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

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

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

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

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

  65.     hashcode = multiplier * hashcode + baseHashCode;

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

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

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

  81.     final CacheKey cacheKey = (CacheKey) object;

  82.     if ((hashcode != cacheKey.hashcode) || (checksum != cacheKey.checksum) || (count != cacheKey.count)) {
  83.       return false;
  84.     }

  85.     for (int i = 0; i < updateList.size(); i++) {
  86.       Object thisObject = updateList.get(i);
  87.       Object thatObject = cacheKey.updateList.get(i);
  88.       if (!ArrayUtil.equals(thisObject, thatObject)) {
  89.         return false;
  90.       }
  91.     }
  92.     return true;
  93.   }

  94.   @Override
  95.   public int hashCode() {
  96.     return hashcode;
  97.   }

  98.   @Override
  99.   public String toString() {
  100.     StringJoiner returnValue = new StringJoiner(":");
  101.     returnValue.add(String.valueOf(hashcode));
  102.     returnValue.add(String.valueOf(checksum));
  103.     updateList.stream().map(ArrayUtil::toString).forEach(returnValue::add);
  104.     return returnValue.toString();
  105.   }

  106.   @Override
  107.   public CacheKey clone() throws CloneNotSupportedException {
  108.     CacheKey clonedCacheKey = (CacheKey) super.clone();
  109.     clonedCacheKey.updateList = new ArrayList<>(updateList);
  110.     return clonedCacheKey;
  111.   }

  112. }