]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/MapList.java
Utilize eclipse-collections immutable lists/map in datastructures
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / MapList.java
index cdfa9f7829ddbfb4065d95f9c4ef27cf774e306d..8a28679b747cb860fd6dc1129bfa4c5f0fc627cc 100644 (file)
  */
 package org.simantics.utils.datastructures;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import org.eclipse.collections.impl.factory.Lists;
+import org.eclipse.collections.impl.factory.Maps;
+
 /**
  * MapList is a data structure with map on left side and arraylist on right side.
  * <p>
@@ -72,23 +73,12 @@ public class MapList<L, R> {
     protected Map<L, List<R>> lists;
 
     public MapList() {
-       lists = new HashMap<L, List<R>>();
-    }
-    
-    @SuppressWarnings("unchecked")
-       public MapList( Class<?> mapClass ) {
-       try {
-                       lists = (Map<L, List<R>>) mapClass.newInstance();
-               } catch (InstantiationException e) {
-                       throw new RuntimeException( e );
-               } catch (IllegalAccessException e) {
-                       throw new RuntimeException( e );
-               }
+        lists = Maps.mutable.empty();
     }
 
     public MapList(MapList<L, R> copyFrom) {
         for (Entry<L, List<R>> e : copyFrom.lists.entrySet())
-            lists.put( e.getKey(), new ArrayList<R>(e.getValue()) );
+            lists.put( e.getKey(), Lists.mutable.withAll(e.getValue()) );
     }
 
        public static <L, R> MapList<L, R> use( Map<L, List<R>> map ) {
@@ -109,20 +99,20 @@ public class MapList<L, R> {
 
     public void add(L key, int index, R value)
     {
-        ArrayList<R> list = getOrCreateList(key);
+        List<R> list = getOrCreateList(key);
         list.add(index, value);
     }
 
        public void addAll(L key, Collection<R> values) {
-               ArrayList<R> list = getOrCreateList(key);
+               List<R> list = getOrCreateList(key);
                list.addAll(values);
        }
     
-    private ArrayList<R> getOrCreateList(L key)
+    private List<R> getOrCreateList(L key)
     {
-        ArrayList<R> list = (ArrayList<R>) lists.get(key);
+        List<R> list = lists.get(key);
         if (list==null) {
-            list = new ArrayList<R>(1);
+            list = Lists.mutable.withInitialCapacity(1);
             lists.put(key, list);
         }
         return list;
@@ -260,7 +250,7 @@ public class MapList<L, R> {
     {
         List<R> l = lists.get(key);
         if (l==null) return Collections.emptyList();
-        return new ArrayList<R>(l);
+        return Lists.mutable.withAll(l);
     }
     
     public List<R> getAllValuesSnapshot() 
@@ -271,7 +261,7 @@ public class MapList<L, R> {
     public List<R> getAllValuesSnapshot(List<R> result) 
     {
         if (result == null)
-            result = new ArrayList<R>();
+            result = Lists.mutable.empty();
         for (List<R> right : lists.values()) {
             result.addAll(right);
         }
@@ -285,10 +275,11 @@ public class MapList<L, R> {
     /**
      * Makes _this_ maplist immutable.
      */
+    @SuppressWarnings("unchecked")
     public void makeImmutable() {
         for (Entry<L, List<R>> e : lists.entrySet())
-            lists.put(e.getKey(), Collections.unmodifiableList(e.getValue()));
-        lists = Collections.unmodifiableMap(lists);
+            lists.put(e.getKey(), (List<R>) Lists.immutable.withAll(e.getValue()));
+        lists = (Map<L, List<R>>) Maps.immutable.withAll(lists);
     }
 
 }