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

develop.toolkit.struct.CollectionInMap Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package develop.toolkit.struct;

import lombok.NonNull;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

/**
 * Map里有集合结构
 *
 * @author qiushui on 2018-07-09.
 * @since 0.1
 */
public class CollectionInMap extends ConcurrentHashMap> {

	private CollectionProvider collectionProvider;

	public CollectionInMap(@NonNull CollectionProvider collectionProvider) {
		this.collectionProvider = collectionProvider;
	}

	public CollectionInMap(int initialCapacity, @NonNull CollectionProvider collectionProvider) {
		super(initialCapacity);
		this.collectionProvider = collectionProvider;
	}

	/**
	 * 追加元素
	 * @param key map key
	 * @param item 新元素
	 */
	public void addItem(K key, V item) {
		if (contains(key)) {
			Collection collection = get(key);
			collection.add(item);
		} else {
			throw new IllegalStateException("key \"" + "\" is not exist.");
		}
	}

	/**
	 * 追加元素
	 * @param key map key
	 * @param items 新元素
	 */
	public void addAllItem(K key, @NonNull Collection items) {
		if (contains(key)) {
			Collection collection = get(key);
			collection.addAll(items);
		} else {
			throw new IllegalStateException("key \"" + "\" is not exist.");
		}
	}

	/**
	 * 软追加元素
	 * @param key map key
	 * @param item 新元素
	 */
	public void addItemSoft(K key, V item) {
		if (contains(key)) {
			Collection collection = get(key);
			collection.add(item);
		} else {
			Collection collection = collectionProvider.provide(key);
			collection.add(item);
			put(key, collection);
		}
	}

	/**
	 * 软追加元素
	 * @param key map key
	 * @param items 新元素
	 */
	public void addAllItemSoft(K key, @NonNull Collection items) {
		if (contains(key)) {
			Collection collection = get(key);
			collection.addAll(items);
		} else {
			Collection collection = collectionProvider.provide(key);
			collection.addAll(items);
			put(key, collection);
		}
	}

	/**
	 * 删除元素
	 * @param key map key
	 * @param item 元素
	 */
	public void removeItem(K key, V item) {
		if (contains(key)) {
			Collection collection = get(key);
			collection.remove(item);
		} else {
			throw new IllegalStateException("key \"" + key +  "\" is not exist.");
		}
	}

	/**
	 * 根据条件删除元素
	 * @param key map key
	 * @param filter 条件
	 */
	public void removeIfItem(K key, @NonNull Predicate filter) {
		if (contains(key)) {
			Collection collection = get(key);
			collection.removeIf(filter);
		} else {
			throw new IllegalStateException("key \"" + key + "\" is not exist.");
		}
	}

	/**
	 * 集合提供器
	 * @param 
	 */
	@FunctionalInterface
	public interface CollectionProvider {

		Collection provide(K key);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy