import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
-import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
/**
* Internal array-backed {@link List}. It assumes the array does not contain nulls and it does not get modified
*/
final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
private static final long serialVersionUID = 1L;
- private final E[] array;
+ private final E @NonNull[] array;
- ConstantArrayCollection(final E[] array) {
+ ConstantArrayCollection(final E @NonNull[] array) {
this.array = requireNonNull(array);
}
return false;
}
- @Nonnull
@Override
- public Iterator<E> iterator() {
- return new UnmodifiableIterator<E>() {
- private int offset = 0;
-
- @Override
- public boolean hasNext() {
- return offset < array.length;
- }
-
- @Override
- public E next() {
- if (offset >= array.length) {
- throw new NoSuchElementException();
- }
- return array[offset++];
- }
- };
+ public @NonNull Iterator<E> iterator() {
+ return new Itr<>(array);
}
- @Nonnull
@Override
- public Object[] toArray() {
+ public Object @NonNull[] toArray() {
return array.clone();
}
- @Nonnull
@SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
@Override
- public <T> T[] toArray(@Nonnull final T[] a) {
+ public <T> T[] toArray(final T[] a) {
if (a.length < array.length) {
return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
}
@Override
@SuppressWarnings("checkstyle:parameterName")
- public boolean containsAll(@Nonnull final Collection<?> c) {
+ public boolean containsAll(final Collection<?> c) {
for (Object o : c) {
if (!contains(o)) {
return false;
@Override
@SuppressWarnings("checkstyle:parameterName")
- public boolean addAll(@Nonnull final Collection<? extends E> c) {
+ public boolean addAll(final Collection<? extends E> c) {
throw new UnsupportedOperationException();
}
@Override
@SuppressWarnings("checkstyle:parameterName")
- public boolean removeAll(@Nonnull final Collection<?> c) {
+ public boolean removeAll(final Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
@SuppressWarnings("checkstyle:parameterName")
- public boolean retainAll(@Nonnull final Collection<?> c) {
+ public boolean retainAll(final Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean equals(final Object obj) {
- if (obj == this) {
- return true;
- }
- if (!(obj instanceof ConstantArrayCollection)) {
- return false;
- }
-
- return Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
+ return obj == this || obj instanceof ConstantArrayCollection
+ && Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
}
@Override
- public String toString() {
+ public @NonNull String toString() {
if (array.length == 0) {
return "[]";
}
}
return sb.append(String.valueOf(array[offset])).append(']').toString();
}
+
+ private static final class Itr<E> extends UnmodifiableIterator<E> {
+ private final E @NonNull[] array;
+ private int offset = 0;
+
+ Itr(final E @NonNull[] array) {
+ this.array = array;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return offset < array.length;
+ }
+
+ @Override
+ public E next() {
+ if (offset >= array.length) {
+ throw new NoSuchElementException();
+ }
+ return array[offset++];
+ }
+ }
}