From 13b2c727fcf8c21c2a819398debf7f5d1c8759b8 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 22 May 2021 06:08:27 +0200 Subject: [PATCH] Added Identity HashMap support via Strategies --- .../templates/utils/Strategy.template | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Strategy.template b/src/builder/resources/speiger/assets/collections/templates/utils/Strategy.template index 4e4f66a2..8fc4af5b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Strategy.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Strategy.template @@ -6,6 +6,19 @@ package speiger.src.collections.PACKAGE.utils; */ 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 /** * Type Specific HashCode function * @param o the element that the hashcode is requested for (if object may be null) @@ -20,4 +33,19 @@ public interface STRATEGY KEY_GENERIC_TYPE * @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 } \ No newline at end of file