| /*=============================================================================# |
| # Copyright (c) 2009, 2021 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.jcommons.collections; |
| |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.FIELD; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.PARAMETER; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.RETURN_TYPE; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.TYPE_ARGUMENT; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.TYPE_BOUND; |
| |
| import java.util.Arrays; |
| import java.util.Collection; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.ListIterator; |
| import java.util.NoSuchElementException; |
| import java.util.Objects; |
| import java.util.RandomAccess; |
| import java.util.Spliterator; |
| import java.util.Spliterators; |
| |
| import org.eclipse.statet.jcommons.collections.ImIdentityList; |
| import org.eclipse.statet.jcommons.collections.ImList; |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| |
| |
| @NonNullByDefault({ PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT }) |
| public final class ImArraySub0List<E> extends AbstractImList<E> implements ImList<E>, |
| RandomAccess { |
| |
| |
| private class Iter extends AbstractImListIter<E> { |
| |
| |
| private int cursor; |
| |
| |
| Iter(final int index) { |
| this.cursor= index; |
| } |
| |
| |
| @Override |
| public boolean hasNext() { |
| return (this.cursor < ImArraySub0List.this.size); |
| } |
| |
| @Override |
| public int nextIndex() { |
| return this.cursor; |
| } |
| |
| @Override |
| public E next() { |
| if (this.cursor >= ImArraySub0List.this.size) { |
| throw new NoSuchElementException(); |
| } |
| return ImArraySub0List.this.array[this.cursor++]; |
| } |
| |
| @Override |
| public boolean hasPrevious() { |
| return (this.cursor > 0); |
| } |
| |
| @Override |
| public int previousIndex() { |
| return this.cursor - 1; |
| } |
| |
| @Override |
| public E previous() { |
| if (this.cursor <= 0 || ImArraySub0List.this.size <= 0) { |
| throw new NoSuchElementException(); |
| } |
| return ImArraySub0List.this.array[--this.cursor]; |
| } |
| |
| } |
| |
| |
| private final E[] array; |
| private final int size; |
| |
| |
| public ImArraySub0List(final E[] array, final int toIndex) { |
| this.array= array; |
| this.size= toIndex; |
| } |
| |
| |
| @Override |
| public int size() { |
| return this.size; |
| } |
| |
| @Override |
| public boolean isEmpty() { |
| return false; |
| } |
| |
| @Override |
| public boolean contains(final @Nullable Object o) { |
| return (indexOf(o) >= 0); |
| } |
| |
| @Override |
| public boolean containsAll(final Collection<?> c) { |
| final Iterator<?> iter= c.iterator(); |
| while(iter.hasNext()) { |
| if (indexOf(iter.next()) < 0) { |
| return false; |
| } |
| } |
| return true; |
| } |
| |
| @Override |
| public E get(final int index) { |
| if (index < 0 || index >= this.size) { |
| throw new IndexOutOfBoundsException("index= " + index); //$NON-NLS-1$ |
| } |
| return this.array[index]; |
| } |
| |
| @Override |
| public int indexOf(final @Nullable Object o) { |
| if (o == null) { |
| for (int i= 0; i < this.size; i++) { |
| if (null == this.array[i]) { |
| return i; |
| } |
| } |
| return -1; |
| } |
| else { |
| for (int i= 0; i < this.size; i++) { |
| if (o.equals(this.array[i])) { |
| return i; |
| } |
| } |
| return -1; |
| } |
| } |
| |
| @Override |
| public int lastIndexOf(final @Nullable Object o) { |
| if (o == null) { |
| for (int i= this.size - 1; i >= 0; i--) { |
| if (null == this.array[i]) { |
| return i; |
| } |
| } |
| return -1; |
| } |
| else { |
| for (int i= this.size - 1; i >= 0; i--) { |
| if (o.equals(this.array[i])) { |
| return i; |
| } |
| } |
| return -1; |
| } |
| } |
| |
| |
| @Override |
| public Iterator<E> iterator() { |
| return new Iter(0); |
| } |
| |
| @Override |
| public ListIterator<E> listIterator() { |
| return new Iter(0); |
| } |
| |
| @Override |
| public ListIterator<E> listIterator(final int index) { |
| if (index < 0 || index > this.size) { |
| throw new IndexOutOfBoundsException("index= " + index); //$NON-NLS-1$ |
| } |
| return new Iter(index); |
| } |
| |
| @Override |
| public Spliterator<E> spliterator() { |
| return Spliterators.spliterator(this.array, 0, this.size, |
| Spliterator.IMMUTABLE | Spliterator.ORDERED ); |
| } |
| |
| |
| @Override |
| @SuppressWarnings("unchecked") |
| public ImList<E> subList(final int fromIndex, final int toIndex) { |
| if (fromIndex < 0 || toIndex > this.size) { |
| throw new IndexOutOfBoundsException("fromIndex= " + fromIndex + ", toIndex= " + toIndex + ", size= " + this.size); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| } |
| if (fromIndex > toIndex) { |
| throw new IllegalArgumentException("fromIndex > toIndex: fromIndex= " + fromIndex + ", toIndex= " + toIndex); //$NON-NLS-1$ //$NON-NLS-2$ |
| } |
| final int l= toIndex - fromIndex; |
| if (l == this.size) { |
| return this; |
| } |
| else if (l == 0) { |
| return ImEmptyList.INSTANCE; |
| } |
| else if (l == 1) { |
| return new ImSingletonList<>(this.array[fromIndex]); |
| } |
| else if (fromIndex == 0) { |
| return new ImArraySub0List<>(this.array, toIndex); |
| } |
| else { |
| return new ImArraySubList<>(this.array, fromIndex, toIndex); |
| } |
| } |
| |
| @Override |
| public Object[] toArray() { |
| final Object[] dest= new Object[this.size]; |
| System.arraycopy(this.array, 0, dest, 0, this.size); |
| return dest; |
| } |
| |
| @Override |
| @SuppressWarnings("unchecked") |
| public <T> T[] toArray(final T[] dest) { |
| if (dest.length < this.size) { |
| return Arrays.copyOf(this.array, this.size, (Class<? extends T[]>)dest.getClass()); |
| } |
| System.arraycopy(this.array, 0, dest, 0, this.size); |
| if (dest.length > this.size) { |
| dest[this.size]= null; |
| } |
| return dest; |
| } |
| |
| @Override |
| public void copyTo(final Object[] dest, final int destPos) { |
| System.arraycopy(this.array, 0, dest, destPos, this.size); |
| } |
| |
| @Override |
| public void copyTo(final int srcPos, final Object[] dest, final int destPos, final int length) { |
| System.arraycopy(this.array, 0 + srcPos, dest, destPos, length); |
| } |
| |
| @Override |
| public ImList<E> toImList() { |
| return this; |
| } |
| |
| @Override |
| public ImIdentityList<E> toImIdentityList() { |
| return new ImArrayIdentitySubList<>(this.array, 0, this.size); |
| } |
| |
| |
| @Override |
| public int hashCode() { |
| int hashCode= 1; |
| for (int i= 0; i < this.size; i++) { |
| hashCode= 31 * hashCode + ((this.array[i] != null) ? this.array[i].hashCode() : 0); |
| } |
| return hashCode; |
| } |
| |
| @Override |
| public boolean equals(final @Nullable Object obj) { |
| if (obj == this) { |
| return true; |
| } |
| if (obj instanceof List) { |
| final List<?> other= (List<?>) obj; |
| if (this.size != other.size()) { |
| return false; |
| } |
| final ListIterator<?> otherIter= other.listIterator(); |
| for (int i= 0; i < this.size; i++) { |
| if (!Objects.equals(this.array[i], otherIter.next())) { |
| return false; |
| } |
| } |
| return true; |
| } |
| return false; |
| } |
| |
| @Override |
| public String toString() { |
| return Arrays.toString(toArray()); |
| } |
| |
| } |