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

org.apache.wicket.util.collections.MultiMap Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.wicket.util.collections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple multimap
 * 
 * @author igor
 * @param 
 * @param 
 */
public class MultiMap extends HashMap>
{
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor
	 * 
	 * @see HashMap#HashMap()
	 */
	public MultiMap()
	{
	}

	/**
	 * Constructor
	 * 
	 * @param initialCapacity
	 * @param loadFactor
	 * 
	 * @see HashMap#HashMap(int, float)
	 */
	public MultiMap(final int initialCapacity, final float loadFactor)
	{
		super(initialCapacity, loadFactor);
	}

	/**
	 * Constructor
	 * 
	 * @param initialCapacity
	 * 
	 * @see HashMap#HashMap(int)
	 */
	public MultiMap(final int initialCapacity)
	{
		super(initialCapacity);
	}

	/**
	 * Constructor
	 * 
	 * @param m
	 * 
	 * @see HashMap#HashMap(Map)
	 */
	public MultiMap(final Map> m)
	{
		super(m);
	}

	/**
	 * Adds value to the specified key
	 * 
	 * @param key
	 * @param value
	 */
	public void addValue(final K key, final V value)
	{
		List list = get(key);
		if (list == null)
		{
			list = new ArrayList<>(1);
			put(key, list);
		}
		list.add(value);
	}

	/**
	 * Removes value from the specified key
	 * 
	 * @param key
	 * @param value
	 */
	public void removeValue(final K key, final V value)
	{
		List list = get(key);
		if (list != null)
		{
			list.remove(value);
		}
	}

	/**
	 * Replaces all existing values with the specified value. If no values exist for the key the
	 * value will be added.
	 * 
	 * @param key
	 * @param value
	 */
	public void replaceValues(final K key, final V value)
	{
		List list = get(key);
		if (list != null)
		{
			list.clear();
			list.add(value);
		}
		else
		{
			addValue(key, value);
		}
	}

	/**
	 * Gets the first value in the value list
	 * 
	 * @param key
	 * @return first value
	 */
	public V getFirstValue(final K key)
	{
		List list = get(key);
		if ((list != null) && !list.isEmpty())
		{
			return list.get(0);
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy