-/*******************************************************************************\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;\r
-\r
-import java.util.Collections;\r
-import java.util.Comparator;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.TreeMap;\r
-\r
-/**\r
- * MapSet is an associative data structure a key type on the left side (L) and a\r
- * set element type of the right side.\r
- * \r
- * <p>\r
- * Based on {@link MapList} by Toni Kalajainen.\r
- * </p>\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public abstract class MapSet<L, R> {\r
-\r
- protected Map<L, Set<R>> sets;\r
- \r
- public static class Hash<L, R> extends MapSet<L, R> {\r
- public Hash() {\r
- sets = new HashMap<L, Set<R>>();\r
- }\r
- protected Set<R> getOrCreateSet(L key) {\r
- Set<R> set = sets.get(key);\r
- if (set == null) {\r
- set = new HashSet<R>();\r
- sets.put(key, set);\r
- }\r
- return set;\r
- }\r
- }\r
-\r
- public static class Tree<L, R> extends MapSet<L, R> {\r
- public Tree() {\r
- sets = new TreeMap<L, Set<R>>();\r
- }\r
- public Tree(Comparator<? super L> comparator) {\r
- sets = new TreeMap<L, Set<R>>(comparator);\r
- }\r
- protected Set<R> getOrCreateSet(L key) {\r
- Set<R> set = sets.get(key);\r
- if (set == null) {\r
- set = new HashSet<R>();\r
- sets.put(key, set);\r
- }\r
- return set;\r
- }\r
- }\r
- \r
- public boolean add(L key, R value) {\r
- Set<R> set = getOrCreateSet(key);\r
- return set.add(value);\r
- }\r
-\r
- protected abstract Set<R> getOrCreateSet(L key);\r
-\r
- private Set<R> getSet(L key) {\r
- return sets.get(key);\r
- }\r
-\r
- /**\r
- * @param key\r
- * @return a valid set, empty if no values exist\r
- */\r
- public Set<R> removeValues(L key) {\r
- Set<R> set = sets.remove(key);\r
- if (set == null)\r
- return Collections.emptySet();\r
- return set;\r
- }\r
-\r
- public boolean remove(L key, R value) {\r
- Set<R> set = getSet(key);\r
- if (set == null)\r
- return false;\r
- boolean result = set.remove(value);\r
- if (set.isEmpty())\r
- sets.remove(key);\r
- return result;\r
- }\r
-\r
- public void clear() {\r
- sets.clear();\r
- }\r
-\r
- public L[] getKeys(L[] list) {\r
- return sets.keySet().toArray(list);\r
- }\r
-\r
- public Set<L> getKeys() {\r
- return sets.keySet();\r
- }\r
-\r
- public boolean hasValues(L key) {\r
- return sets.containsKey(key);\r
- }\r
-\r
- public R[] getValues(L key, R[] list) {\r
- Set<R> l = sets.get(key);\r
- if (l == null)\r
- return null;\r
- return l.toArray(list);\r
- }\r
-\r
- public Set<R> getValues(L key) {\r
- Set<R> l = sets.get(key);\r
- if (l == null)\r
- return null;\r
- return new HashSet<R>(l);\r
- }\r
-\r
- public Set<R> getValuesUnsafe(L key) {\r
- return sets.get(key);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.utils.datastructures;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+/**
+ * MapSet is an associative data structure a key type on the left side (L) and a
+ * set element type of the right side.
+ *
+ * <p>
+ * Based on {@link MapList} by Toni Kalajainen.
+ * </p>
+ *
+ * @author Tuukka Lehtonen
+ */
+public abstract class MapSet<L, R> {
+
+ protected Map<L, Set<R>> sets;
+
+ public static class Hash<L, R> extends MapSet<L, R> {
+ public Hash() {
+ sets = new HashMap<L, Set<R>>();
+ }
+ protected Set<R> getOrCreateSet(L key) {
+ Set<R> set = sets.get(key);
+ if (set == null) {
+ set = new HashSet<R>();
+ sets.put(key, set);
+ }
+ return set;
+ }
+ }
+
+ public static class Tree<L, R> extends MapSet<L, R> {
+ public Tree() {
+ sets = new TreeMap<L, Set<R>>();
+ }
+ public Tree(Comparator<? super L> comparator) {
+ sets = new TreeMap<L, Set<R>>(comparator);
+ }
+ protected Set<R> getOrCreateSet(L key) {
+ Set<R> set = sets.get(key);
+ if (set == null) {
+ set = new HashSet<R>();
+ sets.put(key, set);
+ }
+ return set;
+ }
+ }
+
+ public boolean add(L key, R value) {
+ Set<R> set = getOrCreateSet(key);
+ return set.add(value);
+ }
+
+ protected abstract Set<R> getOrCreateSet(L key);
+
+ private Set<R> getSet(L key) {
+ return sets.get(key);
+ }
+
+ /**
+ * @param key
+ * @return a valid set, empty if no values exist
+ */
+ public Set<R> removeValues(L key) {
+ Set<R> set = sets.remove(key);
+ if (set == null)
+ return Collections.emptySet();
+ return set;
+ }
+
+ public boolean remove(L key, R value) {
+ Set<R> set = getSet(key);
+ if (set == null)
+ return false;
+ boolean result = set.remove(value);
+ if (set.isEmpty())
+ sets.remove(key);
+ return result;
+ }
+
+ public void clear() {
+ sets.clear();
+ }
+
+ public L[] getKeys(L[] list) {
+ return sets.keySet().toArray(list);
+ }
+
+ public Set<L> getKeys() {
+ return sets.keySet();
+ }
+
+ public boolean hasValues(L key) {
+ return sets.containsKey(key);
+ }
+
+ public R[] getValues(L key, R[] list) {
+ Set<R> l = sets.get(key);
+ if (l == null)
+ return null;
+ return l.toArray(list);
+ }
+
+ public Set<R> getValues(L key) {
+ Set<R> l = sets.get(key);
+ if (l == null)
+ return null;
+ return new HashSet<R>(l);
+ }
+
+ public Set<R> getValuesUnsafe(L key) {
+ return sets.get(key);
+ }
+
+}