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

java.util.Collections Maven / Gradle / Ivy

/*
 * Copyright 2016 Carlos Ballesteros Velasco
 *
 * 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 java.util;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Collections {
    private Collections() {
    }

    native public static > void sort(List list);

    native public static  void sort(List list, Comparator c);

    native public static  int binarySearch(List> list, T key);

    native public static  int binarySearch(List list, T key, Comparator c);

    public static void reverse(List list) {
        int size = list.size();
        for (int i = 0, mid = size >> 1, j = size - 1; i < mid; i++, j--) {
            swap(list, i, j);
        }
    }

    native public static void shuffle(List list);

    native public static void shuffle(List list, Random rnd);

    public static void swap(List list, int i, int j) {
        final List l = list;
        l.set(i, l.set(j, l.get(i)));
    }

    public static  void fill(List list, T obj) {
        int length = list.size();
        for (int n = 0; n < length; n++) list.set(n, obj);
    }

    native public static  void copy(List dest, List src);

    native public static > T min(Collection coll);

    native public static  T min(Collection coll, Comparator comp);

    native public static > T max(Collection coll);

    native public static  T max(Collection coll, Comparator comp);

    public static void rotate(List list, int distance) {
        int length = list.size();
        for (int n = 0; n < length; n++) {
            swap(list, n, (n + distance) % length);
        }
    }

    public static  boolean replaceAll(List list, T oldVal, T newVal) {
        int length = list.size();
        int count = 0;
        for (int n = 0; n < length; n++) {
            if (Objects.equals(list.get(n), oldVal)) {
                list.set(n, newVal);
                count++;
            }
        }
        return count > 0;
    }

    native public static int indexOfSubList(List source, List target);

    native public static int lastIndexOfSubList(List source, List target);

    public static  Collection unmodifiableCollection(Collection c) {
        return (Collection) c;
    }

    public static  Set unmodifiableSet(Set s) {
        return (Set) s;
    }

    public static  SortedSet unmodifiableSortedSet(SortedSet s) {
        return s;
    }

    public static  NavigableSet unmodifiableNavigableSet(NavigableSet s) {
        return s;
    }

    public static  List unmodifiableList(List list) {
        return (List) list;
    }

    public static  Map unmodifiableMap(Map m) {
        return (Map) m;
    }

    public static  SortedMap unmodifiableSortedMap(SortedMap m) {
        return (SortedMap) m;
    }

    public static  NavigableMap unmodifiableNavigableMap(NavigableMap m) {
        return (NavigableMap) m;
    }

    public static  Collection synchronizedCollection(Collection c) {
        return c;
    }

    public static  Set synchronizedSet(Set s) {
        return s;
    }

    public static  SortedSet synchronizedSortedSet(SortedSet s) {
        return s;
    }

    public static  NavigableSet synchronizedNavigableSet(NavigableSet s) {
        return s;
    }

    static  List synchronizedList(List list, Object mutex) {
        return (list instanceof RandomAccess ? list : list);
    }
    public static  Map synchronizedMap(Map m) {
        return m;
    }

    public static  SortedMap synchronizedSortedMap(SortedMap m) {
        return m;
    }

    public static  NavigableMap synchronizedNavigableMap(NavigableMap m) {
        return m;
    }

    native public static  Collection checkedCollection(Collection c, Class type);

    native public static  Queue checkedQueue(Queue queue, Class type);

    native public static  Set checkedSet(Set s, Class type);

    native public static  SortedSet checkedSortedSet(SortedSet s, Class type);

    native public static  NavigableSet checkedNavigableSet(NavigableSet s, Class type);

    native public static  List checkedList(List list, Class type);

    native public static  Map checkedMap(Map m, Class keyType, Class valueType);

    native public static  SortedMap checkedSortedMap(SortedMap m, Class keyType, Class valueType);

    native public static  NavigableMap checkedNavigableMap(NavigableMap m, Class keyType, Class valueType);

    static private List empty = new ArrayList();
    public static  Iterator emptyIterator() {
        return (Iterator) empty.iterator();
    }

    public static  ListIterator emptyListIterator() {
        return (ListIterator) empty.listIterator();
    }

    native public static  Enumeration emptyEnumeration();

    // @TODO: Make those immutable
    public static final Set EMPTY_SET = new HashSet();
    public static final List EMPTY_LIST = new ArrayList();
    public static final Map EMPTY_MAP = new HashMap();

    native public static final  Set emptySet();

    native public static  SortedSet emptySortedSet();

    native public static  NavigableSet emptyNavigableSet();

    native public static final  List emptyList();

    native public static final  Map emptyMap();

    native public static final  SortedMap emptySortedMap();

    native public static final  NavigableMap emptyNavigableMap();

    native public static  Set singleton(T o);

    native static  Iterator singletonIterator(final E e);

    //native static  Spliterator singletonSpliterator(final T element);

    public static  List singletonList(T o) {
        return nCopies(1, o);
    }

    public static  Map singletonMap(K key, V value) {
        HashMap ts = new HashMap(1);
        ts.put(key, value);
        return ts;
    }

    public static  List nCopies(int count, T o) {
        ArrayList ts = new ArrayList(count);
        for (int n = 0; n < count; n++) ts.add(o);
        return ts;
    }

    native public static  Comparator reverseOrder();

    native public static  Comparator reverseOrder(Comparator cmp);

    native public static  Enumeration enumeration(final Collection c);

    native public static  ArrayList list(Enumeration e);

    native static boolean eq(Object o1, Object o2);

    native public static int frequency(Collection c, Object o);

    native public static boolean disjoint(Collection c1, Collection c2);

    public static  boolean addAll(Collection c, T... elements) {
        boolean out = false;
        for (T e : elements) out |= c.add(e);
        return out;
    }

    native public static  Set newSetFromMap(Map map);

    native public static  Queue asLifoQueue(Deque deque);
}