com.virjar.ratel.api.inspect.Lists Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ratel-api Show documentation
Show all versions of ratel-api Show documentation
ratel api,used for developer on ratel system,an extension for xposed framewrok,ratel api compatable with original xposed framework
package com.virjar.ratel.api.inspect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* some utils migrated from guava
*/
public class Lists {
public static ArrayList newArrayList(E... elements) {
// Avoid integer overflow when a large array is passed in
int capacity = computeArrayListCapacity(elements.length);
ArrayList list = new ArrayList(capacity);
Collections.addAll(list, elements);
return list;
}
static int computeArrayListCapacity(int arraySize) {
return saturatedCast(5L + arraySize + (arraySize / 10));
}
/**
* Returns the {@code int} nearest in value to {@code value}.
*
* @param value any {@code long} value
* @return the same value cast to {@code int} if it is in the range of the
* {@code int} type, {@link Integer#MAX_VALUE} if it is too large,
* or {@link Integer#MIN_VALUE} if it is too small
*/
public static int saturatedCast(long value) {
if (value > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (value < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
return (int) value;
}
public static LinkedList newLinkedList() {
return new LinkedList();
}
static Collection cast(Iterable iterable) {
return (Collection) iterable;
}
public static CopyOnWriteArrayList newCopyOnWriteArrayList(
Iterable extends E> elements) {
// We copy elements to an ArrayList first, rather than incurring the
// quadratic cost of adding them to the COWAL directly.
Collection extends E> elementsCollection =
(elements instanceof Collection)
? cast(elements)
: newArrayList(elements);
return new CopyOnWriteArrayList(elementsCollection);
}
public static ArrayList newArrayList(Iterable extends E> elements) {
// Let ArrayList's sizing logic work, if possible
return (elements instanceof Collection)
? new ArrayList(cast(elements))
: newArrayList(elements.iterator());
}
public static ArrayList newArrayList(Iterator extends E> elements) {
ArrayList list = newArrayList();
addAll(list, elements);
return list;
}
public static boolean addAll(Collection addTo, Iterator extends T> iterator) {
boolean wasModified = false;
while (iterator.hasNext()) {
wasModified |= addTo.add(iterator.next());
}
return wasModified;
}
public static List newArrayListWithCapacity(int length) {
return new ArrayList<>(length);
}
}