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

at.molindo.utils.collections.SetMap Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/**
 * Copyright 2010 Molindo GmbH
 *
 * 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 at.molindo.utils.collections;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class SetMap implements Map> {

	private final Map> _map;

	public static  SetMap newSetMap() {
		return new SetMap();
	}

	public SetMap() {
		_map = newMap();
	}

	protected Map> newMap() {
		return new HashMap>();
	}

	protected Map> getMap() {
		return _map;
	}

	@Override
	public void clear() {
		_map.clear();
	}

	@Override
	public boolean containsKey(final Object key) {
		return _map.containsKey(key);
	}

	@Override
	public boolean containsValue(final Object value) {
		return _map.containsValue(value);
	}

	@Override
	public Set>> entrySet() {
		return _map.entrySet();
	}

	@Override
	public boolean equals(final Object o) {
		return _map.equals(o);
	}

	@Override
	public Set get(final Object key) {
		return _map.get(key);
	}

	@Override
	public int hashCode() {
		return _map.hashCode();
	}

	@Override
	public boolean isEmpty() {
		return _map.isEmpty();
	}

	@Override
	public Set keySet() {
		return _map.keySet();
	}

	public void add(final T key, final E value) {
		getSet(key).add(value);
	}

	public boolean addAll(final T key, final Collection values) {
		return getSet(key).addAll(values);
	}

	@Override
	public Set put(final T key, final Set value) {
		return _map.put(key, value);
	}

	@Override
	public void putAll(final Map> m) {
		_map.putAll(m);
	}

	@Override
	public Set remove(final Object key) {
		return _map.remove(key);
	}

	public boolean removeValue(final T key, final E value) {
		final Set set = _map.get(key);
		return set == null ? false : set.remove(value);
	}

	public boolean removeAll(final T key, final Collection values) {
		final Set set = _map.get(key);
		return set == null ? false : set.removeAll(values);
	}

	@Override
	public int size() {
		return _map.size();
	}

	@Override
	public Collection> values() {
		return _map.values();
	}

	@Override
	public String toString() {
		return SetMap.class.getSimpleName() + ": " + _map;
	}

	public Set getSet(final T key) {
		Set set = _map.get(key);
		if (set == null) {
			set = new LinkedHashSet();
			_map.put(key, set);
		}
		return set;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy