Utilize eclipse-collections immutable lists/map in datastructures 13/3213/3
authorjsimomaa <jani.simomaa@gmail.com>
Fri, 6 Sep 2019 08:56:11 +0000 (11:56 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Wed, 2 Oct 2019 11:19:55 +0000 (14:19 +0300)
gitlab #376

Change-Id: Ic96633256050766e385ea27816684e97cf0068c2

bundles/org.simantics.utils.datastructures/META-INF/MANIFEST.MF
bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/MapList.java
bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintContext.java
features/org.simantics.utils.feature/feature.xml

index f29d42b4392aaaa5542dc86c1a1c8e567820d1bf..1b012d999f0be311d10a06ee9287d28a2030d4ca 100644 (file)
@@ -20,6 +20,8 @@ Require-Bundle: org.simantics.utils.thread;bundle-version="1.0.0";visibility:=re
  org.simantics.utils;bundle-version="1.0.0",
  gnu.trove3;bundle-version="3.0.0",
  org.simantics.databoard;bundle-version="0.6.5";resolution:=optional,
- org.slf4j.api;bundle-version="1.7.25"
+ org.slf4j.api;bundle-version="1.7.25",
+ org.eclipse.collections.eclipse-collections;bundle-version="9.2.0",
+ org.eclipse.collections.eclipse-collections-api;bundle-version="9.2.0"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Automatic-Module-Name: org.simantics.utils.datastructures
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);
     }
 
 }
index e5633b05539f201ad08720922310037893de88a7..a725fb0d42fa41f185965b406adafff543641ca8 100644 (file)
  */
 package org.simantics.utils.datastructures.hints;
 
-import gnu.trove.map.hash.THashMap;
-
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.Map.Entry;
+import java.util.Set;
+
+import org.eclipse.collections.impl.factory.Maps;
 
 /**
  * 
@@ -33,7 +33,7 @@ import java.util.Map.Entry;
  */
 public class HintContext extends AbstractHintObservable implements IHintContext, Cloneable {
 
-    protected Map<Key, Object> hints = new THashMap<Key, Object>();
+    protected Map<Key, Object> hints = Maps.mutable.empty();
 
     @Override
     public void clearWithoutNotification() {
index 1f71304389839698ca1308b308da0f897f37142b..0a32a1539296294b373a0710b176bc8bde85080d 100644 (file)
          version="0.0.0"
          unpack="false"/>
 
+   <plugin
+         id="org.eclipse.collections.eclipse-collections"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.eclipse.collections.eclipse-collections-api"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
 </feature>