Skip to content

Commit

Permalink
Merge pull request #147 from bejibx/master
Browse files Browse the repository at this point in the history
Add Optional#mapToBoolean(ToBooleanFunction) method
  • Loading branch information
aNNiMON committed Oct 5, 2017
2 parents 8f3f5a6 + 0fa2379 commit 125ed63
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
18 changes: 17 additions & 1 deletion stream/src/main/java/com/annimon/stream/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.annimon.stream.function.ToDoubleFunction;
import com.annimon.stream.function.ToIntFunction;
import com.annimon.stream.function.ToLongFunction;
import com.annimon.stream.function.ToBooleanFunction;

import java.util.NoSuchElementException;

/**
Expand Down Expand Up @@ -237,7 +239,21 @@ public OptionalDouble mapToDouble(ToDoubleFunction<? super T> mapper) {
if (!isPresent()) return OptionalDouble.empty();
return OptionalDouble.of(mapper.applyAsDouble(value));
}


/**
* Invokes mapping function on inner value if present.
*
* @param mapper mapping function
* @return an {@code OptionalBoolean} with transformed value if present,
* otherwise an empty {@code OptionalBoolean}
* @throws NullPointerException if value is present and
* {@code mapper} is {@code null}
*/
public OptionalBoolean mapToBoolean(ToBooleanFunction<? super T> mapper) {
if (!isPresent()) return OptionalBoolean.empty();
return OptionalBoolean.of(mapper.applyAsBoolean(value));
}

/**
* Invokes mapping function with {@code Optional} result if value is present.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.annimon.stream.function;

/**
* Represents a function which produces an {@code boolean}-valued result from input argument.
*
* @param <T> the type of the input of the function
* @see Function
*/
public interface ToBooleanFunction<T> {

/**
* Applies this function to the given argument.
*
* @param t an argument
* @return the function result
*/
boolean applyAsBoolean(T t);
}
19 changes: 19 additions & 0 deletions stream/src/test/java/com/annimon/stream/OptionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.annimon.stream.function.Function;
import com.annimon.stream.function.Predicate;
import com.annimon.stream.function.Supplier;
import com.annimon.stream.function.ToBooleanFunction;
import com.annimon.stream.function.ToDoubleFunction;
import com.annimon.stream.function.ToIntFunction;
import com.annimon.stream.function.ToLongFunction;
import com.annimon.stream.function.UnaryOperator;
import com.annimon.stream.test.hamcrest.OptionalBooleanMatcher;
import com.annimon.stream.test.hamcrest.OptionalDoubleMatcher;
import com.annimon.stream.test.hamcrest.OptionalIntMatcher;
import com.annimon.stream.test.hamcrest.OptionalLongMatcher;
Expand Down Expand Up @@ -309,6 +311,23 @@ public double applyAsDouble(String t) {
assertThat(result, OptionalDoubleMatcher.hasValueThat(closeTo(0.65, 0.0001)));
}

@Test
public void testMapToBoolean() {
final ToBooleanFunction<String> mapper = new ToBooleanFunction<String>() {
@Override
public boolean applyAsBoolean(String s) {
return "true".equalsIgnoreCase(s);
}
};

OptionalBoolean result;
result = Optional.<String>empty().mapToBoolean(mapper);
assertThat(result, OptionalBooleanMatcher.isEmpty());

result = Optional.of("true").mapToBoolean(mapper);
assertThat(result, OptionalBooleanMatcher.hasValueThat(is(true)));
}

@Test
public void testFlatMapAsciiToString() {
Optional<String> result = Optional.of(65)
Expand Down

0 comments on commit 125ed63

Please sign in to comment.