Skip to content

Commit

Permalink
Update OrderedTraversable::isDefinedAt(int)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Dec 30, 2024
1 parent 0356a6d commit bff3ffe
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public interface OrderedTraversable<T> extends Traversable<T> {
return res;
}

@Contract(pure = true)
default boolean isDefinedAt(@Index int index) {
if (index >= 0) {
return sizeGreaterThan(index);
} else {
return index != ~0 && sizeGreaterThanOrEquals(~index);
}
}

//region Element Retrieval Operations

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ public final class ${Type}SeqViews {
return source.get(index);
}

public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

public int indexOf(${PrimitiveType} value) {
return source.indexOf(value);
}
Expand Down Expand Up @@ -477,11 +473,6 @@ public final class ${Type}SeqViews {
return source.knownSize();
}

@Override
public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

@Override
public final ${PrimitiveType} get(int index) {
if (index == this.index) {
Expand Down Expand Up @@ -1094,11 +1085,6 @@ public final class ${Type}SeqViews {
return source.knownSize();
}

@Override
public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

@Override
public final E get(int index) {
return mapper.apply(source.get(index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ default boolean supportsFastRandomAccess() {
return true;
}

@Override
default boolean isDefinedAt(int index) {
return index >= 0 && index < size();
}

@Override
E get(int index);

Expand Down
5 changes: 0 additions & 5 deletions kala-collection/src/main/java/kala/collection/SeqLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ static <E> SeqLike<E> narrow(SeqLike<? extends E> view) {

//region Positional Access Operations

@Contract(pure = true)
default boolean isDefinedAt(int index) {
return index >= 0 && sizeGreaterThan(index);
}

@Override
default E elementAt(int index) {
return get(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ public E get(int index) {
return source.getOption(index);
}

public boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

public int indexOf(Object value) {
return source.indexOf(value);
}
Expand Down Expand Up @@ -1317,11 +1313,6 @@ public final int knownSize() {

//endregion

@Override
public final boolean isDefinedAt(int index) {
return source.isDefinedAt(index);
}

@Override
public final E get(int index) {
return mapper.apply(index, source.get(index));
Expand Down
5 changes: 4 additions & 1 deletion src/test/template/kala/collection/SeqLikeTestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ default void isDefinedAtTest() {

for (Integer[] data : data1()) {
SeqLike<Integer> seq = from(data);
assertFalse(seq.isDefinedAt(-1));
assertFalse(seq.isDefinedAt(~0));
assertFalse(seq.isDefinedAt(Integer.MIN_VALUE));

for (int i = 0; i < data.length; i++) {
assertTrue(seq.isDefinedAt(i));
assertTrue(seq.isDefinedAt(~(i + 1)));
}

assertFalse(seq.isDefinedAt(~(data.length + 1)));
assertFalse(seq.isDefinedAt(data.length));
assertFalse(seq.isDefinedAt(Integer.MAX_VALUE));
}
Expand Down

0 comments on commit bff3ffe

Please sign in to comment.