All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.cache.DeepCacheBuilder Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package com.google.common.cache;

import java.util.Arrays;
import java.util.Objects;

import com.google.common.base.Equivalence;

public class DeepCacheBuilder {
	public static final Equivalence DEEP_EQUIVALENCE = new Equivalence(){

		@Override
		protected boolean doEquivalent(Object a, Object b) {
			return Objects.deepEquals(a, b);
		}

		@Override
		protected int doHash(Object object) {
			return deepHashCode(object);
		}};
		
	public static final int deepHashCode(Object a){
		if (a == null){
            return 0;
		}else if (a instanceof Object[]) {
			return Arrays.deepHashCode((Object[]) a);
		} else if (a instanceof byte[]) {
			return Arrays.hashCode((byte[]) a);
		} else if (a instanceof short[]) {
			return Arrays.hashCode((short[]) a);
		} else if (a instanceof int[]) {
			return Arrays.hashCode((int[]) a);
		} else if (a instanceof long[]) {
			return Arrays.hashCode((long[]) a);
		} else if (a instanceof char[]) {
			return Arrays.hashCode((char[]) a);
		} else if (a instanceof float[]) {
			return Arrays.hashCode((float[]) a);
		} else if (a instanceof double[]) {
			return Arrays.hashCode((double[]) a);
		} else if (a instanceof boolean[]) {
			return Arrays.hashCode((boolean[]) a);
		} 
		return a.hashCode();
	}
	public static final CacheBuilder newBuilder(){
		return newBuilder(DEEP_EQUIVALENCE,DEEP_EQUIVALENCE);
	}
	public static final CacheBuilder newBuilder(
			Equivalence keyEquivalence,
			Equivalence valueEquivalence){
		CacheBuilder builder = CacheBuilder.newBuilder();
		if(keyEquivalence != null){
			builder.keyEquivalence(keyEquivalence);
		}
		if(valueEquivalence != null){
			builder.valueEquivalence(valueEquivalence);	
		}
		return builder;
	}
}