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.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
/**
* 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 listOf();
}
/**
* 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 mapOf();
}
/**
* 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 setOf();
}
/**
* Returns the index of the first element in a list that matches the given
* predicate.
*
* @param
* The element type.
*
* @param list
* The list of elements.
*
* @param predicate
* The predicate.
*
* @return
* The index of the first matching element, or {@code -1} if no match was
* found.
*/
public static int firstIndexWhere(List list, Predicate predicate) {
var iterator = list.iterator();
var i = 0;
while (iterator.hasNext()) {
if (predicate.test(iterator.next())) {
return i;
}
i++;
}
return -1;
}
/**
* Returns the index of the last element in a list that matches the given
* predicate.
*
* @param
* The element type.
*
* @param list
* The list of elements.
*
* @param predicate
* The predicate.
*
* @return
* The index of the last matching element, or {@code -1} if no match was
* found.
*/
public static int lastIndexWhere(List list, Predicate predicate) {
var i = list.size();
var iterator = list.listIterator(i);
while (iterator.hasPrevious()) {
i--;
if (predicate.test(iterator.previous())) {
return i;
}
}
return -1;
}
/**
* Returns the value at a given path.
*
* @param root
* The root object.
*
* @param path
* The path to the value.
*
* @return
* The value at the given path, or {@code null} if the value does not
* exist.
*/
public static Object valueAt(Object root, Object... path) {
return valueAt(root, Arrays.asList(path));
}
/**
* Returns the value at a given path.
*
* @param root
* The root object.
*
* @param path
* The path to the value.
*
* @return
* The value at the given path, or {@code null} if the value does not
* exist.
*/
public static Object valueAt(Object root, List> path) {
if (root == null) {
return null;
} else {
if (path == null) {
throw new IllegalArgumentException();
}
if (path.isEmpty()) {
return root;
} else {
var component = path.get(0);
Object value;
if (root instanceof List> list && component instanceof Number number) {
value = list.get(number.intValue());
} else if (root instanceof Map, ?> map) {
value = map.get(component);
} else {
throw new IllegalArgumentException("Unsupported type.");
}
return valueAt(value, path.subList(1, path.size()));
}
}
}
}