From: jsimomaa Date: Fri, 6 Sep 2019 08:56:11 +0000 (+0300) Subject: Utilize eclipse-collections immutable lists/map in datastructures X-Git-Tag: v1.43.0~136^2~58 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=0e4c17f6848a62d5f8436233ca3524f8deabee02;p=simantics%2Fplatform.git Utilize eclipse-collections immutable lists/map in datastructures gitlab #376 Change-Id: Ic96633256050766e385ea27816684e97cf0068c2 --- diff --git a/bundles/org.simantics.utils.datastructures/META-INF/MANIFEST.MF b/bundles/org.simantics.utils.datastructures/META-INF/MANIFEST.MF index f29d42b43..1b012d999 100644 --- a/bundles/org.simantics.utils.datastructures/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.utils.datastructures/META-INF/MANIFEST.MF @@ -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 diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/MapList.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/MapList.java index cdfa9f782..8a28679b7 100644 --- a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/MapList.java +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/MapList.java @@ -14,15 +14,16 @@ */ 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. *

@@ -72,23 +73,12 @@ public class MapList { protected Map> lists; public MapList() { - lists = new HashMap>(); - } - - @SuppressWarnings("unchecked") - public MapList( Class mapClass ) { - try { - lists = (Map>) mapClass.newInstance(); - } catch (InstantiationException e) { - throw new RuntimeException( e ); - } catch (IllegalAccessException e) { - throw new RuntimeException( e ); - } + lists = Maps.mutable.empty(); } public MapList(MapList copyFrom) { for (Entry> e : copyFrom.lists.entrySet()) - lists.put( e.getKey(), new ArrayList(e.getValue()) ); + lists.put( e.getKey(), Lists.mutable.withAll(e.getValue()) ); } public static MapList use( Map> map ) { @@ -109,20 +99,20 @@ public class MapList { public void add(L key, int index, R value) { - ArrayList list = getOrCreateList(key); + List list = getOrCreateList(key); list.add(index, value); } public void addAll(L key, Collection values) { - ArrayList list = getOrCreateList(key); + List list = getOrCreateList(key); list.addAll(values); } - private ArrayList getOrCreateList(L key) + private List getOrCreateList(L key) { - ArrayList list = (ArrayList) lists.get(key); + List list = lists.get(key); if (list==null) { - list = new ArrayList(1); + list = Lists.mutable.withInitialCapacity(1); lists.put(key, list); } return list; @@ -260,7 +250,7 @@ public class MapList { { List l = lists.get(key); if (l==null) return Collections.emptyList(); - return new ArrayList(l); + return Lists.mutable.withAll(l); } public List getAllValuesSnapshot() @@ -271,7 +261,7 @@ public class MapList { public List getAllValuesSnapshot(List result) { if (result == null) - result = new ArrayList(); + result = Lists.mutable.empty(); for (List right : lists.values()) { result.addAll(right); } @@ -285,10 +275,11 @@ public class MapList { /** * Makes _this_ maplist immutable. */ + @SuppressWarnings("unchecked") public void makeImmutable() { for (Entry> e : lists.entrySet()) - lists.put(e.getKey(), Collections.unmodifiableList(e.getValue())); - lists = Collections.unmodifiableMap(lists); + lists.put(e.getKey(), (List) Lists.immutable.withAll(e.getValue())); + lists = (Map>) Maps.immutable.withAll(lists); } } diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintContext.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintContext.java index e5633b055..a725fb0d4 100644 --- a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintContext.java +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/hints/HintContext.java @@ -16,16 +16,16 @@ */ 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 hints = new THashMap(); + protected Map hints = Maps.mutable.empty(); @Override public void clearWithoutNotification() { diff --git a/features/org.simantics.utils.feature/feature.xml b/features/org.simantics.utils.feature/feature.xml index 1f7130438..0a32a1539 100644 --- a/features/org.simantics.utils.feature/feature.xml +++ b/features/org.simantics.utils.feature/feature.xml @@ -69,4 +69,18 @@ version="0.0.0" unpack="false"/> + + + +