apache java mysql_MySQL Java 架构代码-Apache-立哥开发

/*Copy right 2020  Jacky Zong . All rights reserved.*/

package org.apache.ibatis.cache;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.List;

import java.util.StringJoiner;

import org.apache.ibatis.reflection.ArrayUtil;

public class CacheKey implements Cloneable, Serializable {

private static final long serialVersionUID = 1146682552656046210L;

public static final CacheKey NULL_CACHE_KEY = new CacheKey() {

@Override

public void update(Object object) {

throw new CacheException("Not allowed to update a null cache key instance.");

}

@Override

public void updateAll(Object[] objects) {

throw new CacheException("Not allowed to update a null cache key instance.");

}

};

private static final int DEFAULT_MULTIPLIER = 37;

private static final int DEFAULT_HASHCODE = 17;

private final int multiplier;

private int hashcode;

private long checksum;

private int count;

private List updateList;

public CacheKey() {

this.hashcode = DEFAULT_HASHCODE;

this.multiplier = DEFAULT_MULTIPLIER;

this.count = 0;

this.updateList = new ArrayList<>();

}

public CacheKey(Object[] objects) {

this();

updateAll(objects);

}

public int getUpdateCount() {

return updateList.size();

}

public void update(Object object) {

int baseHashCode = object == null ? 1 : ArrayUtil.hashCode(object);

count++;

checksum += baseHashCode;

baseHashCode *= count;

hashcode = multiplier * hashcode + baseHashCode;

updateList.add(object);

}

public void updateAll(Object[] objects) {

for (Object o : objects) {

update(o);

}

}

@Override

public boolean equals(Object object) {

if (this == object) {

return true;

}

if (!(object instanceof CacheKey)) {

return false;

}

final CacheKey cacheKey = (CacheKey) object;

if (hashcode != cacheKey.hashcode) {

return false;

}

if (checksum != cacheKey.checksum) {

return false;

}

if (count != cacheKey.count) {

return false;

}

for (int i = 0; i < updateList.size(); i++) {

Object thisObject = updateList.get(i);

Object thatObject = cacheKey.updateList.get(i);

if (!ArrayUtil.equals(thisObject, thatObject)) {

return false;

}

}

return true;

}

@Override

public int hashCode() {

return hashcode;

}

@Override

public String toString() {

StringJoiner returnValue = new StringJoiner(":");

returnValue.add(String.valueOf(hashcode));

returnValue.add(String.valueOf(checksum));

updateList.stream().map(ArrayUtil::toString).forEach(returnValue::add);

return returnValue.toString();

}

@Override

public CacheKey clone() throws CloneNotSupportedException {

CacheKey clonedCacheKey = (CacheKey) super.clone();

clonedCacheKey.updateList = new ArrayList<>(updateList);

return clonedCacheKey;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值