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 (Sergey Dorofeev) #29

Open
wants to merge 1 commit 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
136 changes: 125 additions & 11 deletions src/test/java/option/OptionalExample.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package option;

import org.junit.Test;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.function.Predicate;

import static org.junit.Assert.assertEquals;

Expand All @@ -18,15 +18,13 @@ public void get() {

o1.orElse("t");
o1.orElseGet(() -> "t");
o1.orElseThrow(() -> new UnsupportedOperationException());
// o1.orElseThrow(() -> new UnsupportedOperationException());
}

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

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

if (o1.isPresent()) {
System.out.println(o1.get());
}
Expand All @@ -35,12 +33,10 @@ public void ifPresent() {
@Test
public void map() {
final Optional<String> o1 = getOptional();

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

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

final Optional<Integer> actual;

if (o1.isPresent()) {
actual = Optional.of(getLength.apply(o1.get()));
} else {
Expand All @@ -50,9 +46,127 @@ public void map() {
assertEquals(expected, actual);
}

@Test
public void flatMap() {
final Optional<String> o1 = getOptional();
final Function<String, Optional<Integer>> getLengthOpt = s -> Optional.of(s.length());
final Optional<Integer> expected = o1.flatMap(getLengthOpt);
final Optional<Integer> actual;

if (o1.isPresent()) {
actual = Optional.of(getLengthOpt.apply(o1.get()).get());
Copy link
Contributor

Choose a reason for hiding this comment

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

Incorrect.

} else {
actual = Optional.empty();
}

assertEquals(expected, actual);
}

@Test
public void filter() {
final Optional<String> o1 = getOptional();
Predicate<String> predicate = p -> p.startsWith("a");
final Optional<String> expected = o1.filter(predicate);
final Optional<String> actual;

if (o1.isPresent()) {
actual = predicate.test(o1.get()) ? Optional.of(o1.get()) : Optional.empty();
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional.of(opt.get()) == opt

} else {
actual = Optional.empty();
}

assertEquals(expected, actual);
}

@Test
public void orElse() {
final Optional<String> o1 = getOptional();
String expected = o1.orElse("W");
String actual;

if (o1.isPresent()) {
actual = o1.get();
} else {
actual = "W";
}
assertEquals(expected, actual);
}

@Test
public void orElseGet() {
final Optional<String> o1 = getOptional();
String expected = o1.orElseGet(() -> "what");
String actual;

if (o1.isPresent()) {
actual = o1.get();
} else {
actual = "what";
}
assertEquals(expected, actual);
}

@Test
public void orElseThrow() {
final Optional<String> o1 = getOptional();
String expected = null;
try {
expected = o1.orElseThrow(() -> new Exception("what"));
} catch (Exception e) {
assertEquals(e.getMessage(), "what");
return;
}
String actual;
actual = o1.get();

assertEquals(expected, actual);
}

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

String expected = o1.toString();
String actual;
if (o1.isPresent()) {
actual = "Optional[" + o1.get() + "]";
} else {
actual = "Optional.empty";
}

assertEquals(expected, actual);
}

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

Integer expected = o1.hashCode();
Integer actual;
if (o1.isPresent()) {
actual = o1.get().hashCode();
} else {
actual = 0;
}

assertEquals(expected, actual);
}

@Test
public void equalsOpt() {
final Optional<String> o1 = getOptional();
Optional<String> o2 = Optional.of("abc");
boolean expected = o1.equals(o2);
boolean actual;
if (o1.isPresent()) {
actual = o1.get().equals(o2.get());
} else {
actual = !o2.isPresent();
}
assertEquals(expected, actual);
}

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