Skip to content

Commit

Permalink
Added more to the avian classpath for the collection interface as wel…
Browse files Browse the repository at this point in the history
…l 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
  • Loading branch information
Mike Jensen committed Apr 29, 2013
1 parent 3372210 commit a41f8c0
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 67 deletions.
30 changes: 30 additions & 0 deletions classpath/java/util/AbstractCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ public boolean contains(Object element) {
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;
}
Expand All @@ -60,6 +75,21 @@ public boolean remove(Object element) {
+ 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() {
Expand Down
21 changes: 21 additions & 0 deletions classpath/java/util/AbstractList.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ public boolean add(T o) {
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();
}
Expand Down
55 changes: 3 additions & 52 deletions classpath/java/util/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,15 @@ public static boolean equals(Object[] a, Object[] b) {
}

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])) {
Expand All @@ -138,7 +117,7 @@ public int lastIndexOf(Object element) {
}
return -1;
}

public T get(int index) {
return array[index];
}
Expand All @@ -147,41 +126,13 @@ public T set(int index, T value) {
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);
}
};
}

Expand Down
4 changes: 4 additions & 0 deletions classpath/java/util/Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions classpath/java/util/Collections.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ public void clear() {
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> {
Expand Down Expand Up @@ -393,6 +401,18 @@ public <S> S[] toArray(S[] array) {
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) {
Expand Down Expand Up @@ -500,6 +520,14 @@ public Object[] toArray() {

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");
}
}

Expand Down
35 changes: 21 additions & 14 deletions classpath/java/util/HashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public boolean equal(K a, K b) {
}
}

private class EntrySet implements Set<Entry<K, V>> {
private class EntrySet extends AbstractSet<Entry<K, V>> {
public int size() {
return HashMap.this.size();
}
Expand All @@ -321,12 +321,6 @@ public boolean add(Entry<K, V> e) {
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);
}
Expand All @@ -352,7 +346,7 @@ public Iterator<Entry<K, V>> iterator() {
}
}

private class KeySet implements Set<K> {
private class KeySet extends AbstractSet<K> {
public int size() {
return HashMap.this.size();
}
Expand All @@ -369,12 +363,6 @@ public boolean add(K key) {
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;
}
Expand Down Expand Up @@ -409,6 +397,21 @@ public boolean contains(Object 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) {
throw new UnsupportedOperationException();
}
Expand All @@ -421,6 +424,10 @@ public boolean remove(Object value) {
throw new UnsupportedOperationException();
}

public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

public Object[] toArray() {
return toArray(new Object[size()]);
}
Expand Down
2 changes: 2 additions & 0 deletions classpath/java/util/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion classpath/java/util/TreeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public V setValue(V value) {

}

private class KeySet implements Set<K> {
private class KeySet extends AbstractSet<K> {
public int size() {
return TreeMap.this.size();
}
Expand Down Expand Up @@ -177,6 +177,21 @@ public boolean contains(Object 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) {
throw new UnsupportedOperationException();
}
Expand All @@ -189,6 +204,10 @@ public boolean remove(Object value) {
throw new UnsupportedOperationException();
}

public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

public Object[] toArray() {
return toArray(new Object[size()]);
}
Expand Down

0 comments on commit a41f8c0

Please sign in to comment.