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

at.molindo.utils.collections.MapMap 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.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class MapMap implements Map> {

	private final Map> _map;

	public static  MapMap newMapMap() {
		return new MapMap();
	}

	public MapMap() {
		_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 Map get(final Object key) {
		return _map.get(key);
	}

	public V get(final K1 key1, K2 key2) {
		Map map = _map.get(key1);
		return map == null ? null : map.get(key2);
	}

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

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

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

	public V put(final K1 key1, final K2 key2, V value) {
		return getMap(key1).put(key2, value);
	}

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

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

	public void putAll(final K1 key, final Map values) {
		getMap(key).putAll(values);
	}

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

	public V removeValue(final K1 key, final K2 value) {
		final Map map = _map.get(key);
		return map == null ? null : map.remove(value);
	}

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

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

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

	public Map getMap(final K1 key) {
		Map map = _map.get(key);
		if (map == null) {
			map = new LinkedHashMap();
			_map.put(key, map);
		}
		return map;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy