Skip to content

Commit

Permalink
Delete Conditions.checkElementIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Nov 20, 2024
1 parent b5a0ad3 commit df711df
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 84 deletions.
4 changes: 0 additions & 4 deletions kala-base/src/main/java/kala/Conditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public static <E> void set(E @NotNull [] array, int index, E value) {

public static <E> 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);
Expand Down Expand Up @@ -528,7 +528,7 @@ public static <E> int lastIndexWhere(E @NotNull [] array, @NotNull Predicate<? s
}

public static <E> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion kala-base/src/main/java/kala/text/StringSlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringSlice>, CharSequence, Serializable {
@Serial
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package kala.collection.primitive;

import kala.Conditions;
import kala.collection.base.primitive.BitArrays;
import kala.collection.base.primitive.BooleanIterator;
import kala.function.BooleanConsumer;
import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

import static kala.collection.base.primitive.BitArrays.BITS_PRE_VALUE;

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 3 additions & 6 deletions kala-collection/src/main/java/kala/collection/ArraySeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
13 changes: 5 additions & 8 deletions kala-collection/src/main/java/kala/collection/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -195,7 +190,8 @@ static boolean equals(@NotNull Seq<?> seq1, @NotNull AnySeq<?> seq2) {
@Override
@Contract(pure = true)
default @NotNull ImmutableSeq<E> 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();
}

Expand Down Expand Up @@ -256,7 +252,8 @@ static boolean equals(@NotNull Seq<?> seq1, @NotNull AnySeq<?> seq2) {
@Override
@Contract(pure = true)
default @NotNull ImmutableSeq<E> 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();
}

Expand Down
6 changes: 4 additions & 2 deletions kala-collection/src/main/java/kala/collection/SeqView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -183,7 +184,7 @@ static <E> SeqView<E> narrow(SeqView<? extends E> view) {
throw new IndexOutOfBoundsException("index(" + index + ") < 0");
}
} else {
Conditions.checkElementIndex(index, ks);
Objects.checkIndex(index, ks);
}
return new SeqViews.Updated<>(this, index, newValue);
}
Expand Down Expand Up @@ -237,7 +238,8 @@ static <E> SeqView<E> narrow(SeqView<? extends E> view) {
}

default @NotNull SeqView<E> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static <E, T, Builder> T updated(
) {
final int size = seq.size();

Conditions.checkElementIndex(index, size);
Objects.checkIndex(index, size);

Builder builder = factory.newBuilder();

Expand Down Expand Up @@ -87,7 +87,7 @@ static <E, T, Builder> T removedAt(
@NotNull CollectionFactory<? super E, Builder, ? extends T> factory
) {
final int size = seq.size();
Conditions.checkElementIndex(index, size);
Objects.checkIndex(index, size);

if (size == 1) {
return factory.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public int knownSize() {

@Override
public E get(int index) {
Conditions.checkElementIndex(index, size);
Objects.checkIndex(index, size);
Node<E> list = this.node;
for (int i = 0; i < index; i++) {
list = list.tail;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<E> updated(int index, E newValue) {
Conditions.checkElementIndex(index, size());
Objects.checkIndex(index, size());
IndexedTree<E> newRoot = root.plus(index, newValue);
return newRoot != root ? new ImmutableTreeSeq<>(newRoot) : this;
}
Expand All @@ -264,7 +265,7 @@ public E get(int index) {
@Override
public @NotNull ImmutableSeq<E> 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();
}

Expand Down
Loading

0 comments on commit df711df

Please sign in to comment.