Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2008 Google Inc.
*
* 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.Serializable;
/**
* Utility methods that operate on collections. [Sun
* docs]
*/
public class Collections {
private static final class EmptyList extends AbstractList implements
RandomAccess, Serializable {
@Override
public boolean contains(Object object) {
return false;
}
@Override
public Object get(int location) {
throw new IndexOutOfBoundsException();
}
@Override
public int size() {
return 0;
}
}
private static final class EmptySet extends AbstractSet implements
Serializable {
@Override
public boolean contains(Object object) {
return false;
}
@Override
public Iterator iterator() {
return new Iterator() {
public boolean hasNext() {
return false;
}
public Object next() {
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public int size() {
return 0;
}
}
private static final class EmptyMap extends AbstractMap implements
Serializable {
@Override
public boolean containsKey(Object key) {
return false;
}
@Override
public boolean containsValue(Object value) {
return false;
}
@Override
public Set entrySet() {
return EMPTY_SET;
}
@Override
public Object get(Object key) {
return null;
}
@Override
public Set keySet() {
return EMPTY_SET;
}
@Override
public int size() {
return 0;
}
@Override
public Collection values() {
return EMPTY_LIST;
}
}
private static final class SingletonList extends AbstractList implements Serializable {
private E element;
public SingletonList(E element) {
this.element = element;
}
public boolean contains(Object item) {
return Utility.equalsWithNullCheck(element, item);
}
public E get(int index) {
if (index == 0) {
return element;
} else {
throw new IndexOutOfBoundsException();
}
}
public int size() {
return 1;
}
}
/*
* TODO: make the unmodifiable collections serializable.
*/
static class UnmodifiableCollection implements Collection {
protected final Collection extends T> coll;
public UnmodifiableCollection(Collection extends T> coll) {
this.coll = coll;
}
public boolean add(T o) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection extends T> c) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}
public boolean contains(Object o) {
return coll.contains(o);
}
public boolean containsAll(Collection> c) {
return coll.containsAll(c);
}
public boolean isEmpty() {
return coll.isEmpty();
}
public Iterator iterator() {
return new UnmodifiableCollectionIterator(coll.iterator());
}
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection> c) {
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection> c) {
throw new UnsupportedOperationException();
}
public int size() {
return coll.size();
}
public Object[] toArray() {
return coll.toArray();
}
public E[] toArray(E[] a) {
return coll.toArray(a);
}
public String toString() {
return coll.toString();
}
}
static class UnmodifiableList extends UnmodifiableCollection implements
List {
private final List extends T> list;
public UnmodifiableList(List extends T> list) {
super(list);
this.list = list;
}
public void add(int index, T element) {
throw new UnsupportedOperationException();
}
public boolean addAll(int index, Collection extends T> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o) {
return list.equals(o);
}
public T get(int index) {
return list.get(index);
}
@Override
public int hashCode() {
return list.hashCode();
}
public int indexOf(Object o) {
return list.indexOf(o);
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}
public ListIterator listIterator() {
return listIterator(0);
}
public ListIterator listIterator(int from) {
return new UnmodifiableListIterator(list.listIterator(from));
}
public T remove(int index) {
throw new UnsupportedOperationException();
}
public T set(int index, T element) {
throw new UnsupportedOperationException();
}
public List subList(int fromIndex, int toIndex) {
return new UnmodifiableList(list.subList(fromIndex, toIndex));
}
}
static class UnmodifiableMap implements Map {
static class UnmodifiableEntrySet extends
UnmodifiableSet> {
private static class UnmodifiableEntry implements Map.Entry {
private Map.Entry extends K, ? extends V> entry;
public UnmodifiableEntry(Map.Entry extends K, ? extends V> entry) {
this.entry = entry;
}
@Override
public boolean equals(Object o) {
return entry.equals(o);
}
public K getKey() {
return entry.getKey();
}
public V getValue() {
return entry.getValue();
}
@Override
public int hashCode() {
return entry.hashCode();
}
public V setValue(V value) {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return entry.toString();
}
}
@SuppressWarnings("unchecked")
public UnmodifiableEntrySet(
Set extends Map.Entry extends K, ? extends V>> s) {
super((Set extends Entry>) s);
}
@Override
public boolean contains(Object o) {
return coll.contains(o);
}
@Override
public boolean containsAll(Collection> o) {
return coll.containsAll(o);
}
@Override
@SuppressWarnings("unchecked")
public Iterator> iterator() {
final Iterator> it = (Iterator>) coll.iterator();
return new Iterator>() {
public boolean hasNext() {
return it.hasNext();
}
public Map.Entry next() {
return new UnmodifiableEntry(it.next());
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public Object[] toArray() {
Object[] array = super.toArray();
wrap(array, array.length);
return array;
}
@Override
@SuppressWarnings("unchecked")
public T[] toArray(T[] a) {
Object[] result = super.toArray(a);
wrap(result, coll.size());
return (T[]) result;
}
/**
* Wrap an array of Map.Entries as UnmodifiableEntries.
*
* @param array array to wrap
* @param size number of entries to wrap
*/
@SuppressWarnings("unchecked")
private void wrap(Object[] array, int size) {
for (int i = 0; i < size; ++i) {
array[i] = new UnmodifiableEntry((Map.Entry) array[i]);
}
}
}
private transient UnmodifiableSet> entrySet;
private transient UnmodifiableSet keySet;
private final Map extends K, ? extends V> map;
private transient UnmodifiableCollection values;
public UnmodifiableMap(Map extends K, ? extends V> map) {
this.map = map;
}
public void clear() {
throw new UnsupportedOperationException();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public boolean containsValue(Object val) {
return map.containsValue(val);
}
public Set> entrySet() {
if (entrySet == null) {
entrySet = new UnmodifiableEntrySet(map.entrySet());
}
return entrySet;
}
@Override
public boolean equals(Object o) {
return map.equals(o);
}
public V get(Object key) {
return map.get(key);
}
@Override
public int hashCode() {
return map.hashCode();
}
public boolean isEmpty() {
return map.isEmpty();
}
public Set keySet() {
if (keySet == null) {
keySet = new UnmodifiableSet(map.keySet());
}
return keySet;
}
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
public void putAll(Map extends K, ? extends V> t) {
throw new UnsupportedOperationException();
}
public V remove(Object key) {
throw new UnsupportedOperationException();
}
public int size() {
return map.size();
}
@Override
public String toString() {
return map.toString();
}
public Collection values() {
if (values == null) {
values = new UnmodifiableCollection(map.values());
}
return values;
}
}
static class UnmodifiableRandomAccessList extends UnmodifiableList
implements RandomAccess {
public UnmodifiableRandomAccessList(List extends T> list) {
super(list);
}
}
static class UnmodifiableSet extends UnmodifiableCollection implements
Set {
public UnmodifiableSet(Set extends T> set) {
super(set);
}
@Override
public boolean equals(Object o) {
return coll.equals(o);
}
@Override
public int hashCode() {
return coll.hashCode();
}
}
static class UnmodifiableSortedMap extends UnmodifiableMap
implements SortedMap {
private SortedMap sortedMap;
public UnmodifiableSortedMap(SortedMap sortedMap) {
super(sortedMap);
this.sortedMap = sortedMap;
}
public Comparator super K> comparator() {
return sortedMap.comparator();
}
@Override
public boolean equals(Object o) {
return sortedMap.equals(o);
}
public K firstKey() {
return sortedMap.firstKey();
}
@Override
public int hashCode() {
return sortedMap.hashCode();
}
public SortedMap headMap(K toKey) {
return new UnmodifiableSortedMap(sortedMap.headMap(toKey));
}
public K lastKey() {
return sortedMap.lastKey();
}
public SortedMap subMap(K fromKey, K toKey) {
return new UnmodifiableSortedMap(sortedMap.subMap(fromKey, toKey));
}
public SortedMap tailMap(K fromKey) {
return new UnmodifiableSortedMap(sortedMap.tailMap(fromKey));
}
}
static class UnmodifiableSortedSet extends UnmodifiableSet implements
SortedSet {
private SortedSet sortedSet;
@SuppressWarnings("unchecked")
public UnmodifiableSortedSet(SortedSet extends E> sortedSet) {
super(sortedSet);
this.sortedSet = (SortedSet) sortedSet;
}
public Comparator super E> comparator() {
return sortedSet.comparator();
}
@Override
public boolean equals(Object o) {
return sortedSet.equals(o);
}
public E first() {
return sortedSet.first();
}
@Override
public int hashCode() {
return sortedSet.hashCode();
}
public SortedSet headSet(E toElement) {
return new UnmodifiableSortedSet(sortedSet.headSet(toElement));
}
public E last() {
return sortedSet.last();
}
public SortedSet subSet(E fromElement, E toElement) {
return new UnmodifiableSortedSet(sortedSet.subSet(fromElement,
toElement));
}
public SortedSet tailSet(E fromElement) {
return new UnmodifiableSortedSet(sortedSet.tailSet(fromElement));
}
}
private static class UnmodifiableCollectionIterator implements Iterator {
private final Iterator extends T> it;
private UnmodifiableCollectionIterator(Iterator extends T> it) {
this.it = it;
}
public boolean hasNext() {
return it.hasNext();
}
public T next() {
return it.next();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
private static class UnmodifiableListIterator extends
UnmodifiableCollectionIterator implements ListIterator {
private final ListIterator extends T> lit;
private UnmodifiableListIterator(ListIterator extends T> lit) {
super(lit);
this.lit = lit;
}
public void add(T o) {
throw new UnsupportedOperationException();
}
public boolean hasPrevious() {
return lit.hasPrevious();
}
public int nextIndex() {
return lit.nextIndex();
}
public T previous() {
return lit.previous();
}
public int previousIndex() {
return lit.previousIndex();
}
public void set(T o) {
throw new UnsupportedOperationException();
}
}
@SuppressWarnings("unchecked")
public static final List EMPTY_LIST = new EmptyList();
@SuppressWarnings("unchecked")
public static final Map EMPTY_MAP = new EmptyMap();
@SuppressWarnings("unchecked")
public static final Set EMPTY_SET = new EmptySet();
private static Comparator> reverseComparator = new Comparator>() {
public int compare(Comparable