Forráskód Böngészése

change: semantics of GenericKey and GenericMap

Ranides Atterwim 1 éve
szülő
commit
6bbdf698ed

+ 2 - 2
assira.core/src/main/java/net/ranides/assira/collection/prototype/GenericKey.java

@@ -24,7 +24,7 @@ import net.ranides.assira.reflection.IClass;
  * @param <V> V
  * @author msieron
  */
-@EqualsAndHashCode
+@EqualsAndHashCode(exclude = "type")
 public class GenericKey<K,V> implements Function<GenericMap<K>, V>, Serializable {
 
     private final IClass<V> type;
@@ -32,7 +32,7 @@ public class GenericKey<K,V> implements Function<GenericMap<K>, V>, Serializable
 
     @SuppressWarnings("unchecked")
     protected GenericKey(K id) {
-        this.type = (IClass<V>)IClass.typefor(this).context().typeinfo("K");;
+        this.type = (IClass<V>)IClass.typefor(this).context().typeinfo("V");;
         this.id = id;
     }
     

+ 3 - 2
assira.core/src/main/java/net/ranides/assira/collection/prototype/OpenGenericMap.java

@@ -82,7 +82,7 @@ public class OpenGenericMap<K> implements GenericMap<K> {
 
     @Override
     public boolean containsKey(GenericKey<K, ?> key) {
-        return map.containsKey(key);
+        return map.containsKey(key) && key.type().isInstance(map.get(key));
     }
 
     @Override
@@ -92,7 +92,8 @@ public class OpenGenericMap<K> implements GenericMap<K> {
 
     @Override
     public <V> V get(GenericKey<K, V> key) {
-        return (V)map.get(key);
+        V value = (V) map.get(key);
+        return key.type().isInstance(value) ? value : null;
     }
 
     @Override

+ 2 - 1
assira.core/src/main/java/net/ranides/assira/credentials/UserProperty.java

@@ -17,7 +17,8 @@ import net.ranides.assira.collection.prototype.GenericKey;
  *
  * @author Ranides Atterwim {@literal <ranides@gmail.com>}
  */
-@UtilityClass public class UserProperty {
+@UtilityClass
+public class UserProperty {
 
     /**
      * Unique account identifier: most of the time equivalent to login

+ 14 - 0
assira.core/src/test/java/net/ranides/assira/collection/maps/OpenGenericMapTest.java

@@ -1,9 +1,13 @@
 package net.ranides.assira.collection.maps;
 
+import net.ranides.assira.collection.prototype.GenericKey;
+import net.ranides.assira.collection.prototype.GenericMap;
 import net.ranides.assira.collection.prototype.OpenGenericMap;
 import net.ranides.test.contracts.collection.maps.GenericMapTester;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+
 public class OpenGenericMapTest {
 
     @Test
@@ -31,4 +35,14 @@ public class OpenGenericMapTest {
         GenericMapTester.setEntry(OpenGenericMap::new);
     }
 
+    @Test
+    public void keyInstance() {
+        GenericMap<String> map = new OpenGenericMap<>();
+        map.put(GenericKey.of(Integer.class, "a"), 1);
+        map.put(GenericKey.of(Float.class, "b"), 2.0f);
+
+        assertEquals(Integer.valueOf(1), map.get(GenericKey.of(Integer.class, "a")));
+        assertEquals(1, map.get(GenericKey.of(Number.class, "a")));
+        assertEquals(1, map.get(GenericKey.of("a")));
+    }
 }

+ 17 - 2
assira.core/src/test/java/net/ranides/test/contracts/collection/maps/GenericMapTester.java

@@ -11,9 +11,12 @@ import static org.junit.Assert.assertEquals;
 
 public class GenericMapTester {
 
-    private static final GenericKey<String, Integer> I_A = GenericKey.of(Integer.class, "a");
+    private static final GenericKey<String, Integer>      I_A = GenericKey.of(Integer.class, "a");
     private static final GenericKey<String, String>       S_A = GenericKey.of(String.class, "a");
+    private static final GenericKey<String, Number>       N_A = GenericKey.of(Number.class, "a");
     private static final GenericKey<String, Integer>      I_B = GenericKey.of(Integer.class, "b");
+    private static final GenericKey<String, Object>       O_B = GenericKey.of(Object.class, "b");
+    private static final GenericKey<String, Float>        F_B = GenericKey.of(Float.class, "b");
     private static final GenericKey<String, Double>       D_C = GenericKey.of(Double.class, "c");
     private static final GenericKey<String, CharSequence> C_D = GenericKey.of(CharSequence.class, "d");
     private static final GenericKey<String, CharSequence> C_E = GenericKey.of(CharSequence.class, "e");
@@ -59,8 +62,20 @@ public class GenericMapTester {
         map1.put(C_D, "text");
 
         assertTrue(map1.containsKey(I_A));
-        assertTrue(map1.containsKey(I_B));
+        assertTrue(map1.containsKey(N_A));
         assertFalse(map1.containsKey(S_A));
+
+        assertTrue(map1.containsKey(I_B));
+        assertTrue(map1.containsKey(O_B));
+        assertFalse(map1.containsKey(F_B));
+
+        assertNotNull(map1.get(I_A));
+        assertNotNull(map1.get(N_A));
+        assertNull(map1.get(S_A));
+
+        assertNotNull(map1.get(I_B));
+        assertNotNull(map1.get(O_B));
+        assertNull(map1.get(F_B));
     }
 
     public static void entries(Supplier<GenericMap<String>> factory) {