From 0ae5350ce690b42fe1bc8f571cfe85e1d0e4efbe Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 26 Aug 2016 16:42:45 +0200 Subject: [PATCH] [New] Added new collection MapOfSets --- .../strolch/utils/collections/MapOfSets.java | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java new file mode 100644 index 000000000..1e3b3d3fd --- /dev/null +++ b/li.strolch.utils/src/main/java/li/strolch/utils/collections/MapOfSets.java @@ -0,0 +1,123 @@ +/* + * Copyright 2016 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.utils.collections; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +/** + * @author Robert von Burg + */ +public class MapOfSets { + + private Map> mapOfSets; + + public MapOfSets() { + this.mapOfSets = new HashMap<>(); + } + + public Set keySet() { + return this.mapOfSets.keySet(); + } + + public Set getSet(T t) { + return this.mapOfSets.get(t); + } + + public boolean addElement(T t, U u) { + Set set = this.mapOfSets.get(t); + if (set == null) { + set = new HashSet<>(); + this.mapOfSets.put(t, set); + } + return set.add(u); + } + + public boolean addSet(T t, Set u) { + Set set = this.mapOfSets.get(t); + if (set == null) { + set = new HashSet<>(); + this.mapOfSets.put(t, set); + } + return set.addAll(u); + } + + public boolean removeElement(T t, U u) { + Set set = this.mapOfSets.get(t); + if (set == null) { + return false; + } + boolean removed = set.remove(u); + if (set.isEmpty()) { + this.mapOfSets.remove(t); + } + + return removed; + } + + public Set removeSet(T t) { + return this.mapOfSets.remove(t); + } + + public void clear() { + Set>> entrySet = this.mapOfSets.entrySet(); + Iterator>> iter = entrySet.iterator(); + while (iter.hasNext()) { + iter.next().getValue().clear(); + iter.remove(); + } + } + + public boolean containsSet(T t) { + return this.mapOfSets.containsKey(t); + } + + public boolean containsElement(T t, U u) { + Set set = this.mapOfSets.get(t); + if (set == null) + return false; + return set.contains(u); + } + + public int sizeKeys() { + return this.mapOfSets.size(); + } + + public int size() { + int size = 0; + Set>> entrySet = this.mapOfSets.entrySet(); + Iterator>> iter = entrySet.iterator(); + while (iter.hasNext()) { + size += iter.next().getValue().size(); + } + return size; + } + + public int size(T t) { + Set set = this.mapOfSets.get(t); + if (set.size() == 0) + return 0; + return set.size(); + } + + public boolean isEmpty() { + return this.mapOfSets.isEmpty(); + } +}