From df711dff669ba6a112389c7fb3b0133b2a1bf3a4 Mon Sep 17 00:00:00 2001 From: Glavo Date: Wed, 20 Nov 2024 15:07:46 +0800 Subject: [PATCH] Delete Conditions.checkElementIndex --- kala-base/src/main/java/kala/Conditions.java | 4 ---- .../java/kala/collection/base/GenericArrays.java | 4 ++-- .../java/kala/collection/base/ObjectArrays.java | 4 ++-- kala-base/src/main/java/kala/text/StringSlice.java | 3 ++- .../mutable/primitive/MutableBooleanArrayList.java | 6 +++--- .../java/kala/collection/primitive/BitArray.java | 4 ++-- .../primitive/MutablePrimitiveArrayList.java.ftl | 6 +++--- .../collection/primitive/PrimitiveSeqView.java.ftl | 2 +- .../internal/view/PrimitiveSeqViews.java.ftl | 5 +++-- .../src/main/java/kala/collection/ArraySeq.java | 9 +++------ .../src/main/java/kala/collection/Seq.java | 13 +++++-------- .../src/main/java/kala/collection/SeqView.java | 6 ++++-- .../collection/immutable/AbstractImmutableSeq.java | 4 ++-- .../collection/immutable/ImmutableLinkedSeq.java | 12 ++++++------ .../kala/collection/immutable/ImmutableSeqs.java | 2 +- .../kala/collection/immutable/ImmutableTreeSeq.java | 7 ++++--- .../kala/collection/immutable/ImmutableVectors.java | 13 ++++++------- .../kala/collection/internal/vector/Vector.java | 5 +++-- .../kala/collection/internal/view/SeqViews.java | 12 +++++------- .../kala/collection/mutable/MutableArrayDeque.java | 11 ++++++----- .../kala/collection/mutable/MutableArrayList.java | 6 +++--- .../collection/mutable/MutableArraySliceView.java | 4 +++- .../kala/collection/mutable/MutableLinkedList.java | 13 +++++-------- .../collection/mutable/MutableSmartArrayList.java | 6 +++--- 24 files changed, 77 insertions(+), 84 deletions(-) diff --git a/kala-base/src/main/java/kala/Conditions.java b/kala-base/src/main/java/kala/Conditions.java index ab9884e5..483d6bab 100644 --- a/kala-base/src/main/java/kala/Conditions.java +++ b/kala-base/src/main/java/kala/Conditions.java @@ -29,10 +29,6 @@ public final class Conditions { private Conditions() { } - public static void checkElementIndex(int index, @Range(from = 0, to = Integer.MAX_VALUE) int size) throws IndexOutOfBoundsException { - Objects.checkIndex(index, size); - } - public static void checkPositionIndex(int index, @Range(from = 0, to = Integer.MAX_VALUE) int size) throws IndexOutOfBoundsException { if (index < 0 || index > size) { // Optimized for execution by hotspot diff --git a/kala-base/src/main/java/kala/collection/base/GenericArrays.java b/kala-base/src/main/java/kala/collection/base/GenericArrays.java index 13aa1867..311619fa 100644 --- a/kala-base/src/main/java/kala/collection/base/GenericArrays.java +++ b/kala-base/src/main/java/kala/collection/base/GenericArrays.java @@ -266,7 +266,7 @@ public static void set(E @NotNull [] array, int index, E value) { public static E @NotNull [] removedAt(E @NotNull [] array, int index) { final int arrayLength = array.length; // implicit null check of array - Conditions.checkElementIndex(index, arrayLength); + Objects.checkIndex(index, arrayLength); E[] result = newArrayByOldType(array, arrayLength - 1); System.arraycopy(array, 0, result, 0, index); System.arraycopy(array, index + 1, result, index, arrayLength - index - 1); @@ -528,7 +528,7 @@ public static int lastIndexWhere(E @NotNull [] array, @NotNull Predicate E @NotNull [] updated(E @NotNull [] array, int index, E newValue) { - Conditions.checkElementIndex(index, array.length); + Objects.checkIndex(index, array.length); E[] newValues = array.clone(); newValues[index] = newValue; diff --git a/kala-base/src/main/java/kala/collection/base/ObjectArrays.java b/kala-base/src/main/java/kala/collection/base/ObjectArrays.java index 96238731..2e013558 100644 --- a/kala-base/src/main/java/kala/collection/base/ObjectArrays.java +++ b/kala-base/src/main/java/kala/collection/base/ObjectArrays.java @@ -227,7 +227,7 @@ public static void set(Object @NotNull [] array, int index, Object value) { public static Object @NotNull [] removedAt(Object @NotNull [] array, int index) { final int arrayLength = array.length; // implicit null check of array - Conditions.checkElementIndex(index, arrayLength); + Objects.checkIndex(index, arrayLength); Object[] result = new Object[arrayLength - 1]; System.arraycopy(array, 0, result, 0, index); System.arraycopy(array, index + 1, result, index, arrayLength - index - 1); @@ -610,7 +610,7 @@ public static int lastIndexWhere(Object @NotNull [] array, @NotNull Predicate public static Object @NotNull [] updated(Object @NotNull [] array, int index, Object newValue) { final int size = array.length; - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); Object[] newValues = array.clone(); newValues[index] = newValue; diff --git a/kala-base/src/main/java/kala/text/StringSlice.java b/kala-base/src/main/java/kala/text/StringSlice.java index 84cc0dfe..101b35bb 100644 --- a/kala-base/src/main/java/kala/text/StringSlice.java +++ b/kala-base/src/main/java/kala/text/StringSlice.java @@ -28,6 +28,7 @@ import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.*; +import java.util.Objects; public final class StringSlice implements Comparable, CharSequence, Serializable { @Serial @@ -77,7 +78,7 @@ public int length() { @Override public char charAt(int index) { - Conditions.checkElementIndex(index, length); + Objects.checkIndex(index, length); return value.charAt(index + offset); } diff --git a/kala-collection-primitive/src/main/java/kala/collection/mutable/primitive/MutableBooleanArrayList.java b/kala-collection-primitive/src/main/java/kala/collection/mutable/primitive/MutableBooleanArrayList.java index 18e89ac6..4f9dfa42 100644 --- a/kala-collection-primitive/src/main/java/kala/collection/mutable/primitive/MutableBooleanArrayList.java +++ b/kala-collection-primitive/src/main/java/kala/collection/mutable/primitive/MutableBooleanArrayList.java @@ -278,13 +278,13 @@ public int size() { @Override public boolean get(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); return elements[index]; } @Override public void set(int index, boolean newValue) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); elements[index] = newValue; } @@ -476,7 +476,7 @@ public void clear() { @Override public boolean removeAt(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); boolean oldValue = elements[index]; int newSize = size - 1; if (newSize > index) { diff --git a/kala-collection-primitive/src/main/java/kala/collection/primitive/BitArray.java b/kala-collection-primitive/src/main/java/kala/collection/primitive/BitArray.java index 684ac0fc..801a3be7 100644 --- a/kala-collection-primitive/src/main/java/kala/collection/primitive/BitArray.java +++ b/kala-collection-primitive/src/main/java/kala/collection/primitive/BitArray.java @@ -15,7 +15,6 @@ */ package kala.collection.primitive; -import kala.Conditions; import kala.collection.base.primitive.BitArrays; import kala.collection.base.primitive.BooleanIterator; import kala.function.BooleanConsumer; @@ -23,6 +22,7 @@ import java.io.Serial; import java.io.Serializable; +import java.util.Objects; import static kala.collection.base.primitive.BitArrays.BITS_PRE_VALUE; @@ -99,7 +99,7 @@ public final int size() { @Override public final boolean get(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); if (bitsArray == null) { return BitArrays.get(bits, index); } else { diff --git a/kala-collection-primitive/src/main/template/kala/collection/mutable/primitive/MutablePrimitiveArrayList.java.ftl b/kala-collection-primitive/src/main/template/kala/collection/mutable/primitive/MutablePrimitiveArrayList.java.ftl index dc109f23..80db7202 100644 --- a/kala-collection-primitive/src/main/template/kala/collection/mutable/primitive/MutablePrimitiveArrayList.java.ftl +++ b/kala-collection-primitive/src/main/template/kala/collection/mutable/primitive/MutablePrimitiveArrayList.java.ftl @@ -286,13 +286,13 @@ public final class Mutable${Type}ArrayList extends AbstractMutable${Type}List im @Override public ${PrimitiveType} get(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); return elements[index]; } @Override public void set(int index, ${PrimitiveType} newValue) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); elements[index] = newValue; } @@ -496,7 +496,7 @@ public final class Mutable${Type}ArrayList extends AbstractMutable${Type}List im @Override public ${PrimitiveType} removeAt(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); ${PrimitiveType} oldValue = elements[index]; int newSize = size - 1; if (newSize > index) { diff --git a/kala-collection-primitive/src/main/template/kala/collection/primitive/PrimitiveSeqView.java.ftl b/kala-collection-primitive/src/main/template/kala/collection/primitive/PrimitiveSeqView.java.ftl index ab56a5f1..882c4bc3 100644 --- a/kala-collection-primitive/src/main/template/kala/collection/primitive/PrimitiveSeqView.java.ftl +++ b/kala-collection-primitive/src/main/template/kala/collection/primitive/PrimitiveSeqView.java.ftl @@ -159,7 +159,7 @@ public interface ${Type}SeqView extends ${Type}SeqLike, ${Type}CollectionView, P throw new IndexOutOfBoundsException("index(" + index + ") < 0"); } } else { - Conditions.checkElementIndex(index, ks); + Objects.checkIndex(index, ks); } return new ${Type}SeqViews.Updated(this, index, newValue); } diff --git a/kala-collection-primitive/src/main/template/kala/collection/primitive/internal/view/PrimitiveSeqViews.java.ftl b/kala-collection-primitive/src/main/template/kala/collection/primitive/internal/view/PrimitiveSeqViews.java.ftl index 60260f7e..f13cc4e9 100644 --- a/kala-collection-primitive/src/main/template/kala/collection/primitive/internal/view/PrimitiveSeqViews.java.ftl +++ b/kala-collection-primitive/src/main/template/kala/collection/primitive/internal/view/PrimitiveSeqViews.java.ftl @@ -12,6 +12,7 @@ import org.jetbrains.annotations.Range; import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Objects; <#if IsSpecialized> import java.util.function.*; import java.util.stream.${Type}Stream; @@ -218,7 +219,7 @@ public final class ${Type}SeqViews { @Override public final ${PrimitiveType} get(int index) { - Conditions.checkElementIndex(index, size()); + Objects.checkIndex(index, size()); return array[index + beginIndex]; } @@ -574,7 +575,7 @@ public final class ${Type}SeqViews { } final int size = Integer.max(source.size() - n, 0); - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); return this.source.get(index); } diff --git a/kala-collection/src/main/java/kala/collection/ArraySeq.java b/kala-collection/src/main/java/kala/collection/ArraySeq.java index dae7747f..817a70d7 100644 --- a/kala-collection/src/main/java/kala/collection/ArraySeq.java +++ b/kala-collection/src/main/java/kala/collection/ArraySeq.java @@ -30,10 +30,7 @@ import kala.function.IndexedBiConsumer; import kala.function.IndexedConsumer; import kala.function.IndexedFunction; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.Debug; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.*; import java.io.IOException; import java.io.Serial; @@ -462,7 +459,7 @@ public final E get(int index) { final Object[] elements = this.elements; final int size = elements.length; - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); Object[] newValues = elements.clone(); newValues[index] = newValue; @@ -483,7 +480,7 @@ public final E get(int index) { final Object[] elements = this.elements; final int size = elements.length; - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); if (size == 1) { return ImmutableArray.empty(); diff --git a/kala-collection/src/main/java/kala/collection/Seq.java b/kala-collection/src/main/java/kala/collection/Seq.java index f009b46f..9715c402 100644 --- a/kala-collection/src/main/java/kala/collection/Seq.java +++ b/kala-collection/src/main/java/kala/collection/Seq.java @@ -6,8 +6,6 @@ import kala.collection.base.Iterators; import kala.collection.base.OrderedTraversable; import kala.collection.factory.CollectionFactory; -import kala.collection.immutable.AbstractImmutableCollection; -import kala.collection.immutable.ImmutableCollection; import kala.collection.immutable.ImmutableSeq; import kala.collection.internal.convert.AsJavaConvert; import kala.collection.internal.convert.FromJavaConvert; @@ -17,10 +15,7 @@ import kala.tuple.Tuple; import kala.tuple.Tuple2; import kala.tuple.Tuple3; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.UnmodifiableView; +import org.jetbrains.annotations.*; import java.util.*; import java.util.function.*; @@ -195,7 +190,8 @@ static boolean equals(@NotNull Seq seq1, @NotNull AnySeq seq2) { @Override @Contract(pure = true) default @NotNull ImmutableSeq updated(int index, E newValue) { - Conditions.checkElementIndex(index, size()); + @Range(from = 0, to = Integer.MAX_VALUE) int size = size(); + Objects.checkIndex(index, size); return view().updated(index, newValue).toImmutableSeq(); } @@ -256,7 +252,8 @@ static boolean equals(@NotNull Seq seq1, @NotNull AnySeq seq2) { @Override @Contract(pure = true) default @NotNull ImmutableSeq removedAt(int index) { - Conditions.checkElementIndex(index, size()); + @Range(from = 0, to = Integer.MAX_VALUE) int size = size(); + Objects.checkIndex(index, size); return view().removedAt(index).toImmutableSeq(); } diff --git a/kala-collection/src/main/java/kala/collection/SeqView.java b/kala-collection/src/main/java/kala/collection/SeqView.java index 118417d7..b08f8523 100644 --- a/kala-collection/src/main/java/kala/collection/SeqView.java +++ b/kala-collection/src/main/java/kala/collection/SeqView.java @@ -29,6 +29,7 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.Range; import java.util.Comparator; import java.util.LinkedHashSet; @@ -183,7 +184,7 @@ static SeqView narrow(SeqView view) { throw new IndexOutOfBoundsException("index(" + index + ") < 0"); } } else { - Conditions.checkElementIndex(index, ks); + Objects.checkIndex(index, ks); } return new SeqViews.Updated<>(this, index, newValue); } @@ -237,7 +238,8 @@ static SeqView narrow(SeqView view) { } default @NotNull SeqView removedAt(int index) { - Conditions.checkElementIndex(index, size()); + @Range(from = 0, to = Integer.MAX_VALUE) int size = size(); + Objects.checkIndex(index, size); return new SeqViews.RemovedAt<>(this, index); } diff --git a/kala-collection/src/main/java/kala/collection/immutable/AbstractImmutableSeq.java b/kala-collection/src/main/java/kala/collection/immutable/AbstractImmutableSeq.java index 02a6a6da..2d49c4cf 100644 --- a/kala-collection/src/main/java/kala/collection/immutable/AbstractImmutableSeq.java +++ b/kala-collection/src/main/java/kala/collection/immutable/AbstractImmutableSeq.java @@ -42,7 +42,7 @@ static T updated( ) { final int size = seq.size(); - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); Builder builder = factory.newBuilder(); @@ -87,7 +87,7 @@ static T removedAt( @NotNull CollectionFactory factory ) { final int size = seq.size(); - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); if (size == 1) { return factory.empty(); diff --git a/kala-collection/src/main/java/kala/collection/immutable/ImmutableLinkedSeq.java b/kala-collection/src/main/java/kala/collection/immutable/ImmutableLinkedSeq.java index b0849028..e5109f18 100644 --- a/kala-collection/src/main/java/kala/collection/immutable/ImmutableLinkedSeq.java +++ b/kala-collection/src/main/java/kala/collection/immutable/ImmutableLinkedSeq.java @@ -247,7 +247,7 @@ public int knownSize() { @Override public E get(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); Node list = this.node; for (int i = 0; i < index; i++) { list = list.tail; @@ -559,7 +559,7 @@ public final int knownSize() { @Override public final E get(int index) { - Conditions.checkElementIndex(index, len); + Objects.checkIndex(index, len); if (index == len - 1) return last.head; @@ -573,8 +573,8 @@ public final E get(int index) { @Override public final void swap(int index1, int index2) { - Conditions.checkElementIndex(index1, len); - Conditions.checkElementIndex(index2, len); + Objects.checkIndex(index1, len); + Objects.checkIndex(index2, len); if (index1 == index2) { return; } @@ -676,7 +676,7 @@ public void insert(int index, E value) { @Override public final E removeAt(int index) { - Conditions.checkElementIndex(index, len); + Objects.checkIndex(index, len); if (index == 0) { E v = first.head; @@ -721,7 +721,7 @@ public final void clear() { @Override public final void set(int index, E newValue) { - Conditions.checkElementIndex(index, len); + Objects.checkIndex(index, len); ensureUnaliased(); if (index == len - 1) { diff --git a/kala-collection/src/main/java/kala/collection/immutable/ImmutableSeqs.java b/kala-collection/src/main/java/kala/collection/immutable/ImmutableSeqs.java index 4fffc5fb..c844e084 100644 --- a/kala-collection/src/main/java/kala/collection/immutable/ImmutableSeqs.java +++ b/kala-collection/src/main/java/kala/collection/immutable/ImmutableSeqs.java @@ -538,7 +538,7 @@ public final int size() { @Override public final E get(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); return value; } diff --git a/kala-collection/src/main/java/kala/collection/immutable/ImmutableTreeSeq.java b/kala-collection/src/main/java/kala/collection/immutable/ImmutableTreeSeq.java index 05c90dc9..224b4452 100644 --- a/kala-collection/src/main/java/kala/collection/immutable/ImmutableTreeSeq.java +++ b/kala-collection/src/main/java/kala/collection/immutable/ImmutableTreeSeq.java @@ -25,6 +25,7 @@ import java.io.*; import java.util.Iterator; +import java.util.Objects; import java.util.function.IntFunction; import java.util.function.Predicate; import java.util.function.Supplier; @@ -234,13 +235,13 @@ public int knownSize() { @Override public E get(int index) { - Conditions.checkElementIndex(index, size()); + Objects.checkIndex(index, size()); return root.get(index); } @Override public @NotNull ImmutableSeq updated(int index, E newValue) { - Conditions.checkElementIndex(index, size()); + Objects.checkIndex(index, size()); IndexedTree newRoot = root.plus(index, newValue); return newRoot != root ? new ImmutableTreeSeq<>(newRoot) : this; } @@ -264,7 +265,7 @@ public E get(int index) { @Override public @NotNull ImmutableSeq removedAt(int index) { final int size = size(); - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); return size > 1 ? new ImmutableTreeSeq<>(root.minus(index).changeKeysBelow(index, -1)) : empty(); } diff --git a/kala-collection/src/main/java/kala/collection/immutable/ImmutableVectors.java b/kala-collection/src/main/java/kala/collection/immutable/ImmutableVectors.java index 265fadfc..b2a452a9 100644 --- a/kala-collection/src/main/java/kala/collection/immutable/ImmutableVectors.java +++ b/kala-collection/src/main/java/kala/collection/immutable/ImmutableVectors.java @@ -30,7 +30,6 @@ import java.util.function.Function; import java.util.function.Predicate; -import static kala.Conditions.*; import static java.util.Arrays.copyOf; import static kala.collection.base.GenericArrays.copyOrUse; @@ -350,7 +349,7 @@ public E getLast() { public @NotNull ImmutableVector updated(int index, E newValue) { final Object[] prefix1 = this.prefix1; final int size = prefix1.length; - checkElementIndex(index, size); + Objects.checkIndex(index, size); Object[] res = prefix1.clone(); res[index] = newValue; return new ImmutableVectors.Vector1<>(res); @@ -887,7 +886,7 @@ int vectorSlicePrefixLength(int idx) { @Override public E get(int index) { - checkElementIndex(index, length0); + Objects.checkIndex(index, length0); int io = index - len1; if (io >= 0) { @@ -970,7 +969,7 @@ int vectorSlicePrefixLength(int idx) { @Override public E get(int index) { - checkElementIndex(index, length0); + Objects.checkIndex(index, length0); int io = index - len12; if (io >= 0) { @@ -1084,7 +1083,7 @@ int vectorSlicePrefixLength(int idx) { @Override public E get(int index) { - checkElementIndex(index, length0); + Objects.checkIndex(index, length0); int io = index - len123; if (io >= 0) { int i4 = io >>> BITS3; @@ -1218,7 +1217,7 @@ int vectorSlicePrefixLength(int idx) { @Override public E get(int index) { - checkElementIndex(index, length0); + Objects.checkIndex(index, length0); int io = index - len1234; if (io >= 0) { int i5 = io >>> BITS4; @@ -1377,7 +1376,7 @@ int vectorSlicePrefixLength(int idx) { @Override public E get(int index) { - checkElementIndex(index, length0); + Objects.checkIndex(index, length0); int io = index - len12345; if (io >= 0) { int i6 = io >>> BITS5; diff --git a/kala-collection/src/main/java/kala/collection/internal/vector/Vector.java b/kala-collection/src/main/java/kala/collection/internal/vector/Vector.java index e800c2c4..654256ae 100644 --- a/kala-collection/src/main/java/kala/collection/internal/vector/Vector.java +++ b/kala-collection/src/main/java/kala/collection/internal/vector/Vector.java @@ -15,13 +15,14 @@ */ package kala.collection.internal.vector; -import kala.Conditions; import kala.collection.IndexedSeq; import kala.collection.base.ObjectArrays; import kala.collection.immutable.AbstractImmutableSeq; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range; +import java.util.Objects; + @SuppressWarnings("unchecked") public final class Vector extends AbstractImmutableSeq implements IndexedSeq { @@ -58,7 +59,7 @@ public int size() { @Override public E get(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); // fastpath if (depth == 0) { diff --git a/kala-collection/src/main/java/kala/collection/internal/view/SeqViews.java b/kala-collection/src/main/java/kala/collection/internal/view/SeqViews.java index 18469a0c..47cf79b2 100644 --- a/kala-collection/src/main/java/kala/collection/internal/view/SeqViews.java +++ b/kala-collection/src/main/java/kala/collection/internal/view/SeqViews.java @@ -33,10 +33,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Range; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Iterator; -import java.util.NoSuchElementException; +import java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -280,7 +277,7 @@ public final int endIndex() { @Override public final E get(int index) { - Conditions.checkElementIndex(index, size()); + Objects.checkIndex(index, size()); return (E) array[index + beginIndex]; } @@ -587,7 +584,7 @@ public final E get(int index) { } final int size = Integer.max(source.size() - n, 0); - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); return this.source.get(index); } @@ -1099,7 +1096,8 @@ public int knownSize() { @Override public E get(int index) { - Conditions.checkElementIndex(index, size()); + @Range(from = 0, to = Integer.MAX_VALUE) int size = size(); + Objects.checkIndex(index, size); return source.get(index < removedIndex ? index : index -1); } } diff --git a/kala-collection/src/main/java/kala/collection/mutable/MutableArrayDeque.java b/kala-collection/src/main/java/kala/collection/mutable/MutableArrayDeque.java index 0e08c061..61a41ebf 100644 --- a/kala-collection/src/main/java/kala/collection/mutable/MutableArrayDeque.java +++ b/kala-collection/src/main/java/kala/collection/mutable/MutableArrayDeque.java @@ -13,6 +13,7 @@ import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.function.IntFunction; import java.util.function.Predicate; import java.util.function.Supplier; @@ -299,10 +300,10 @@ public E get(int index) { if (isEmpty()) { throw new IndexOutOfBoundsException("Index out of range: " + index); } else if (begin < end) { - Conditions.checkElementIndex(index, end - begin); + Objects.checkIndex(index, end - begin); return (E) elements[begin + index]; } else { - Conditions.checkElementIndex(index, elements.length - begin + end); + Objects.checkIndex(index, elements.length - begin + end); return (E) elements[inc(begin, index, elements.length)]; } } @@ -312,11 +313,11 @@ public void set(int index, E newValue) { if (isEmpty()) { throw new IndexOutOfBoundsException(); } else if (begin < end) { - Conditions.checkElementIndex(index, end - begin); + Objects.checkIndex(index, end - begin); elements[begin + index] = newValue; } else { final int size = elements.length - begin + end; - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); elements[inc(begin, index, elements.length)] = newValue; } } @@ -416,7 +417,7 @@ public void insertAll(int index, E @NotNull [] values) { @Override public E removeAt(int index) { final int oldSize = size(); - Conditions.checkElementIndex(index, oldSize); + Objects.checkIndex(index, oldSize); if (index == 0) { return removeFirst(); diff --git a/kala-collection/src/main/java/kala/collection/mutable/MutableArrayList.java b/kala-collection/src/main/java/kala/collection/mutable/MutableArrayList.java index 9d17d8c0..f53dff2a 100644 --- a/kala-collection/src/main/java/kala/collection/mutable/MutableArrayList.java +++ b/kala-collection/src/main/java/kala/collection/mutable/MutableArrayList.java @@ -311,13 +311,13 @@ public int size() { @Override public E get(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); return (E) elements[index]; } @Override public void set(int index, E newValue) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); elements[index] = newValue; } @@ -532,7 +532,7 @@ public void clear() { @Override public E removeAt(int index) { - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); E oldValue = (E) elements[index]; int newSize = size - 1; if (newSize > index) { diff --git a/kala-collection/src/main/java/kala/collection/mutable/MutableArraySliceView.java b/kala-collection/src/main/java/kala/collection/mutable/MutableArraySliceView.java index e7d4d2dc..f30f88b4 100644 --- a/kala-collection/src/main/java/kala/collection/mutable/MutableArraySliceView.java +++ b/kala-collection/src/main/java/kala/collection/mutable/MutableArraySliceView.java @@ -4,6 +4,8 @@ import kala.collection.internal.view.SeqViews; import org.jetbrains.annotations.NotNull; +import java.util.Objects; + public final class MutableArraySliceView extends SeqViews.OfArraySlice { MutableArraySliceView(Object[] array, int beginIndex, int endIndex) { super(array, beginIndex, endIndex); @@ -15,7 +17,7 @@ public final class MutableArraySliceView extends SeqViews.OfArraySlice { } public void set(int index, E value) { - Conditions.checkElementIndex(index, endIndex - beginIndex); + Objects.checkIndex(index, endIndex - beginIndex); array[beginIndex + index] = value; } diff --git a/kala-collection/src/main/java/kala/collection/mutable/MutableLinkedList.java b/kala-collection/src/main/java/kala/collection/mutable/MutableLinkedList.java index 12f66087..fce489ba 100644 --- a/kala-collection/src/main/java/kala/collection/mutable/MutableLinkedList.java +++ b/kala-collection/src/main/java/kala/collection/mutable/MutableLinkedList.java @@ -21,10 +21,7 @@ import kala.function.IndexedFunction; import kala.collection.factory.CollectionFactory; import kala.collection.base.AbstractIterator; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.Debug; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.*; import java.io.Serial; import java.io.Serializable; @@ -286,7 +283,7 @@ void internalUnlink(@NotNull Node node) { } public Node getNode(int index) { - Conditions.checkElementIndex(index, len); + Objects.checkIndex(index, len); return internalGetNode(index); } @@ -303,7 +300,7 @@ public int getIndex(Node node) { @Override public E get(int index) { - Conditions.checkElementIndex(index, len); + Objects.checkIndex(index, len); return internalGetNode(index).value; } @@ -482,7 +479,7 @@ public void insertAfter(@NotNull Node node, @NotNull Node newNode, E newVa @Override public E removeAt(int index) { - Conditions.checkElementIndex(index, len); + Objects.checkIndex(index, len); final Node node = internalGetNode(index); final E value = node.value; @@ -529,7 +526,7 @@ public void clear() { @Override public void set(int index, E newValue) { - Conditions.checkElementIndex(index, len); + Objects.checkIndex(index, len); internalGetNode(index).value = newValue; } diff --git a/kala-collection/src/main/java/kala/collection/mutable/MutableSmartArrayList.java b/kala-collection/src/main/java/kala/collection/mutable/MutableSmartArrayList.java index f3558250..d0ee66e5 100644 --- a/kala-collection/src/main/java/kala/collection/mutable/MutableSmartArrayList.java +++ b/kala-collection/src/main/java/kala/collection/mutable/MutableSmartArrayList.java @@ -271,7 +271,7 @@ public int size() { @Override public E get(int index) { final int size = this.size; - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); if (size == 1) { return (E) elem; } else { @@ -282,7 +282,7 @@ public E get(int index) { @Override public void set(int index, E newValue) { final int size = this.size; - Conditions.checkElementIndex(index, size); + Objects.checkIndex(index, size); if (size == 1) { elem = newValue; } else { @@ -328,7 +328,7 @@ public void insert(int index, E value) { @Override public E removeAt(int index) { final int oldSize = this.size; - Conditions.checkElementIndex(index, oldSize); + Objects.checkIndex(index, oldSize); E res; if (oldSize == 1) {