All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.container.map.set.SetMap Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.map.set;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * Default implementation of {@link ISetMap}.
 *
 * @author Oliver Richers
 */
public class SetMap, V extends Comparable> implements ISetMap {

	private final Map> setMap;

	public SetMap() {

		this.setMap = new TreeMap<>();
	}

	public SetMap(Comparator comparator) {

		this.setMap = new TreeMap<>(comparator);
	}

	@Override
	public void addAllToSet(K key, Collection values) {

		TreeSet set = setMap.get(key);
		if (set == null) {
			setMap.put(key, set = new TreeSet<>());
		}
		set.addAll(values);
	}

	@Override
	public boolean addToSet(K key, V value) {

		TreeSet set = setMap.get(key);
		if (set == null) {
			setMap.put(key, set = new TreeSet<>());
		}
		return set.add(value);
	}

	@Override
	public void clear() {

		this.setMap.clear();
	}

	@Override
	public boolean contains(K key, V value) {

		TreeSet set = setMap.get(key);
		return set != null && set.contains(value);
	}

	@Override
	public boolean containsKey(K key) {

		return setMap.containsKey(key);
	}

	public Set>> entrySet() {

		return setMap.entrySet();
	}

	@Override
	public boolean equals(Object object) {

		if (object instanceof SetMap) {
			return ((SetMap) object).setMap.equals(this.setMap);
		} else {
			return false;
		}
	}

	@Override
	public TreeSet getSet(K key) {

		TreeSet set = setMap.get(key);
		if (set == null) {
			setMap.put(key, set = new TreeSet<>());
		}
		return set;
	}

	@Override
	public Collection> getSets() {

		return Collections.unmodifiableCollection(setMap.values());
	}

	public Set getSetsContent() {

		Set contentOfAllSets = new TreeSet<>();
		for (TreeSet set: setMap.values()) {
			for (V v: set) {
				contentOfAllSets.add(v);
			}
		}
		return contentOfAllSets;
	}

	@Override
	public int hashCode() {

		return setMap.hashCode();
	}

	@Override
	public boolean isEmpty() {

		return setMap.isEmpty();
	}

	@Override
	public Set keySet() {

		return setMap.keySet();
	}

	@Override
	public void remove(K key) {

		setMap.remove(key);
	}

	@Override
	public boolean remove(K key, V value) {

		TreeSet set = setMap.get(key);
		return set != null && set.remove(value);
	}

	@Override
	public int size() {

		return setMap.size();
	}

	@Override
	public String toString() {

		return setMap.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy