Skip to content

Commit

Permalink
CAUSEWAY-2297: any/all match support for Can<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Nov 13, 2024
1 parent aa6aa2b commit b3bd742
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,18 @@ default Can<T> addUnique(final @Nullable T element) {
*/
int indexOf(@Nullable T element);

// -- MATCH

/**
* Returns whether any elements of this {@link Can} match the provided
* predicate.
*/
boolean anyMatch(final Predicate<? super T> predicate);
/**
* Returns whether all elements of this stream match the provided predicate.
*/
boolean allMatch(final Predicate<? super T> predicate);

// -- EQUALITY

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ public int indexOf(final @Nullable T element) {
return -1;
}

@Override
public boolean anyMatch(final Predicate<? super T> predicate) {
return false;
}
@Override
public boolean allMatch(final Predicate<? super T> predicate) {
return true;
}

@Override
public String toString() {
return "Can[]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,19 @@ public int indexOf(final @Nullable T element) {
return this.elements.indexOf(element);
}

@Override
public boolean anyMatch(final Predicate<? super T> predicate) {
// equivalent to stream().anyMatch(predicate); hoping the loop is faster..
for(var el : elements) if(predicate.test(el)) return true;
return false;
}
@Override
public boolean allMatch(final Predicate<? super T> predicate) {
// equivalent to stream().allMatch(predicate); hoping the loop is faster..
for(var el : elements) if(!predicate.test(el)) return false;
return true;
}

@Override
public String toString() {
var literal = stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@ public int indexOf(final @Nullable T element) {
return this.element.equals(element) ? 0 : -1;
}

@Override
public boolean anyMatch(final Predicate<? super T> predicate) {
return predicate.test(element);
}
@Override
public boolean allMatch(final Predicate<? super T> predicate) {
return predicate.test(element);
}

@Override
public String toString() {
return "Can["+element+"]";
Expand Down

0 comments on commit b3bd742

Please sign in to comment.