classlib: more argument validation in Collections methods (#839)

This commit is contained in:
Ivan Hetman 2023-10-31 10:46:43 +02:00 committed by GitHub
parent 5b5c26cf99
commit 2756fe4384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -183,6 +183,9 @@ public class TCollections extends TObject {
} }
public static <T> TList<T> nCopies(final int n, final T o) { public static <T> TList<T> nCopies(final int n, final T o) {
if (n < 0) {
throw new IllegalArgumentException();
}
return new TAbstractList<>() { return new TAbstractList<>() {
@Override public T get(int index) { @Override public T get(int index) {
if (index < 0 || index >= n) { if (index < 0 || index >= n) {
@ -455,6 +458,7 @@ public class TCollections extends TObject {
} }
public static <T> TList<T> unmodifiableList(final TList<? extends T> list) { public static <T> TList<T> unmodifiableList(final TList<? extends T> list) {
TObjects.requireNonNull(list);
return new TAbstractList<>() { return new TAbstractList<>() {
@Override public T get(int index) { @Override public T get(int index) {
return list.get(index); return list.get(index);
@ -462,10 +466,14 @@ public class TCollections extends TObject {
@Override public int size() { @Override public int size() {
return list.size(); return list.size();
} }
@Override public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
}; };
} }
public static <T> TCollection<T> unmodifiableCollection(final TCollection<? extends T> c) { public static <T> TCollection<T> unmodifiableCollection(final TCollection<? extends T> c) {
TObjects.requireNonNull(c);
return new TAbstractCollection<>() { return new TAbstractCollection<>() {
@Override public TIterator<T> iterator() { @Override public TIterator<T> iterator() {
return unmodifiableIterator(c.iterator()); return unmodifiableIterator(c.iterator());
@ -473,6 +481,9 @@ public class TCollections extends TObject {
@Override public int size() { @Override public int size() {
return c.size(); return c.size();
} }
@Override public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
}; };
} }
@ -491,6 +502,7 @@ public class TCollections extends TObject {
} }
public static <T> TSet<T> unmodifiableSet(final TSet<? extends T> s) { public static <T> TSet<T> unmodifiableSet(final TSet<? extends T> s) {
TObjects.requireNonNull(s);
return new TAbstractSet<>() { return new TAbstractSet<>() {
@Override public TIterator<T> iterator() { @Override public TIterator<T> iterator() {
return unmodifiableIterator(s.iterator()); return unmodifiableIterator(s.iterator());
@ -498,14 +510,21 @@ public class TCollections extends TObject {
@Override public int size() { @Override public int size() {
return s.size(); return s.size();
} }
@Override public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
}; };
} }
public static <K, V> TMap<K, V> unmodifiableMap(final TMap<? extends K, ? extends V> m) { public static <K, V> TMap<K, V> unmodifiableMap(final TMap<? extends K, ? extends V> m) {
TObjects.requireNonNull(m);
return new TAbstractMap<>() { return new TAbstractMap<>() {
@Override public TSet<Entry<K, V>> entrySet() { @Override public TSet<Entry<K, V>> entrySet() {
return unmodifiableMapEntrySet(m.entrySet()); return unmodifiableMapEntrySet(m.entrySet());
} }
@Override public V remove(Object o) {
throw new UnsupportedOperationException();
}
}; };
} }
@ -518,6 +537,9 @@ public class TCollections extends TObject {
@Override public TIterator<Entry<K, V>> iterator() { @Override public TIterator<Entry<K, V>> iterator() {
return unmodifiableMapEntryIterator(c.iterator()); return unmodifiableMapEntryIterator(c.iterator());
} }
@Override public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
}; };
} }