org.httprpc.kilo.util.Collections Maven / Gradle / Ivy
/*
* 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.httprpc.kilo.util;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Provides static utility methods for working with collections.
*/
public class Collections {
private Collections() {
}
/**
* Creates a list of elements.
*
* @param
* The element type.
*
* @param elements
* The list elements.
*
* @return
* A mutable, random-access list containing the provided elements in the
* order given.
*/
@SafeVarargs
public static List listOf(E... elements) {
var list = new ArrayList(elements.length);
java.util.Collections.addAll(list, elements);
return list;
}
/**
* Creates an immutable list of elements.
*
* @param
* The element type.
*
* @param elements
* The list elements.
*
* @return
* An immutable, random-access list containing the provided elements in the
* order given.
*/
@SafeVarargs
public static List immutableListOf(E... elements) {
return java.util.Collections.unmodifiableList(listOf(elements));
}
/**
* Creates a map of entries.
*
* @param
* The key type.
*
* @param
* The value type.
*
* @param entries
* The map entries.
*
* @return
* A mutable map containing the provided entries in the order given.
*/
@SafeVarargs
public static Map mapOf(Map.Entry... entries) {
var map = new LinkedHashMap();
for (var entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
/**
* Creates an immutable map of entries.
*
* @param
* The key type.
*
* @param
* The value type.
*
* @param entries
* The map entries.
*
* @return
* An immutable map containing the provided entries in the order given.
*/
@SafeVarargs
public static Map immutableMapOf(Map.Entry... entries) {
return java.util.Collections.unmodifiableMap(mapOf(entries));
}
/**
* Creates an immutable map entry.
*
* @param
* The key type.
*
* @param
* The value type.
*
* @param key
* The entry key.
*
* @param value
* The entry value.
*
* @return
* An immutable map entry containing the provided key/value pair.
*/
public static Map.Entry entry(K key, V value) {
return new AbstractMap.SimpleImmutableEntry<>(key, value);
}
/**
* Creates a set of elements.
*
* @param
* The element type.
*
* @param elements
* The set elements.
*
* @return
* A mutable set containing the provided elements.
*/
@SafeVarargs
public static Set setOf(E... elements) {
var set = new LinkedHashSet(elements.length);
java.util.Collections.addAll(set, elements);
return set;
}
/**
* Creates an immutable set of elements.
*
* @param
* The element type.
*
* @param elements
* The set elements.
*
* @return
* An immutable set containing the provided elements.
*/
@SafeVarargs
public static Set immutableSetOf(E... elements) {
return java.util.Collections.unmodifiableSet(setOf(elements));
}
/**
* Returns an empty list.
*
* @param
* The element type.
*
* @param elementType
* The element type.
*
* @return
* An empty list.
*/
public static List emptyListOf(Class elementType) {
if (elementType == null) {
throw new IllegalArgumentException();
}
return immutableListOf();
}
/**
* Returns an empty map.
*
* @param
* The key type.
*
* @param
* The value type.
*
* @param keyType
* The key type.
*
* @param valueType
* The value type.
*
* @return
* An empty map.
*/
public static Map emptyMapOf(Class keyType, Class valueType) {
if (keyType == null || valueType == null) {
throw new IllegalArgumentException();
}
return immutableMapOf();
}
/**
* Returns an empty set.
*
* @param
* The element type.
*
* @param elementType
* The element type.
*
* @return
* An empty set.
*/
public static Set emptySetOf(Class elementType) {
if (elementType == null) {
throw new IllegalArgumentException();
}
return immutableSetOf();
}
}