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

fr.vergne.pester.util.cache.ParameteredKey Maven / Gradle / Ivy

The newest version!
package fr.vergne.pester.util.cache;

import java.util.Arrays;
import java.util.function.Function;

public class ParameteredKey implements Key {
	private final Object[] parameters;
	private final Function caster;

	@SuppressWarnings("unchecked")
	private ParameteredKey(Object[] parameters) {
		this(x -> (R) x, parameters);
	}

	private ParameteredKey(Class type, Object[] parameters) {
		this(type::cast, parameters);
	}

	private ParameteredKey(Function caster, Object[] parameters) {
		this.parameters = parameters;
		this.caster = caster;
	}

	@Override
	public R cast(Object value) {
		return caster.apply(value);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		} else if (obj instanceof ParameteredKey) {
			ParameteredKey that = (ParameteredKey) obj;
			return Arrays.deepEquals(this.parameters, that.parameters);
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return Arrays.deepHashCode(parameters);
	}

	@Override
	public String toString() {
		return Arrays.deepToString(parameters);
	}

	public static  ParameteredKey create(Object... parameters) {
		return new ParameteredKey(parameters);
	}

	public static  ParameteredKey createSafe(Class type, Object... parameters) {
		return new ParameteredKey(type, parameters);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy