]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/collections/CollectionUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / collections / CollectionUtils.java
diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/collections/CollectionUtils.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/collections/CollectionUtils.java
new file mode 100644 (file)
index 0000000..d74467b
--- /dev/null
@@ -0,0 +1,271 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.datastructures.collections;\r
+\r
+import gnu.trove.map.hash.TObjectIntHashMap;\r
+import gnu.trove.set.hash.THashSet;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class CollectionUtils {\r
+\r
+    /**\r
+     * Add the elements in <code>toBeAdded</code> to addTo. The only difference\r
+     * to {@link Collection#addAll(Collection)} is that <code>toBeAdded</code>\r
+     * may be null.\r
+     * \r
+     * @param <T>\r
+     * @param toBeAdded collection of elements to be added to <code>addTo</code> or\r
+     *        <code>null</code> to do nothing\r
+     * @param addTo the collection the elements should be added to\r
+     * @return <code>true</code> if the destination collection changed as a\r
+     *         result of the operation\r
+     */\r
+    public static <T> boolean checkedAdd(Collection<T> toBeAdded, Collection<T> addTo) {\r
+        assert addTo != null;\r
+        if (toBeAdded == null)\r
+            return false;\r
+        return addTo.addAll(toBeAdded);\r
+    }\r
+\r
+    /**\r
+     * @return the accumulated size of all the specified collections\r
+     */\r
+    public static int sizeAll(Collection<?>... collections) {\r
+        int size = 0;\r
+        for (Collection<?> c : collections)\r
+            size += c.size();\r
+        return size;\r
+    }\r
+\r
+    /**\r
+     * @return a new ArrayList<T> that sequentially contains all the elements in\r
+     *         the specified collections\r
+     */\r
+    public static <T> Collection<T> join(@SuppressWarnings("unchecked") Collection<T>... collections) {\r
+        int size = sizeAll(collections);\r
+        ArrayList<T> result = new ArrayList<T>(size);\r
+        for (Collection<T> c : collections)\r
+            result.addAll(c);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * @return a new HashSet<T> that contains once all the elements in the\r
+     *         specified collections\r
+     */\r
+    public static <T> Set<T> join(@SuppressWarnings("unchecked") Set<T>... collections) {\r
+        int size = sizeAll(collections);\r
+        HashSet<T> result = new HashSet<T>(size);\r
+        for (Collection<T> c : collections)\r
+            result.addAll(c);\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * A specialization of {@link #join(Collection...)} for two arguments.\r
+     */\r
+    public static <T> Collection<T> join(Collection<T> first, Collection<T> second) {\r
+        int size = first.size();\r
+        size += second.size();\r
+        ArrayList<T> result = new ArrayList<T>(size);\r
+        result.addAll(first);\r
+        result.addAll(second);\r
+        return result;\r
+    }\r
+    \r
+    static class PairFirstComparator<K extends Comparable<K>> implements Comparator<Pair<K, ?>> {\r
+\r
+               @Override\r
+               public int compare(Pair<K,?> o1, Pair<K,?> o2) {\r
+                       return o1.first.compareTo(o2.first);\r
+               }\r
+       \r
+    }\r
+    \r
+    static class PairSecondComparator<V extends Comparable<V>> implements Comparator<Pair<?, V>> {\r
+\r
+               @Override\r
+               public int compare(Pair<?, V> o1, Pair<?, V> o2) {\r
+                       return o1.second.compareTo(o2.second);\r
+               }\r
+       \r
+     }    \r
+    \r
+    public static <K> Collection<Pair<K,Integer>> valueSortedEntries(TObjectIntHashMap<K> map) {\r
+       \r
+       ArrayList<Pair<K,Integer>> result = new ArrayList<Pair<K,Integer>>();\r
+       for(K key : map.keySet()) result.add(new Pair<K,Integer>(key, map.get(key)));\r
+       Collections.sort(result, new PairSecondComparator<Integer>());\r
+       return result;\r
+       \r
+    }\r
+\r
+    public static <K, V extends Comparable<V>> List<Pair<K,V>> valueSortedEntries(Map<K,V> map) {\r
+       \r
+       ArrayList<Pair<K,V>> result = new ArrayList<Pair<K,V>>(); \r
+       for(Map.Entry<K, V> entry : map.entrySet()) result.add(new Pair<K,V>(entry.getKey(), entry.getValue()));\r
+       Collections.sort(result, new PairSecondComparator<V>());\r
+       return result;\r
+       \r
+    }\r
+    \r
+    public static <K extends Comparable<K>, V> List<V> sortedBy(List<K> keys, List<V> values) {\r
+\r
+       ArrayList<Pair<V,K>> work = new ArrayList<Pair<V,K>>();\r
+       for(int i=0;i<keys.size();i++) work.add(Pair.make(values.get(i), keys.get(i)));\r
+       Collections.sort(work, new PairSecondComparator<K>());\r
+       ArrayList<V> result = new ArrayList<V>();\r
+       for(Pair<V, K> pair : work) result.add(pair.first);\r
+       return result;\r
+\r
+    }\r
+\r
+    public static <K extends Comparable<K>,V> Collection<V> sortByFirst(List<Pair<K,V>> collection) {\r
+       Collections.sort(collection, new PairFirstComparator<K>());\r
+       ArrayList<V> values = new ArrayList<V>();\r
+       for(Pair<K,V> pair : collection) values.add(pair.second);\r
+       return values;\r
+    }\r
+    \r
+    /**\r
+     * A specialization of {@link #join(Set...)} for two arguments.\r
+     */\r
+    public static <T> Set<T> join(Set<T> first, Set<T> second) {\r
+        int size = first.size();\r
+        size += second.size();\r
+        HashSet<T> result = new HashSet<T>(size);\r
+        result.addAll(first);\r
+        result.addAll(second);\r
+        return result;\r
+    }\r
+\r
+    /*\r
+     * Some tests.\r
+     */\r
+    @SuppressWarnings({ "unused", "unchecked" })\r
+    public static void main(String[] args) {\r
+        Collection<String> c1 = Collections.singleton("FOO");\r
+        Collection<String> c2 = Collections.singleton("BAR");\r
+        Collection<String> c3 = Collections.singleton("BAZ");\r
+        Collection<String> c4 = Collections.singleton("FOO");\r
+\r
+        Collection<String> c123 = join(c1, c2, c3);\r
+        Collection<String> c12 = join(c1, c2);\r
+\r
+        Set<String> s1 = Collections.singleton("FOO");\r
+        Set<String> s2 = Collections.singleton("BAR");\r
+        Set<String> s3 = Collections.singleton("BAZ");\r
+        Set<String> s4 = Collections.singleton("FOO");\r
+\r
+        Set<String> s123 = join(s1, s2, s3);\r
+        Set<String> s12 = join(s1, s2);\r
+        Set<String> s14 = join(s1, s4);\r
+    }\r
+\r
+    public static <T> void toggle(Set<T> src, Set<T> toggleSet)\r
+    {\r
+       for (T i : toggleSet)\r
+       {\r
+               if (src.contains(i))\r
+                       src.remove(i);\r
+               else \r
+                       src.add(i);\r
+       }\r
+    }\r
+\r
+    public static <T> List<T> toList(@SuppressWarnings("unchecked") T...objs) \r
+    {\r
+       ArrayList<T> result = new ArrayList<T>(objs.length);\r
+       for (T o : objs)\r
+               result.add(o);\r
+       return result;\r
+    }\r
+\r
+    public static <T> Set<T> toSet(@SuppressWarnings("unchecked") T...objs)\r
+    {\r
+       THashSet<T> result = new THashSet<T>(objs.length);\r
+       for (T o : objs)\r
+               result.add(o);\r
+       result.compact();\r
+       return result;\r
+    }\r
+    \r
+    /**\r
+     * Remove elements that appear more than once. Keep order otherwise.\r
+     * @param list to be pruned\r
+     */\r
+    public static void unique(List<?> list) {\r
+       int c = list.size();\r
+       int i = c-1;\r
+       while (i>0) {\r
+               Object o = list.get(i);\r
+               int index = list.indexOf(o);\r
+               if (index>=0) {\r
+                       list.remove(index);\r
+                       c--;\r
+                       i--;\r
+               } else {\r
+                       i--;\r
+               }\r
+       }\r
+    }\r
+\r
+    public static <T> T element(Collection<T> collection, int n) {\r
+       \r
+        if (collection instanceof List<?>)\r
+            return ((List<T>) collection).get(n);\r
+       if(n >= collection.size()) throw new IllegalArgumentException();\r
+       Iterator<T> it = collection.iterator();\r
+       for( int i=0;i<n-1;i++ ) it.next();\r
+       return it.next();\r
+       \r
+    }\r
+    \r
+    public static String toString(Collection<?> collection, String separator) {\r
+       StringBuilder sb = new StringBuilder();\r
+       \r
+       int index = 0;\r
+       for (Object o : collection) {\r
+               if ( index++>0 ) sb.append(separator);\r
+               sb.append( o.toString() );\r
+       }\r
+       \r
+       return sb.toString();\r
+    }\r
+    \r
+    public static String toString(Object[] array, String separator) {\r
+       StringBuilder sb = new StringBuilder();\r
+       \r
+       int index = 0;\r
+       for (Object o : array) {\r
+               if ( index++>0 ) sb.append(separator);\r
+               sb.append( o.toString() );\r
+       }\r
+       \r
+       return sb.toString();\r
+    }\r
+    \r
+}\r