浏览代码

PrototypeKey: merged with GenericKey
PrototypeMap: fixed equals & serializable

Ranides Atterwim 5 年之前
父节点
当前提交
180ff100e7

+ 32 - 41
assira/src/main/java/net/ranides/assira/collection/maps/GenericKey.java

@@ -6,74 +6,65 @@
  */
  */
 package net.ranides.assira.collection.maps;
 package net.ranides.assira.collection.maps;
 
 
-import java.util.Objects;
+import lombok.EqualsAndHashCode;
+
+import java.io.Serializable;
 import java.util.function.Function;
 import java.util.function.Function;
-import net.ranides.assira.generic.HashUtils;
+
 import net.ranides.assira.reflection.IClass;
 import net.ranides.assira.reflection.IClass;
 
 
 /**
 /**
  *
  *
  * @author msieron
  * @author msieron
  */
  */
-public class GenericKey<K,V> implements Function<GenericMap<K>, V> {
-    
+@EqualsAndHashCode
+public class GenericKey<K,V> implements Function<GenericMap<K>, V>, Serializable {
+
     private final IClass<V> type;
     private final IClass<V> type;
-    private final K key;
+    private final K id;
 
 
     @SuppressWarnings("unchecked")
     @SuppressWarnings("unchecked")
-    public GenericKey(K key) {
-        this.type = (IClass<V>)IClass.typeinfo(getClass()).params().get(0);
-        this.key = key;
+    protected GenericKey(K id) {
+        this.type = (IClass<V>)IClass.typeinfo(getClass()).params().get(1);
+        this.id = id;
     }
     }
     
     
-    public GenericKey(IClass<V> type, K key) {
+    private GenericKey(IClass<V> type, K id) {
         this.type = type;
         this.type = type;
-        this.key = key;
+        this.id = id;
     }
     }
-    
-    public GenericKey(Class<V> type, K key) {
-        this(IClass.typeinfo(type), key);
+
+    public static <K, T> GenericKey<K,T> of(Class<T> type, K id) {
+        return new GenericKey<>(IClass.typeinfo(type), id);
     }
     }
-    
+
+    public static <K, T> GenericKey<K, T> of(IClass<T> type, K id) {
+        return new GenericKey<>(type, id);
+    }
+
+    public static <K> GenericKey<K, Object> of(K id) {
+        return new GenericKey<>(IClass.OBJECT, id);
+    }
+
+    public V cast(Object value) {
+        return type.cast(value);
+    }
+
     public IClass<V> type() {
     public IClass<V> type() {
         return type;
         return type;
     }
     }
     
     
-    public K key() {
-        return key;
+    public K id() {
+        return id;
     }
     }
 
 
     @Override
     @Override
     public V apply(GenericMap<K> map) {
     public V apply(GenericMap<K> map) {
         return map.get(this);
         return map.get(this);
     }
     }
-    
-    public V apply(GenericHashMap<K> map, V ddefault) {
-        return map.getOrDefault(this, ddefault);
-    }
-
-    @Override
-    public int hashCode() {
-        return HashUtils.hashPair(type, key);
-    }
-
-    @Override
-    public boolean equals(Object object) {
-        if (this == object) {
-            return true;
-        }
-        if (object == null) {
-            return false;
-        }
-        if (getClass() != object.getClass()) {
-            return false;
-        }
-        final GenericKey<?, ?> that = (GenericKey<?, ?>) object;
-        return Objects.equals(this.type, that.type) && Objects.equals(this.key, that.key);
-    }
 
 
     @Override
     @Override
     public String toString() {
     public String toString() {
-        return "#" + key;
+        return id+":"+type.shortname();
     }
     }
 }
 }

+ 0 - 2
assira/src/main/java/net/ranides/assira/collection/prototype/PrototypeEntry.java

@@ -8,6 +8,4 @@ public interface PrototypeEntry<V> extends Map.Entry<Object, V> {
 
 
     IClass<V> getType();
     IClass<V> getType();
 
 
-    String getName();
-
 }
 }

+ 0 - 39
assira/src/main/java/net/ranides/assira/collection/prototype/PrototypeKey.java

