Merge remote-tracking branch 'origin/master'

This commit is contained in:
Joel Dice 2013-04-30 23:08:18 -06:00
commit 6b3a352b38
8 changed files with 129 additions and 67 deletions

View File

@ -51,6 +51,21 @@ public abstract class AbstractCollection<T> implements Collection<T> {
return false;
}
public boolean containsAll(Collection<?> c) {
if (c == null) {
throw new NullPointerException("Collection is null");
}
Iterator<?> it = c.iterator();
while (it.hasNext()) {
if (! contains(it.next())) {
return false;
}
}
return true;
}
public boolean isEmpty() {
return size() == 0;
}
@ -60,6 +75,21 @@ public abstract class AbstractCollection<T> implements Collection<T> {
+ this.getClass().getName());
}
public boolean removeAll(Collection<?> c) {
if (c == null) {
throw new NullPointerException("Collection is null");
}
boolean changed = false;
Iterator<?> it = c.iterator();
while (it.hasNext()) {
changed = remove(it.next()) || changed;
}
return changed;
}
public abstract int size();
public Object[] toArray() {

View File

@ -20,6 +20,27 @@ public abstract class AbstractList<T> extends AbstractCollection<T>
return true;
}
public boolean addAll(Collection<? extends T> c) {
return addAll(size(), c);
}
public boolean addAll(int startIndex, Collection<? extends T> c) {
if (c == null) {
throw new NullPointerException("Collection is null");
}
int index = startIndex;
boolean changed = false;
Iterator<? extends T> it = c.iterator();
while (it.hasNext()) {
add(index++, it.next());
changed = true;
}
return changed;
}
public Iterator<T> iterator() {
return listIterator();
}

View File

@ -91,36 +91,15 @@ public class Arrays {
}
public static <T> List<T> asList(final T ... array) {
return new List<T>() {
public String toString() {
return Collections.toString(this);
}
return new AbstractList<T>() {
public int size() {
return array.length;
}
public boolean add(T element) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends T> collection) {
throw new UnsupportedOperationException();
}
public void add(int index, T element) {
throw new UnsupportedOperationException();
}
public boolean contains(Object element) {
for (int i = 0; i < array.length; ++i) {
if (equal(element, array[i])) {
return true;
}
}
return false;
}
public int indexOf(Object element) {
for (int i = 0; i < array.length; ++i) {
if (equal(element, array[i])) {
@ -138,7 +117,7 @@ public class Arrays {
}
return -1;
}
public T get(int index) {
return array[index];
}
@ -147,41 +126,13 @@ public class Arrays {
throw new UnsupportedOperationException();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <S> S[] toArray(S[] a) {
return (S[])array;
}
public boolean isEmpty() {
return size() == 0;
}
public T remove(int index) {
throw new UnsupportedOperationException();
}
public boolean remove(Object element) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}
public Iterator<T> iterator() {
return listIterator();
}
public ListIterator<T> listIterator(int index) {
return new Collections.ArrayListIterator(this, index);
}
public ListIterator<T> listIterator() {
return listIterator(0);
}
};
}

View File

@ -17,12 +17,16 @@ public interface Collection<T> extends Iterable<T> {
public boolean contains(Object element);
public boolean containsAll(Collection<?> c);
public boolean add(T element);
public boolean addAll(Collection<? extends T> collection);
public boolean remove(Object element);
public boolean removeAll(Collection<?> c);
public Object[] toArray();
public <S> S[] toArray(S[] array);

View File

@ -170,6 +170,14 @@ public class Collections {
public Iterator<T> iterator() {
return new SynchronizedIterator(lock, collection.iterator());
}
public boolean containsAll(Collection<?> c) {
synchronized (lock) { return collection.containsAll(c); }
}
public boolean removeAll(Collection<?> c) {
synchronized (lock) { return collection.removeAll(c); }
}
}
static class SynchronizedMap<K,V> implements Map<K,V> {
@ -393,6 +401,18 @@ public class Collections {
public void clear() {
throw new UnsupportedOperationException("not supported");
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("not supported");
}
public boolean addAll(int startIndex, Collection<? extends T> c) {
throw new UnsupportedOperationException("not supported");
}
public boolean containsAll(Collection<?> c) {
return inner.containsAll(c);
}
}
public static <K,V> Map<K,V> unmodifiableMap(Map<K,V> m) {
@ -500,6 +520,14 @@ public class Collections {
public <S> S[] toArray(S[] array) {
return inner.toArray(array);
}
public boolean containsAll(Collection<?> c) {
return inner.containsAll(c);
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("not supported");
}
}

View File

@ -303,7 +303,7 @@ public class HashMap<K, V> implements Map<K, V> {
}
}
private class EntrySet implements Set<Entry<K, V>> {
private class EntrySet extends AbstractSet<Entry<K, V>> {
public int size() {
return HashMap.this.size();
}
@ -321,12 +321,6 @@ public class HashMap<K, V> implements Map<K, V> {
return putCell(e.getKey(), e.getValue()) != null;
}
public boolean addAll(Collection<? extends Entry<K, V>> collection) {
boolean change = false;
for (Entry<K, V> e: collection) if (add(e)) change = true;
return change;
}
public boolean remove(Object o) {
return (o instanceof Entry<?,?>) && remove((Entry<?,?>)o);
}
@ -352,7 +346,7 @@ public class HashMap<K, V> implements Map<K, V> {
}
}
private class KeySet implements Set<K> {
private class KeySet extends AbstractSet<K> {
public int size() {
return HashMap.this.size();
}
@ -369,12 +363,6 @@ public class HashMap<K, V> implements Map<K, V> {
return putCell(key, null) != null;
}
public boolean addAll(Collection<? extends K> collection) {
boolean change = false;
for (K k: collection) if (add(k)) change = true;
return change;
}
public boolean remove(Object key) {
return removeCell(key) != null;
}
@ -409,6 +397,21 @@ public class HashMap<K, V> implements Map<K, V> {
return containsValue(value);
}
public boolean containsAll(Collection<?> c) {
if (c == null) {
throw new NullPointerException("collection is null");
}
Iterator<?> it = c.iterator();
while (it.hasNext()) {
if (! contains(it.next())) {
return false;
}
}
return true;
}
public boolean add(V value) {
throw new UnsupportedOperationException();
}
@ -421,6 +424,10 @@ public class HashMap<K, V> implements Map<K, V> {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}

View File

@ -21,6 +21,8 @@ public interface List<T> extends Collection<T> {
public void add(int index, T element);
public boolean addAll(int startIndex, Collection<? extends T> c);
public int indexOf(Object value);
public int lastIndexOf(Object value);

View File

@ -120,7 +120,7 @@ public class TreeMap<K,V> implements Map<K,V> {
}
private class KeySet implements Set<K> {
private class KeySet extends AbstractSet<K> {
public int size() {
return TreeMap.this.size();
}
@ -177,6 +177,21 @@ public class TreeMap<K,V> implements Map<K,V> {
return containsValue(value);
}
public boolean containsAll(Collection<?> c) {
if (c == null) {
throw new NullPointerException("collection is null");
}
Iterator<?> it = c.iterator();
while (it.hasNext()) {
if (! contains(it.next())) {
return false;
}
}
return true;
}
public boolean add(V value) {
throw new UnsupportedOperationException();
}
@ -189,6 +204,10 @@ public class TreeMap<K,V> implements Map<K,V> {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}