org.elasticsearch.common.util.CollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.common.util;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Iterators;
import java.nio.file.Path;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.RandomAccess;
import java.util.Set;
/** Collections-related utility methods. */
public class CollectionUtils {
/**
* Checks if the given array contains any elements.
*
* @param array The array to check
*
* @return false if the array contains an element, true if not or the array is null.
*/
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}
/**
* Eliminate duplicates from a sorted list in-place.
*
* @param list A sorted list, which will be modified in place.
* @param cmp A comparator the list is already sorted by.
*/
public static void uniquify(List list, Comparator cmp) {
if (list.size() <= 1) {
return;
}
ListIterator uniqueItr = list.listIterator();
ListIterator existingItr = list.listIterator();
T uniqueValue = uniqueItr.next(); // get first element to compare with
existingItr.next(); // advance the existing iterator to the second element, where we will begin comparing
do {
T existingValue = existingItr.next();
if (cmp.compare(existingValue, uniqueValue) != 0 && (uniqueValue = uniqueItr.next()) != existingValue) {
uniqueItr.set(existingValue);
}
} while (existingItr.hasNext());
// Lop off the rest of the list. Note with LinkedList this requires advancing back to this index,
// but Java provides no way to efficiently remove from the end of a non random-access list.
list.subList(uniqueItr.nextIndex(), list.size()).clear();
}
/**
* Return a rotated view of the given list with the given distance.
*/
public static List rotate(final List list, int distance) {
if (list.isEmpty()) {
return list;
}
int d = distance % list.size();
if (d < 0) {
d += list.size();
}
if (d == 0) {
return list;
}
return new RotatedList<>(list, d);
}
public static int[] toArray(Collection ints) {
Objects.requireNonNull(ints);
return ints.stream().mapToInt(s -> s).toArray();
}
/**
* Deeply inspects a Map, Iterable, or Object array looking for references back to itself.
* @throws IllegalArgumentException if a self-reference is found
* @param value The object to evaluate looking for self references
* @param messageHint A string to be included in the exception message if the call fails, to provide
* more context to the handler of the exception
*/
public static void ensureNoSelfReferences(Object value, String messageHint) {
Iterable it = convert(value);
if (it != null) {
ensureNoSelfReferences(it, value, Collections.newSetFromMap(new IdentityHashMap<>()), messageHint);
}
}
@SuppressWarnings("unchecked")
private static Iterable convert(Object value) {
if (value == null) {
return null;
}
if (value instanceof Map map) {
return () -> Iterators.concat(map.keySet().iterator(), map.values().iterator());
} else if ((value instanceof Iterable) && (value instanceof Path == false)) {
return (Iterable) value;
} else if (value instanceof Object[]) {
return Arrays.asList((Object[]) value);
} else {
return null;
}
}
private static void ensureNoSelfReferences(
final Iterable value,
Object originalReference,
final Set