@@ -1,39 +0,0 @@
-package net.ranides.assira.collection.prototype;
-
-import lombok.EqualsAndHashCode;
-import net.ranides.assira.reflection.IClass;
-
-@EqualsAndHashCode
-public final class PrototypeKey<V> {
-
-    // TODO merge with net.ranides.assira.collection.maps.GenericKey ?
-
-    private final IClass<V> type;
-    private final String name;
-
-    private PrototypeKey(IClass<V> type, String name) {
-        this.type = type;
-        this.name = name;
-    }
-
-    public IClass<V> type() {
-        return type;
-    }
-
-    public String name() {
-        return name;
-    }
-
-    public static <V> PrototypeKey<V> of(Class<V> type, String name) {
-        return new PrototypeKey<>(IClass.typeinfo(type), name);
-    }
-
-    public static <V> PrototypeKey<V> of(IClass<V> type, String name) {
-        return new PrototypeKey<>(type, name);
-    }
-
-    public static PrototypeKey<Object> of(String name) {
-        return new PrototypeKey<>(IClass.OBJECT, name);
-    }
-
-}

+ 19 - 27
assira/src/main/java/net/ranides/assira/collection/prototype/PrototypeMap.java

@@ -2,13 +2,15 @@ package net.ranides.assira.collection.prototype;
 
 
 import net.ranides.assira.collection.iterators.RemoveIterator;
 import net.ranides.assira.collection.iterators.RemoveIterator;
 import net.ranides.assira.collection.maps.AEntry;
 import net.ranides.assira.collection.maps.AEntry;
+import net.ranides.assira.collection.maps.AMap;
+import net.ranides.assira.collection.maps.GenericKey;
 import net.ranides.assira.collection.sets.SetUtils;
 import net.ranides.assira.collection.sets.SetUtils;
 import net.ranides.assira.reflection.*;
 import net.ranides.assira.reflection.*;
 
 
 import java.util.*;
 import java.util.*;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
 
 
