Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional (Vinnichek T) #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 117 additions & 8 deletions src/test/java/option/OptionalExample.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package option;

import com.google.common.base.Strings;
import org.junit.Test;

import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static org.junit.Assert.assertEquals;

public class OptionalExample {

public static <T1, T2, R> Optional<R> zipMap(Optional<T1> o1, Optional<T2> o2, BiFunction<T1, T2, R> f) {
throw new UnsupportedOperationException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

}

@Test
public void get() {
final Optional<String> o1 = Optional.empty();
Optional<String> o1 = Optional.empty();

o1.ifPresent(s -> System.out.println(s));

Expand All @@ -23,7 +31,7 @@ public void get() {

@Test
public void ifPresent() {
final Optional<String> o1 = getOptional();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for removing all final?

Optional<String> o1 = getOptional();

o1.ifPresent(System.out::println);

Expand All @@ -34,13 +42,13 @@ public void ifPresent() {

@Test
public void map() {
final Optional<String> o1 = getOptional();
Optional<String> o1 = getOptional();

final Function<String, Integer> getLength = String::length;
Function<String, Integer> getLength = String::length;

final Optional<Integer> expected = o1.map(getLength);
Optional<Integer> expected = o1.map(getLength);

final Optional<Integer> actual;
Optional<Integer> actual;
if (o1.isPresent()) {
actual = Optional.of(getLength.apply(o1.get()));
} else {
Expand All @@ -50,9 +58,110 @@ public void map() {
assertEquals(expected, actual);
}


@Test
public void filter() {
Optional<String> opt = getOptional();

Predicate<String> test = s -> s.equals("fffff");

Optional<String> expected = opt.filter(test);

Optional<String> actual;
if (opt.isPresent()) {
if (test.test(opt.get())) {
actual = opt;
} else {
actual = Optional.empty();
}
} else {
actual = Optional.empty();
}
assertEquals(expected, actual);
}

@Test
public void flatMap() {

Optional<String> opt = this.getOptional();

Function<String, Optional<Integer>> getLength = s -> Optional.of(s.length());

Optional<Integer> expected = opt.flatMap(getLength);

Optional<Integer> actual;
if (opt.isPresent()) {
actual = getLength.apply(opt.get());
} else {
actual = Optional.empty();
}
assertEquals(expected, actual);
}

@Test
public void orElse() {

Optional<String> opt = this.getOptional();

String exp = "fffff";

String expected = opt.orElse(exp);

String actual;

if (opt.isPresent()) {
actual = opt.get();
} else {
actual = exp;
}
assertEquals(expected, actual);
}

@Test
public void orElseGet() {
Optional<String> opt = getOptional();

Supplier<String> supplier = () -> "ffff";
String expected = opt.orElseGet(supplier);

String actual;

if (opt.isPresent()) {
actual = opt.get();
} else {
actual = supplier.get();
}

assertEquals(expected, actual);
}

@Test
public void orElseThrow() {

Optional<String> opt = this.getOptional();
Supplier<Exception> exception = () -> new Exception("message");
Optional<String> actual;

try {
Optional<String> expected = Optional.of(opt.orElseThrow(exception));
if (opt.isPresent()) {
actual = Optional.of(opt.get());
assertEquals(expected, actual);
} else {
actual = Optional.empty();
assertEquals(expected, actual);
}
} catch (Exception e) {
System.out.println(e.getMessage());
assertEquals("message", e.getMessage());
}

}


private Optional<String> getOptional() {
return ThreadLocalRandom.current().nextBoolean()
? Optional.empty()
: Optional.of("abc");
? Optional.empty()
: Optional.of("abc");
}
}