From 7fa215fa9bc26e4bbaaa863f7c7a3a852c42811b Mon Sep 17 00:00:00 2001 From: jan zens <89642092+sneznaj@users.noreply.github.com> Date: Tue, 16 Nov 2021 10:33:48 +0100 Subject: [PATCH] mplement subList() on primitive immutable lists --- .../primitive/ImmutableBooleanArrayList.java | 109 +++++++++++++++- .../impl/list/primitive/LongInterval.java | 120 +++++++++++++++++- 2 files changed, 227 insertions(+), 2 deletions(-) diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/immutable/primitive/ImmutableBooleanArrayList.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/immutable/primitive/ImmutableBooleanArrayList.java index c34b592676..706a32e70c 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/immutable/primitive/ImmutableBooleanArrayList.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/immutable/primitive/ImmutableBooleanArrayList.java @@ -640,7 +640,7 @@ public void appendString( @Override public ImmutableBooleanList subList(int fromIndex, int toIndex) { - throw new UnsupportedOperationException("subList not yet implemented!"); + return new ImmutableSubList<>(this, fromIndex, toIndex); } private Object writeReplace() @@ -718,4 +718,111 @@ public boolean next() return next; } } + + protected static class ImmutableBooleanArraySubList + extends ImmutableBooleanArrayList + implements Serializable, RandomAccess //? + { + private final ImmutableBooleanArrayList original; + private final int offset; + private final int size; + + protected ImmutableBooleanArraySubList(ImmutableBooleanArrayList list, int fromIndex, int toIndex) + { + if (fromIndex < 0) + { + throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); + } + if (toIndex > list.size()) + { + throw new IndexOutOfBoundsException("toIndex = " + toIndex); + } + if (fromIndex > toIndex) + { + throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ')'); + } + this.original = list; + this.offset = fromIndex; + this.size = toIndex - fromIndex; + } + + @Override + public T get(int index) + { + this.checkIfOutOfBounds(index); + return this.original.get(index + this.offset); + } + + @Override + public int size() + { + return this.size; + } + + @Override + public ImmutableBooleanList newWith(boolean element) + { + BitSet newItems = (BitSet) this.original.items.clone(); + if (element) + { + newItems.set(this.size); + } + return new ImmutableBooleanArrayList(newItems, this.size + 1); + } + + protected Object writeReplace() + { + return Lists.immutable.withAll(this); + } + + @Override + public Iterator iterator() + { + return this.listIterator(0); + } + + @Override + public ImmutableBooleanArraySubList subList(int fromIndex, int toIndex) + { + if (fromIndex < 0) + { + throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); + } + if (toIndex > this.size()) + { + throw new IndexOutOfBoundsException("toIndex = " + toIndex); + } + if (fromIndex > toIndex) + { + throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ')'); + } + + return new ImmutableBooleanArraySubList<>(this.original, this.offset + fromIndex, this.offset + toIndex); + } + + @Override + public MutableStack toStack() + { + return ArrayStack.newStack(this); + } + + @Override + public void each(Procedure procedure) + { + ListIterate.forEach(this, procedure); + } + + @Override + public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) + { + ListIterate.forEachWithIndex(this, objectIntProcedure); + }° + + @Override + public

void forEachWith(Procedure2 procedure, P parameter) + { + ListIterate.forEachWith(this, procedure, parameter); + } + + } } diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/primitive/LongInterval.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/primitive/LongInterval.java index 4ec6c37371..85d0bc0b3d 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/primitive/LongInterval.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/primitive/LongInterval.java @@ -532,7 +532,7 @@ public ImmutableLongList distinct() @Override public ImmutableLongList subList(int fromIndex, int toIndex) { - throw new UnsupportedOperationException("subList not yet implemented!"); + return new LongIntervalSubList<>(this, fromIndex, toIndex); } /** @@ -1101,4 +1101,122 @@ public boolean tryAdvance(LongConsumer action) return this.current >= this.to; } } + + protected static class LongIntervalSubList + extends LongIntervall + implements Serializable, RandomAccess + { + // Not important since it uses writeReplace() + private static final long serialVersionUID = 1L; + + private final LongInterval original; + private final int offset; + private final int size; + + protected SubList(ImmutableList list, int fromIndex, int toIndex) + { + if (fromIndex < 0) + { + throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); + } + if (toIndex > list.size()) + { + throw new IndexOutOfBoundsException("toIndex = " + toIndex); + } + if (fromIndex > toIndex) + { + throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ')'); + } + this.original = list; + this.offset = fromIndex; + this.size = toIndex - fromIndex; + } + + @Override + public T get(int index) + { + this.checkIfOutOfBounds(index); + return this.original.get(index + this.offset); + } + + @Override + public int size() + { + return this.size; + } + + @Override + public ImmutableLongList newWith(T newItem) + { + return LongArrayList.newList(this.original).with(element).toImmutable(); + } + + protected Object writeReplace() + { + return Lists.immutable.withAll(this.original); + } + + @Override + public LongIntervalSubList subList(int fromIndex, int toIndex) + { + if (fromIndex < 0) + { + throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); + } + if (toIndex > this.size()) + { + throw new IndexOutOfBoundsException("toIndex = " + toIndex); + } + if (fromIndex > toIndex) + { + throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ')'); + } + + return new LongIntervalSubList<>(this.original, this.offset + fromIndex, this.offset + toIndex); + } + + private void checkIfOutOfBounds(int index) + { + if (index >= this.size || index < 0) + { + throw new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size); + } + } + + @Override + public long getFirst() + { + return this.isEmpty() ? null : this.original.get(this.offset); + } + + @Override + public long getLast() + { + return this.isEmpty() ? null : this.original.get(this.offset + this.size - 1); + } + + @Override + public MutableStack toStack() + { + return ArrayStack.newStack(this); + } + + @Override + public void each(Procedure procedure) + { + ListIterate.forEach(this, procedure); + } + + @Override + public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) + { + ListIterate.forEachWithIndex(this, objectIntProcedure); + } + + @Override + public

void forEachWith(Procedure2 procedure, P parameter) + { + ListIterate.forEachWith(this, procedure, parameter); + } + } }