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

org.rapidoid.commons.Coll Maven / Gradle / Ivy

There is a newer version: 5.5.5
Show newest version
package org.rapidoid.commons;

import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.u.U;

import java.util.*;
import java.util.concurrent.*;

/*
 * #%L
 * rapidoid-commons
 * %%
 * Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
 * %%
 * 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.
 * #L%
 */

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class Coll extends RapidoidThing {
	@SuppressWarnings("unchecked")
	public static  Set synchronizedSet(T... values) {
		return (Set) Collections.synchronizedSet(U.set(values));
	}

	@SuppressWarnings("unchecked")
	public static  List synchronizedList(T... values) {
		return (List) Collections.synchronizedList(U.list(values));
	}

	public static  Set concurrentSet() {
		return Collections.newSetFromMap(new ConcurrentHashMap());
	}

	public static boolean isMap(Object obj) {
		return obj instanceof Map;
	}

	public static boolean isList(Object obj) {
		return obj instanceof List;
	}

	public static boolean isSet(Object obj) {
		return obj instanceof Set;
	}

	public static boolean isCollection(Object obj) {
		return obj instanceof Collection;
	}

	public static  void assign(Collection destination, Collection source) {
		Err.argMust(destination != null, "destination cannot be null!");

		destination.clear();

		if (source != null) {
			destination.addAll(source);
		}
	}

	public static  void assign(Collection destination, T[] source) {
		Err.argMust(destination != null, "destination cannot be null!");

		destination.clear();

		if (source != null) {
			Collections.addAll(destination, source);
		}
	}

	public static  void assign(Map destination, Map source) {
		Err.argMust(destination != null, "destination cannot be null!");

		destination.clear();

		if (source != null) {
			destination.putAll(source);
		}
	}

	public static  V get(Map map, K key) {
		V value = map.get(key);
		U.notNull(value, "map[%s]", key);
		return value;
	}

	public static  V get(Map map, K key, V defaultValue) {
		V value = map.get(key);
		return value != null ? value : defaultValue;
	}

	public static  ConcurrentMap concurrentMap() {
		return new ConcurrentHashMap();
	}

	public static  ConcurrentMap concurrentMap(Map src, boolean ignoreNullValues) {
		ConcurrentMap map = concurrentMap();

		for (Map.Entry e : src.entrySet()) {
			if (!ignoreNullValues || e.getValue() != null) {
				map.put(e.getKey(), e.getValue());
			}
		}

		return map;
	}

	public static  ConcurrentMap concurrentMap(K key, V value) {
		ConcurrentMap map = concurrentMap();
		map.put(key, value);
		return map;
	}

	public static  ConcurrentMap concurrentMap(K key1, V value1, K key2, V value2) {
		ConcurrentMap map = concurrentMap(key1, value1);
		map.put(key2, value2);
		return map;
	}

	public static  ConcurrentMap concurrentMap(K key1, V value1, K key2, V value2, K key3, V value3) {
		ConcurrentMap map = concurrentMap(key1, value1, key2, value2);
		map.put(key3, value3);
		return map;
	}

	public static  ConcurrentMap concurrentMap(K key1, V value1, K key2, V value2, K key3, V value3,
	                                                       K key4, V value4) {
		ConcurrentMap map = concurrentMap(key1, value1, key2, value2, key3, value3);
		map.put(key4, value4);
		return map;
	}

	public static  ConcurrentMap concurrentMap(K key1, V value1, K key2, V value2, K key3, V value3,
	                                                       K key4, V value4, K key5, V value5) {
		ConcurrentMap map = concurrentMap(key1, value1, key2, value2, key3, value3, key4, value4);
		map.put(key5, value5);
		return map;
	}

	@SuppressWarnings("unchecked")
	public static  ConcurrentMap concurrentMap(Object... keysAndValues) {
		U.must(keysAndValues.length % 2 == 0, "Incorrect number of arguments (expected key-value pairs)!");

		ConcurrentMap map = concurrentMap();

		for (int i = 0; i < keysAndValues.length / 2; i++) {
			map.put((K) keysAndValues[i * 2], (V) keysAndValues[i * 2 + 1]);
		}

		return map;
	}

	public static  Map orderedMap() {
		return new LinkedHashMap();
	}

	public static  Map orderedMap(Map src, boolean ignoreNullValues) {
		Map map = orderedMap();

		for (Map.Entry e : src.entrySet()) {
			if (!ignoreNullValues || e.getValue() != null) {
				map.put(e.getKey(), e.getValue());
			}
		}

		return map;
	}

	public static  Map orderedMap(K key, V value) {
		Map map = orderedMap();
		map.put(key, value);
		return map;
	}

	public static  Map orderedMap(K key1, V value1, K key2, V value2) {
		Map map = orderedMap(key1, value1);
		map.put(key2, value2);
		return map;
	}

	public static  Map orderedMap(K key1, V value1, K key2, V value2, K key3, V value3) {
		Map map = orderedMap(key1, value1, key2, value2);
		map.put(key3, value3);
		return map;
	}

	public static  Map orderedMap(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) {
		Map map = orderedMap(key1, value1, key2, value2, key3, value3);
		map.put(key4, value4);
		return map;
	}

	public static  Map orderedMap(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4,
	                                          K key5, V value5) {
		Map map = orderedMap(key1, value1, key2, value2, key3, value3, key4, value4);
		map.put(key5, value5);
		return map;
	}

	@SuppressWarnings("unchecked")
	public static  Map orderedMap(Object... keysAndValues) {
		U.must(keysAndValues.length % 2 == 0, "Incorrect number of arguments (expected key-value pairs)!");

		Map map = orderedMap();

		for (int i = 0; i < keysAndValues.length / 2; i++) {
			map.put((K) keysAndValues[i * 2], (V) keysAndValues[i * 2 + 1]);
		}

		return map;
	}

	public static  Map synchronizedMap() {
		return Collections.synchronizedMap(U.map());
	}

	public static  Queue queue() {
		return new ConcurrentLinkedQueue();
	}

	public static  BlockingQueue queue(int maxSize) {
		Err.argMust(maxSize > 0, "Maximum queue size must be > 0!");
		return new ArrayBlockingQueue(maxSize);
	}

	public static  Map autoExpandingMap(final Class clazz) {
		return autoExpandingMap(new Mapper() {

			@SuppressWarnings("unchecked")
			@Override
			public V map(K src) throws Exception {
				try {
					return (V) clazz.newInstance();
				} catch (Exception e) {
					throw U.rte(e);
				}
			}
		});
	}

	@SuppressWarnings("serial")
	public static  Map autoExpandingMap(Mapper valueFactory) {
		return new AutoExpandingMap(valueFactory);
	}

	public static  Map> mapOfMaps() {
		return autoExpandingMap(new Mapper>() {

			@Override
			public Map map(K1 src) throws Exception {
				return synchronizedMap();
			}

		});
	}

	public static  Map>> mapOfMapOfMaps() {
		return autoExpandingMap(new Mapper>>() {

			@Override
			public Map> map(K1 src) throws Exception {
				return mapOfMaps();
			}

		});
	}

	public static  Map> mapOfLists() {
		return autoExpandingMap(new Mapper>() {

			@SuppressWarnings("unchecked")
			@Override
			public List map(K src) throws Exception {
				return synchronizedList();
			}

		});
	}

	public static  Map>> mapOfMapOfLists() {
		return autoExpandingMap(new Mapper>>() {

			@Override
			public Map> map(K1 src) throws Exception {
				return mapOfLists();
			}

		});
	}

	public static  Map> mapOfSets() {
		return autoExpandingMap(new Mapper>() {

			@SuppressWarnings("unchecked")
			@Override
			public Set map(K src) throws Exception {
				return synchronizedSet();
			}

		});
	}

	public static  Map>> mapOfMapOfSets() {
		return autoExpandingMap(new Mapper>>() {

			@Override
			public Map> map(K1 src) throws Exception {
				return mapOfSets();
			}

		});
	}

	public static  List range(Iterable items, int from, int to) {
		U.must(from <= to, "'from' must be <= 'to'!");

		if (from == to) {
			return Collections.emptyList();
		}

		if (items instanceof DataItems) {
			DataItems dataItems = (DataItems) items;
			return dataItems.page(from, to - from);
		}

		List list = (items instanceof List) ? (List) items : U.list(items);
		to = Math.min(to, list.size());
		return U.cast(list.subList(from, to));
	}

	public static Integer getSizeOrNull(Iterable items) {
		return (items instanceof Collection) ? ((Collection) items).size() : null;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy