Skip to content

Commit

Permalink
Add LeftProjection#contains
Browse files Browse the repository at this point in the history
  • Loading branch information
2bllw8 committed Jun 28, 2022
1 parent dd54999 commit a0e0bfd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/src/main/java/exe/bbllw8/either/Left.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ private LeftToLeftProjection(A value) {
this.value = value;
}

@Override
public boolean contains(B elem) {
return Objects.equals(value, elem);
}

@Override
public boolean exists(Function<A, Boolean> predicate) {
return predicate.apply(value);
Expand Down
11 changes: 11 additions & 0 deletions lib/src/main/java/exe/bbllw8/either/LeftProjection.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public abstract class LeftProjection<A, B> {
/* package */ LeftProjection() {
}

/**
* Returns true if this holds a {@link Left} value and its value is equal to elem (as determined
* by {@link Object#equals(Object)}), returns false otherwise.
*
* @param elem The element to test.
* @return <code>true</code> if this holds a {@link Left} value that is equal to
* <code>elem</code>
* @since 3.4.0
*/
public abstract boolean contains(B elem);

/**
* @return Returns false if {@link Right} or returns the result of the application of the given
* function to the {@link Left} value.
Expand Down
5 changes: 5 additions & 0 deletions lib/src/main/java/exe/bbllw8/either/Right.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ private RightToLeftProjection(B value) {
this.value = value;
}

@Override
public boolean contains(B elem) {
return false;
}

@Override
public boolean exists(Function<A, Boolean> predicate) {
return false;
Expand Down
10 changes: 10 additions & 0 deletions lib/src/test/java/exe/bbllw8/either/LeftProjectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

public class LeftProjectionTest {

@Test
public void contains() {
Assert.assertTrue("Contains should return true if the values are equal",
new Left<>(12).left().contains(12));
Assert.assertFalse("Contains should return false if the values are not equal",
new Left<>(7).left().contains(10));
Assert.assertFalse("Exists should return false if the projection is from a Right",
new Right<Integer, Integer>(12).left().contains(12));
}

@Test
public void exists() {
Assert.assertTrue("Exists should return true if the predicate is satisfied",
Expand Down

0 comments on commit a0e0bfd

Please sign in to comment.