📄 collectionutils.java
字号:
public static void reverseArray(Object[] array) {
int i = 0;
int j = array.length - 1;
Object tmp;
while(j>i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
private static final int getFreq(final Object obj, final Map freqMap) {
try {
return ((Integer)(freqMap.get(obj))).intValue();
} catch(NullPointerException e) {
// ignored
} catch(NoSuchElementException e) {
// ignored
}
return 0;
}
/**
* Base class for collection decorators. I decided to do it this way
* because it seemed to result in the most reuse.
*
* Inner class tree looks like:
* <pre>
* CollectionWrapper
* PredicatedCollection
* PredicatedSet
* PredicatedList
* PredicatedBag
* PredicatedBuffer
* UnmodifiableCollection
* UnmodifiableBag
* UnmodifiableBuffer
* LazyCollection
* LazyList
* LazyBag
* SynchronizedCollection
* SynchronizedBuffer
* SynchronizedBag
* SynchronizedBuffer
* </pre>
*/
static class CollectionWrapper
implements Collection {
protected final Collection collection;
public CollectionWrapper(Collection collection) {
if (collection == null) {
throw new IllegalArgumentException("Collection must not be null");
}
this.collection = collection;
}
public int size() {
return collection.size();
}
public boolean isEmpty() {
return collection.isEmpty();
}
public boolean contains(Object o) {
return collection.contains(o);
}
public Iterator iterator() {
return collection.iterator();
}
public Object[] toArray() {
return collection.toArray();
}
public Object[] toArray(Object[] o) {
return collection.toArray(o);
}
public boolean add(Object o) {
return collection.add(o);
}
public boolean remove(Object o) {
return collection.remove(o);
}
public boolean containsAll(Collection c2) {
return collection.containsAll(c2);
}
public boolean addAll(Collection c2) {
return collection.addAll(c2);
}
public boolean removeAll(Collection c2) {
return collection.removeAll(c2);
}
public boolean retainAll(Collection c2) {
return collection.retainAll(c2);
}
public void clear() {
collection.clear();
}
public boolean equals(Object o) {
if (o == this) return true;
return collection.equals(o);
}
public int hashCode() {
return collection.hashCode();
}
public String toString() {
return collection.toString();
}
}
static class PredicatedCollection
extends CollectionWrapper {
protected final Predicate predicate;
public PredicatedCollection(Collection c, Predicate p) {
super(c);
if (p == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
this.predicate = p;
for (Iterator iter = c.iterator(); iter.hasNext(); ) {
validate(iter.next());
}
}
public boolean add(Object o) {
validate(o);
return collection.add(o);
}
public boolean addAll(Collection c2) {
for (Iterator iter = c2.iterator(); iter.hasNext(); ) {
validate(iter.next());
}
return collection.addAll(c2);
}
protected void validate(Object o) {
if (!predicate.evaluate(o)) {
throw new IllegalArgumentException("Cannot add Object - Predicate rejected it");
}
}
}
static class UnmodifiableCollection
extends CollectionWrapper {
public UnmodifiableCollection(Collection c) {
super(c);
}
public boolean add(Object o) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection c) {
throw new UnsupportedOperationException();
}
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 void clear() {
throw new UnsupportedOperationException();
}
public Iterator iterator() {
return new UnmodifiableIterator(collection.iterator());
}
}
static class SynchronizedCollection {
protected final Collection collection;
public SynchronizedCollection(Collection collection) {
if (collection == null) {
throw new IllegalArgumentException("Collection must not be null");
}
this.collection = collection;
}
public synchronized int size() {
return collection.size();
}
public synchronized boolean isEmpty() {
return collection.isEmpty();
}
public synchronized boolean contains(Object o) {
return collection.contains(o);
}
public Iterator iterator() {
return collection.iterator();
}
public synchronized Object[] toArray() {
return collection.toArray();
}
public synchronized Object[] toArray(Object[] o) {
return collection.toArray(o);
}
public synchronized boolean add(Object o) {
return collection.add(o);
}
public synchronized boolean remove(Object o) {
return collection.remove(o);
}
public synchronized boolean containsAll(Collection c2) {
return collection.containsAll(c2);
}
public synchronized boolean addAll(Collection c2) {
return collection.addAll(c2);
}
public synchronized boolean removeAll(Collection c2) {
return collection.removeAll(c2);
}
public synchronized boolean retainAll(Collection c2) {
return collection.retainAll(c2);
}
public synchronized void clear() {
collection.clear();
}
public synchronized boolean equals(Object o) {
return collection.equals(o);
}
public synchronized int hashCode() {
return collection.hashCode();
}
public synchronized String toString() {
return collection.toString();
}
}
static class UnmodifiableIterator
implements Iterator {
protected final Iterator iterator;
public UnmodifiableIterator(Iterator iterator) {
if (iterator == null) {
throw new IllegalArgumentException("Iterator must not be null");
}
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* Returns a predicated collection backed by the given collection.
* Only objects that pass the test in the given predicate can be
* added to the collection.
* It is important not to use the original collection after invoking this
* method, as it is a backdoor for adding unvalidated objects.
*
* @param collection the collection to predicate, must not be null
* @param predicate the predicate for the collection, must not be null
* @return a predicated collection backed by the given collection
* @throws IllegalArgumentException if the Collection is null
*/
public static Collection predicatedCollection(Collection collection, Predicate predicate) {
return new PredicatedCollection(collection, predicate);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -