java.util.Collections Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dragome-js-jre Show documentation
Show all versions of dragome-js-jre Show documentation
Dragome SDK module: js-jre
The newest version!
/*
* Copyright (c) 2011-2014 Fernando Petrola
*
* 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;
public class Collections
{
public static List checkedList(List list, Class type)
{
return list;
}
private static final class EmptySet extends HashSet
{
private static final long serialVersionUID= 1582296315990362920L;
public boolean contains(Object object)
{
return false;
}
public int size()
{
return 0;
}
public Iterator iterator()
{
return new Iterator()
{
public boolean hasNext()
{
return false;
}
public Object next()
{
throw new NoSuchElementException();
}
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
private Object readResolve()
{
return Collections.EMPTY_SET;
}
}
public static final Set EMPTY_SET= new EmptySet();
private static class IteratorEnumarator implements Enumeration
{
Iterator iterator;
IteratorEnumarator(Iterator theIterator)
{
iterator= theIterator;
}
public boolean hasMoreElements()
{
return iterator.hasNext();
}
public E nextElement()
{
if (!iterator.hasNext())
{
throw new NoSuchElementException();
}
return iterator.next();
}
}
/**
* Returns an enumeration over the specified collection. This provides interoperatbility with legacy APIs that require an enumeration as input.
*/
public static Enumeration enumeration(Collection c)
{
return new IteratorEnumarator(c.iterator());
}
private static Random r= new Random();
/**
* Randomly permutes the specified list using a default source of randomness.
*/
public static void shuffle(List> list)
{
int size= list.size();
for (int i= size; i > 1; i--)
{
swap(list, i - 1, r.nextInt(i));
}
}
/**
* Sorts the specified list into ascending order, according to the natural ordering of its elements.
*
* Important: This is a restriction of the Java API sort(List) function.
*/
public static void sort(List list)
{
String[] array= (String[]) list.toArray();
Arrays.sort(array);
int count= array.length;
list.clear();
for (int i= 0; i < count; i++)
{
list.add(array[i]);
}
}
/**
* Swaps the two specified elements in the specified array.
*/
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 Map unmodifiableMap(Map extends K, ? extends V> map)
{
if (map == null)
{
throw new NullPointerException();
}
return (Map) map;
}
public static final Map emptyMap()
{
return (Map) new HashMap();
}
public static final List emptyList()
{
return (List) new ArrayList();
}
public static final Set emptySet()
{
return (Set) EMPTY_SET;
}
public static List unmodifiableList(List extends T> list)
{
return (List) list;
}
public static Collection unmodifiableCollection(Collection extends T> collection)
{
return (Collection) collection;
}
public static void reverse(List> list)
{
int size= list.size();
ListIterator fwd= list.listIterator();
ListIterator rev= list.listIterator(size);
for (int i= 0, mid= list.size() >> 1; i < mid; i++)
{
Object tmp= fwd.next();
fwd.set(rev.previous());
rev.set(tmp);
}
}
public static void sort(List list, Comparator super T> c)
{
Object[] a= list.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator i= list.listIterator();
for (int j= 0; j < a.length; j++)
{
i.next();
i.set(a[j]);
}
}
public static List synchronizedList(List list)
{
return list;
}
public static boolean addAll(Collection super T> c, T... elements) {
boolean result = false;
for (T element : elements)
result |= c.add(element);
return result;
}
}