package speiger.src.collections.PACKAGE.utils; import java.util.Objects; /** * A Type Specific Strategy class that allows to give control hashcode generation and equals comparason for maps * @Type(T) */ public interface STRATEGY KEY_GENERIC_TYPE { #if TYPE_OBJECT /** * Identity Strategy */ public static final STRATEGY NO_GENERIC_TYPE IDENTITY = new IdentityStrategyBRACES(); /** * @Type(T) * @return a IdentityStrategy that allows to emulate a IdentityHashMap */ public static GENERIC_KEY_BRACES STRATEGY KEY_GENERIC_TYPE identityStrategy() { return (STRATEGY)IDENTITY; } #endif /** * Normal Strategy */ public static final STRATEGY NO_GENERIC_TYPE NORMAL = new NormalStrategyBRACES(); /** * @Type(T) * @return a Normal Strategy that is behaving exactly like the normal Hash Strategy in the Hash Collections */ public static GENERIC_KEY_BRACES STRATEGY KEY_GENERIC_TYPE normalStrategy() { return (STRATEGY KEY_GENERIC_TYPE)NORMAL; } /** * Type Specific HashCode function * @param o the element that the hashcode is requested for (if object may be null) * @return hashcode for the given entry */ public int hashCode(KEY_TYPE o); /** * Type Specific Equals function * @param key the first element that should be compared with * @param value the second element that should be compared with (if object may be null) * @return if the elements match */ public boolean equals(KEY_TYPE key, KEY_TYPE value); #if TYPE_OBJECT /** * A Strategy that uses Identity HashCode instead of the normal hashing function. * This simulates a IdentityHashMap without having the need to actually implement it. * @Type(T) */ public static class IdentityStrategy KEY_GENERIC_TYPE implements STRATEGY KEY_GENERIC_TYPE { @Override public int hashCode(KEY_TYPE o) { return System.identityHashCode(o); } @Override public boolean equals(KEY_TYPE key, KEY_TYPE value) { return key == value; } } #endif /** * A Strategy that simulates the normal Hash Collection Behavior if you want to use Hash Control Collections to replace normal ones. * Only real reason to do that is you have to use this and want to get rid of implementations. * @Type(T) */ public static class NormalStrategy KEY_GENERIC_TYPE implements STRATEGY KEY_GENERIC_TYPE { @Override public int hashCode(KEY_TYPE o) { return KEY_TO_HASH(o); } @Override public boolean equals(KEY_TYPE key, KEY_TYPE value) { return EQUALS_KEY_TYPE(key, value); } } }