-public class PrototypeMap implements Map<Object, Object> {
+public class PrototypeMap extends AMap<Object, Object> implements Map<Object, Object> {
 
 
     public static final PrototypeMap EMPTY = new PrototypeMap(true);
     public static final PrototypeMap EMPTY = new PrototypeMap(true);
 
 
@@ -135,8 +137,8 @@ public class PrototypeMap implements Map<Object, Object> {
 
 
     public final Optional<PrototypeEntry<?>> getProperty(Object key) {
     public final Optional<PrototypeEntry<?>> getProperty(Object key) {
         if(containsKey(key)) {
         if(containsKey(key)) {
-            if(key instanceof PrototypeKey<?>) {
-                return Optional.of(new FieldEntry<>((PrototypeKey<?>)key));
+            if(key instanceof GenericKey<?,?>) {
+                return Optional.of(new FieldEntry<>((GenericKey<?,?>)key));
             }
             }
             if(key instanceof String) {
             if(key instanceof String) {
                 return Optional.of(new NameEntry((String)key));
                 return Optional.of(new NameEntry((String)key));
@@ -152,7 +154,7 @@ public class PrototypeMap implements Map<Object, Object> {
         return Optional.empty();
         return Optional.empty();
     }
     }
 
 
-    public final <V> Optional<PrototypeEntry<V>> getProperty(PrototypeKey<V> key) {
+    public final <V> Optional<PrototypeEntry<V>> getProperty(GenericKey<?,V> key) {
         if(containsKey(key)) {
         if(containsKey(key)) {
             return Optional.of(new FieldEntry<>(key));
             return Optional.of(new FieldEntry<>(key));
         }
         }
@@ -167,7 +169,7 @@ public class PrototypeMap implements Map<Object, Object> {
         return containsKey((Object)key);
         return containsKey((Object)key);
     }
     }
 
 
-    public final boolean containsKey(PrototypeKey<?> key) {
+    public final boolean containsKey(GenericKey<?,?> key) {
         return containsKey((Object)key);
         return containsKey((Object)key);
     }
     }
 
 
@@ -175,22 +177,22 @@ public class PrototypeMap implements Map<Object, Object> {
         return put((Object)key, value);
         return put((Object)key, value);
     }
     }
 
 
-    public final <V> V put(PrototypeKey<V> key, V value) {
+    public final <V> V put(GenericKey<?,V> key, V value) {
         if(key == null) {
         if(key == null) {
             return null;
             return null;
         }
         }
-        return key.type().cast(put((Object)key, value));
+        return key.cast(put((Object)key, value));
     }
     }
 
 
     public final Object get(String key) {
     public final Object get(String key) {
         return get((Object)key);
         return get((Object)key);
     }
     }
 
 
-    public final <V> V get(PrototypeKey<V> key) {
+    public final <V> V get(GenericKey<?,V> key) {
         if(key == null) {
         if(key == null) {
             return null;
             return null;
         }
         }
-        return key.type().cast(get((Object)key));
+        return key.cast(get((Object)key));
     }
     }
 
 
     public final Optional<Object> getOptional(Object key) {
     public final Optional<Object> getOptional(Object key) {
@@ -201,7 +203,7 @@ public class PrototypeMap implements Map<Object, Object> {
         return Optional.ofNullable(get(key));
         return Optional.ofNullable(get(key));
     }
     }
 
 
-    public final <V> Optional<V> getOptional(PrototypeKey<V> key) {
+    public final <V> Optional<V> getOptional(GenericKey<?,V> key) {
         return Optional.ofNullable(get(key));
         return Optional.ofNullable(get(key));
     }
     }
 
 
@@ -209,11 +211,11 @@ public class PrototypeMap implements Map<Object, Object> {
         return remove((Object)key);
         return remove((Object)key);
     }
     }
 
 
-    public final <V> V remove(PrototypeKey<V> key) {
+    public final <V> V remove(GenericKey<?,V> key) {
         if(key == null) {
         if(key == null) {
             return null;
             return null;
         }
         }
-        return key.type().cast(remove((Object)key));
+        return key.cast(remove((Object)key));
     }
     }
 
 
     protected void forEachScope(Consumer<Map<Object, Object>> consumer) {
     protected void forEachScope(Consumer<Map<Object, Object>> consumer) {
@@ -226,7 +228,7 @@ public class PrototypeMap implements Map<Object, Object> {
         if(key instanceof String) {
         if(key instanceof String) {
             return new NameEntry((String) key);
             return new NameEntry((String) key);
         } else {
         } else {
-            return new FieldEntry<>((PrototypeKey<?>)key);
+            return new FieldEntry<>((GenericKey<?,?>)key);
         }
         }
     }
     }
 
 
@@ -245,8 +247,8 @@ public class PrototypeMap implements Map<Object, Object> {
             Object key = entry.getKey();
             Object key = entry.getKey();
             Object val = entry.getValue();
             Object val = entry.getValue();
 
 
-            if(key instanceof PrototypeKey<?>) {
-                PrototypeKey<?> field = (PrototypeKey<?>) key;
+            if(key instanceof GenericKey<?,?>) {
+                GenericKey<?,?> field = (GenericKey<?,?>) key;
                 if(!field.type().isInstance(val)) {
                 if(!field.type().isInstance(val)) {
                     throw new ClassCastException(val + " can't be cast to " + field.type());
                     throw new ClassCastException(val + " can't be cast to " + field.type());
                 }
                 }
@@ -370,11 +372,6 @@ public class PrototypeMap implements Map<Object, Object> {
             return IClass.OBJECT;
             return IClass.OBJECT;
         }
         }
 
 
-        @Override
-        public String getName() {
-            return name;
-        }
-
         @Override
         @Override
         public Object getKey() {
         public Object getKey() {
             return name;
             return name;
@@ -392,9 +389,9 @@ public class PrototypeMap implements Map<Object, Object> {
     }
     }
 
 
     private final class FieldEntry<V> extends AEntry<Object, V> implements PrototypeEntry<V> {
     private final class FieldEntry<V> extends AEntry<Object, V> implements PrototypeEntry<V> {
-        private final PrototypeKey<V> field;
+        private final GenericKey<?,V> field;
 
 
-        public FieldEntry(PrototypeKey<V> field) {
+        public FieldEntry(GenericKey<?,V> field) {
             this.field = field;
             this.field = field;
         }
         }
 
 
@@ -403,11 +400,6 @@ public class PrototypeMap implements Map<Object, Object> {
             return field.type();
             return field.type();
         }
         }
 
 
-        @Override
-        public String getName() {
-            return field.name();
-        }
-
         @Override
         @Override
         public Object getKey() {
         public Object getKey() {
             return field;
             return field;

+ 1 - 1
assira/src/main/java/net/ranides/assira/credentials/BasicUserStore.java

@@ -25,7 +25,7 @@ public class BasicUserStore implements UserStore {
     }
     }
 
 
     private <T> void index(GenericKey<String, T> key) {
     private <T> void index(GenericKey<String, T> key) {
-        tree.index(key.key(), key);
+        tree.index(key.id(), key);
     }
     }
 
 
     @Override
     @Override

+ 7 - 7
assira/src/main/java/net/ranides/assira/credentials/UserProperty.java

@@ -20,19 +20,19 @@ public final class UserProperty<V> {
         /* utility class */
         /* utility class */
     }
     }
     
     
-    public static final GenericKey<String,String> ACCOUNT = new GenericKey<>(String.class, "account");
+    public static final GenericKey<String,String> ACCOUNT = GenericKey.of(String.class, "account");
 
 
-    public static final GenericKey<String,String> DOMAIN = new GenericKey<>(String.class, "user.login");
+    public static final GenericKey<String,String> DOMAIN = GenericKey.of(String.class, "user.login");
     
     
-    public static final GenericKey<String,String> LOGIN = new GenericKey<>(String.class, "user.login");
+    public static final GenericKey<String,String> LOGIN = GenericKey.of(String.class, "user.login");
     
     
-    public static final GenericKey<String,String> PASSWORD = new GenericKey<>(String.class, "user.password");
+    public static final GenericKey<String,String> PASSWORD = GenericKey.of(String.class, "user.password");
     
     
-    public static final GenericKey<String,String> EMAIL = new GenericKey<>(String.class, "user.e-mail");
+    public static final GenericKey<String,String> EMAIL = GenericKey.of(String.class, "user.e-mail");
     
     
-    public static final GenericKey<String,String> HOST = new GenericKey<>(String.class, "addr.host");
+    public static final GenericKey<String,String> HOST = GenericKey.of(String.class, "addr.host");
     
     
-    public static final GenericKey<String,Integer> PORT = new GenericKey<>(Integer.class, "addr.port");
+    public static final GenericKey<String,Integer> PORT = GenericKey.of(Integer.class, "addr.port");
     
     
     public static final GenericKey<String,Predicate<URI>> URI = new GenericKey<String,Predicate<URI>>("addr.uri"){};
     public static final GenericKey<String,Predicate<URI>> URI = new GenericKey<String,Predicate<URI>>("addr.uri"){};
     
     

+ 7 - 7
assira/src/test/java/net/ranides/assira/collection/maps/GenericHashMapTest.java

@@ -9,12 +9,12 @@ import static org.junit.Assert.*;
 
 
 public class GenericHashMapTest {
 public class GenericHashMapTest {
 
 
-    GenericKey<String, Integer>      I_A = new GenericKey<>(Integer.class, "a");
-    GenericKey<String, String>       S_A = new GenericKey<>(String.class, "a");
-    GenericKey<String, Integer>      I_B = new GenericKey<>(Integer.class, "b");
-    GenericKey<String, Double>       D_C = new GenericKey<>(Double.class, "c");
-    GenericKey<String, CharSequence> C_D = new GenericKey<>(CharSequence.class, "d");
-    GenericKey<String, CharSequence> C_E = new GenericKey<>(CharSequence.class, "e");
+    GenericKey<String, Integer>      I_A = GenericKey.of(Integer.class, "a");
+    GenericKey<String, String>       S_A = GenericKey.of(String.class, "a");
+    GenericKey<String, Integer>      I_B = GenericKey.of(Integer.class, "b");
+    GenericKey<String, Double>       D_C = GenericKey.of(Double.class, "c");
+    GenericKey<String, CharSequence> C_D = GenericKey.of(CharSequence.class, "d");
+    GenericKey<String, CharSequence> C_E = GenericKey.of(CharSequence.class, "e");
 
 
     @Test
     @Test
     public void basic() {
     public void basic() {
@@ -97,7 +97,7 @@ public class GenericHashMapTest {
         map1.put(C_D, "text");
         map1.put(C_D, "text");
 
 
         assertEquals("1.5,32,45,text", map1.values().stream().map(String::valueOf).sorted().collect(Collectors.joining(",")) );
         assertEquals("1.5,32,45,text", map1.values().stream().map(String::valueOf).sorted().collect(Collectors.joining(",")) );
-        assertEquals("#a,#b,#c,#d", map1.keySet().stream().map(String::valueOf).sorted().collect(Collectors.joining(",")) );
+        assertEquals("a:Integer,b:Integer,c:Double,d:CharSequence", map1.keySet().stream().map(String::valueOf).sorted().collect(Collectors.joining(",")) );
     }
     }
 
 
     @Test
     @Test