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

org.conqat.lib.commons.collections.ListMap Maven / Gradle / Ivy

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE 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 org.conqat.lib.commons.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.conqat.lib.commons.assertion.CCSMAssert;

/**
 * Manages a map of lists, i.e. each key can store multiple elements.
 * 
 * @param 
 *            the key type.
 * @param 
 *            the value type (i.e. the values stored in the collections).
 */
public class ListMap extends CollectionMap> {

	private static final long serialVersionUID = 1L;

	/** Create new hashed list map. */
	public ListMap() {
		// Nothing to do
	}

	/** Copy constructor. */
	public ListMap(ListMap other) {
		this();
		addAll(other);
	}

	/** Copy constructor. */
	public ListMap(Map> other) {
		this();
		addAll(other);
	}

	/** Adds the value only if the key and value are not null */
	public boolean addIfNotNull(K key, V value) {
		if (key != null && value != null) {
			return add(key, value);
		}
		return true;
	}

	/**
	 * Creates a list map from a string array provided it's length is even
	 */
	public static ListMap of(String... keysAndValues) {
		CCSMAssert.isTrue(keysAndValues.length % 2 == 0, "Expecting even number of arguments.");
		ListMap listMap = new ListMap<>();
		for (int i = 0; i < keysAndValues.length; i += 2) {
			listMap.add(keysAndValues[i], keysAndValues[i + 1]);
		}

		return listMap;
	}

	/**
	 * Creates a list map from string, string pairs, where pairs are only added when both elements of
	 * the pair are not empty
	 */
	public static ListMap fromNonEmptyPairs(Pair... keysAndValuePairs) {
		ListMap map = new ListMap<>();
		Arrays.stream(keysAndValuePairs).filter(Objects::nonNull) //
				.filter(opt -> opt.getFirst() != null) //
				.filter(opt -> opt.getSecond() != null) //
				.forEach(opt -> map.add(opt.getFirst().toString(), opt.getSecond().toString()));
		return map;
	}

	/**
	 * Creates a new {@link ListMap} of the given key/values.
	 */
	public static  ListMap of(K key, Collection values) {
		ListMap listMap = new ListMap<>();
		listMap.addAll(key, values);
		return listMap;
	}

	/**
	 * Create a new, empty {@link ListMap}.
	 */
	public static  ListMap emptyMap() {
		return new ListMap<>();
	}

	/** {@inheritDoc} */
	@Override
	protected List createNewCollection() {
		return new ArrayList<>();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy