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

com.virjar.ratel.api.inspect.Lists Maven / Gradle / Ivy

Go to download

ratel api,used for developer on ratel system,an extension for xposed framewrok,ratel api compatable with original xposed framework

There is a newer version: 1.3.6
Show newest version
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 elements) {
        // We copy elements to an ArrayList first, rather than incurring the
        // quadratic cost of adding them to the COWAL directly.
        Collection elementsCollection =
                (elements instanceof Collection)
                        ? cast(elements)
                        : newArrayList(elements);
        return new CopyOnWriteArrayList(elementsCollection);
    }

    public static  ArrayList newArrayList(Iterable 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 elements) {
        ArrayList list = newArrayList();
        addAll(list, elements);
        return list;
    }

    public static  boolean addAll(Collection addTo, Iterator iterator) {
        boolean wasModified = false;
        while (iterator.hasNext()) {
            wasModified |= addTo.add(iterator.next());
        }
        return wasModified;
    }

    public static  List newArrayListWithCapacity(int length) {
        return new ArrayList<>(length);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy