Ranides Atterwim 9 anni fa
parent
commit
9d79409ec2

+ 11 - 0
assira.drafts/src/main/java/net/ranides/assira/collections/map/IndexMap.java

@@ -8,6 +8,7 @@ package net.ranides.assira.collections.map;
 
 import java.util.Collection;
 import java.util.Comparator;
+import java.util.function.Consumer;
 
 /**
  *
@@ -19,6 +20,8 @@ public interface IndexMap<V> {
     
     <T> IndexMap<V> index(String name, IndexComparator<V,T> index);
     
+    
+    
     IndexQuery<V> find();
 
     IndexQuery<V> find(Comparator<V> index, V value);
@@ -27,10 +30,18 @@ public interface IndexMap<V> {
 
     IndexQuery<V> find(String index, V value);
 
+    
+    
     void put(V value);
 
     void put(Collection<? extends V> values);
 
+    void update(V value, Consumer<? super V> consumer);
+    
+    void updateAll();
+    
+    
+    
     void remove(Comparator<V> index, V value);
     
     <T> void remove(IndexComparator<V,T> index, T value);

+ 5 - 0
assira.drafts/src/main/java/net/ranides/assira/collections/map/IndexQuery.java

@@ -15,6 +15,7 @@ import net.ranides.assira.collection.query.CQuery;
  * @author Ranides Atterwim <ranides@gmail.com>
  */
 public interface IndexQuery<V> extends CQuery<V> {
+    
 
     IndexQuery<V> eq(Comparator<V> index, V value);
     
@@ -23,6 +24,7 @@ public interface IndexQuery<V> extends CQuery<V> {
     IndexQuery<V> eq(String index, Object value);
     
 
+    
     IndexQuery<V> gt(Comparator<V> index, V value);
     
     <T> IndexQuery<V> gt(IndexComparator<V,T> index, T value);
@@ -30,12 +32,14 @@ public interface IndexQuery<V> extends CQuery<V> {
     IndexQuery<V> gt(String index, Object value);
     
     
+    
     IndexQuery<V> lt(Comparator<V> index, V value);
     
     <T> IndexQuery<V> lt(IndexComparator<V,T> index, T value);
 
     IndexQuery<V> lt(String index, Object value);
     
+    
 
     IndexQuery<V> in(Comparator<V> index, V begin, V end);
     
@@ -44,6 +48,7 @@ public interface IndexQuery<V> extends CQuery<V> {
     IndexQuery<V> in(String index, Object begin, Object end);
 
     
+    
     void remove();
 
     void update(Consumer<? super V> consumer);

+ 7 - 4
assira.drafts/src/main/java/net/ranides/assira/collections/map/impl/AIndexMap.java

@@ -67,12 +67,15 @@ public abstract class AIndexMap<V> implements IndexMap<V> {
         }
     }
     
+    @Override
+    public void update(V value, Consumer<? super V> consumer) {
+        remove(value);
+        consumer.accept(value);
+        put(value);
+    }
+    
     protected abstract <T> IndexComparator<V,T> mindex(String name);
     
     protected abstract Comparator<V> sindex(String name);
     
-    protected abstract void update(V value, Consumer<? super V> consumer);
-    
-    protected abstract void update();
-    
 }

+ 4 - 0
assira.drafts/src/main/java/net/ranides/assira/collections/map/impl/AIndexQuery.java

@@ -22,21 +22,25 @@ public abstract class AIndexQuery<V> extends CQueryAbstract<V> implements IndexQ
     
     protected abstract AIndexMap<V> parent();
     
+    @SuppressWarnings("unchecked")
     @Override
     public final IndexQuery<V> eq(String index, Object value) {
         return filter(index, a -> eq(a,value), a -> eq(a,(V)value));
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public final IndexQuery<V> lt(String index, Object value) {
         return filter(index, a -> lt(a,value), a -> lt(a,(V)value));
     }
     
+    @SuppressWarnings("unchecked")
     @Override
     public final IndexQuery<V> gt(String index, Object value) {
         return filter(index, a -> gt(a,value), a -> gt(a,(V)value));
     }
     
+    @SuppressWarnings("unchecked")
     @Override
     public final IndexQuery<V> in(String index, Object begin, Object end) {
         return filter(index, a -> in(a,begin,end), a -> in(a,(V)begin,(V)end));

+ 14 - 18
assira.drafts/src/main/java/net/ranides/assira/collections/map/impl/CIndexTreeMap.java

@@ -17,7 +17,6 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.SortedMap;
 import java.util.SortedSet;
-import java.util.function.Consumer;
 import java.util.function.Supplier;
 import java.util.stream.Stream;
 import net.ranides.assira.collection.maps.ASortedMap;
@@ -65,34 +64,29 @@ public class CIndexTreeMap<V> extends AIndexMap<V> {
         return this;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected <T> IndexComparator<V, T> mindex(String name) {
         return name2mindex.get(name);
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     protected Comparator<V> sindex(String name) {
         return name2sindex.get(name);
     }
-
-    @Override
-    protected void update(V value, Consumer<? super V> consumer) {
-        remove(value);
-        consumer.accept(value);
-        put(value);
-    }
     
     @Override
     public void remove(V value) {
         for(SortedSet<V> index : cindexes.values()) {
-            $remove(index, value);
+            CIndexTreeMap.this.iremove(index, value);
         }
         for(SortedMap<Object,V> index : mindexes.values()) {
-            CIndexTreeMap.this.$remove(index, value);
+            iremove(index, value);
         }
     }
     
-    void $remove(SortedSet<V> set, V value) {
+    private void iremove(SortedSet<V> set, V value) {
         Comparator<? super V> c = set.comparator();
         Iterator<V> i = set.headSet(value).iterator();
         while(i.hasNext()) {
@@ -107,7 +101,7 @@ public class CIndexTreeMap<V> extends AIndexMap<V> {
         }
     }
     
-    void $remove(SortedMap<Object,V> map, V value) {
+    private void iremove(SortedMap<Object,V> map, V value) {
         Comparator<Object> c = CompareUtils.comparator();
         Iterator<Entry<Object,V>> i = map.headMap(value).entrySet().iterator();
         while(i.hasNext()) {
@@ -122,8 +116,9 @@ public class CIndexTreeMap<V> extends AIndexMap<V> {
         }
     }
     
+    @SuppressWarnings("unchecked")
     @Override
-    protected void update() {
+    public void updateAll() {
         List<V> items = new ArrayList<>(values());
         for(SortedSet<V> index : cindexes.values()) {
             index.clear();
@@ -145,6 +140,7 @@ public class CIndexTreeMap<V> extends AIndexMap<V> {
         return new CHQuery();
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public void put(V value) {
         for(SortedSet<V> index : cindexes.values()) {
@@ -171,7 +167,7 @@ public class CIndexTreeMap<V> extends AIndexMap<V> {
         return cindexes.values().iterator().next();
     }
 
-    private Object after(ASortedMap<Object, V> map, Object value) {
+    private static Object after(ASortedMap<Object, ?> map, Object value) {
         Comparator<Object> cmp = CompareUtils.comparator();
         Iterator<Object> i = map.keySet().iterator(value);
         while(i.hasNext()) {
@@ -183,11 +179,11 @@ public class CIndexTreeMap<V> extends AIndexMap<V> {
         return null;
     }
     
-    private V after(ASortedSet<V> set, V value) {
-        Comparator<? super V> cmp = set.comparator();
-        Iterator<V> i = set.iterator(value);
+    private static <T> T after(ASortedSet<T> set, T value) {
+        Comparator<? super T> cmp = set.comparator();
+        Iterator<T> i = set.iterator(value);
         while(i.hasNext()) {
-            V next = i.next();
+            T next = i.next();
             if(cmp.compare(value, next)!=0) {
                 return next;
             }

+ 0 - 2
assira.drafts/src/test/java/net/ranides/assira/collections/map/impl/CIndexTreeMapTest.java

@@ -10,7 +10,6 @@ import net.ranides.assira.collections.map.IndexMap;
 import net.ranides.assira.generic.CompareUtils;
 import org.junit.Test;
 import static org.junit.Assert.*;
-import org.junit.Ignore;
 
 /**
  *
@@ -18,7 +17,6 @@ import org.junit.Ignore;
  */
 public class CIndexTreeMapTest {
     
-    @Ignore
     @Test
     public void testSetIndex() {