Added more to the avian classpath for the collection interface as well as the list interface

Added to collection:
public boolean containsAll(Collection<?> c);
public boolean removeAll(Collection<?> c);

Added to list:
public boolean addAll(int startIndex, Collection<? extends T> c);

Also where possible for inner classes I made them extend the abstract version instead of just implement the interface.  This helps reduce code duplication where possible.

These changes were necessary to support protobuf 2.5.0
This commit is contained in:
Mike Jensen 2013-04-29 11:32:56 -06:00
parent 3372210f45
commit a41f8c0103
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; 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() { public boolean isEmpty() {
return size() == 0; return size() == 0;
} }
@ -60,6 +75,21 @@ public abstract class AbstractCollection<T> implements Collection<T> {
+ this.getClass().getName()); + 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 abstract int size();
public Object[] toArray() { public Object[] toArray() {

View File

@ -20,6 +20,27 @@ public abstract class AbstractList<T> extends AbstractCollection<T>
return true; 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() { public Iterator<T> iterator() {
return listIterator(); return listIterator();
} }

View File

@ -91,36 +91,15 @@ public class Arrays {
} }
public static <T> List<T> asList(final T ... array) { public static <T> List<T> asList(final T ... array) {
return new List<T>() { return new AbstractList<T>() {
public String toString() {
return Collections.toString(this);
}
public int size() { public int size() {
return array.length; 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) { public void add(int index, T element) {
throw new UnsupportedOperationException(); 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) { public int indexOf(Object element) {
for (int i = 0; i < array.length; ++i) { for (int i = 0; i < array.length; ++i) {
if (equal(element, array[i])) { if (equal(element, array[i])) {
@ -147,41 +126,13 @@ public class Arrays {
throw new UnsupportedOperationException(); 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) { public T remove(int index) {
throw new UnsupportedOperationException(); 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) { public ListIterator<T> listIterator(int index) {
return new Collections.ArrayListIterator(this, 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 contains(Object element);
public boolean containsAll(Collection<?> c);
public boolean add(T element); public boolean add(T element);
public boolean addAll(Collection<? extends T> collection); public boolean addAll(Collection<? extends T> collection);
public boolean remove(Object element); public boolean remove(Object element);
public boolean removeAll(Collection<?> c);
public Object[] toArray(); public Object[] toArray();
public <S> S[] toArray(S[] array); public <S> S[] toArray(S[] array);

View File

@ -170,6 +170,14 @@ public class Collections {
public Iterator<T> iterator() { public Iterator<T> iterator() {
return new SynchronizedIterator(lock, collection.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> { static class SynchronizedMap<K,V> implements Map<K,V> {
@ -393,6 +401,18 @@ public class Collections {
public void clear() { public void clear() {
throw new UnsupportedOperationException("not supported"); 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) { public static <K,V> Map<K,V> unmodifiableMap(Map<K,V> m) {
@ -501,6 +521,14 @@ public class Collections {
public <S> S[] toArray(S[] array) { public <S> S[] toArray(S[] array) {
return inner.toArray(array); return inner.toArray(array);
} }
public boolean containsAll(Collection<?> c) {
return inner.containsAll(c);
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("not supported");
}
} }
public static <T> Set<T> unmodifiableSet(Set<T> hs) { public static <T> Set<T> unmodifiableSet(Set<T> hs) {

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() { public int size() {
return HashMap.this.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; 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) { public boolean remove(Object o) {
return (o instanceof Entry<?,?>) && remove((Entry<?,?>)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() { public int size() {
return HashMap.this.size(); return HashMap.this.size();
} }
@ -369,12 +363,6 @@ public class HashMap<K, V> implements Map<K, V> {
return putCell(key, null) != null; 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) { public boolean remove(Object key) {
return removeCell(key) != null; return removeCell(key) != null;
} }
@ -409,6 +397,21 @@ public class HashMap<K, V> implements Map<K, V> {
return containsValue(value); 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) { public boolean add(V value) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -421,6 +424,10 @@ public class HashMap<K, V> implements Map<K, V> {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public Object[] toArray() { public Object[] toArray() {
return toArray(new Object[size()]); 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 void add(int index, T element);
public boolean addAll(int startIndex, Collection<? extends T> c);
public int indexOf(Object value); public int indexOf(Object value);
public int lastIndexOf(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() { public int size() {
return TreeMap.this.size(); return TreeMap.this.size();
} }
@ -177,6 +177,21 @@ public class TreeMap<K,V> implements Map<K,V> {
return containsValue(value); 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) { public boolean add(V value) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -189,6 +204,10 @@ public class TreeMap<K,V> implements Map<K,V> {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public Object[] toArray() { public Object[] toArray() {
return toArray(new Object[size()]); return toArray(new Object[size()]);
} }