Skip to content

Instantly share code, notes, and snippets.

@kinjouj
Last active September 26, 2015 12:05
Show Gist options
  • Save kinjouj/93c44bad7cb66ea35ae5 to your computer and use it in GitHub Desktop.
Save kinjouj/93c44bad7cb66ea35ae5 to your computer and use it in GitHub Desktop.
package sample;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import com.annimon.stream.Collectors;
import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.annimon.stream.function.Consumer;
import com.annimon.stream.function.Predicate;
import org.junit.Test;
import static org.junit.Assert.*;
public class SampleTest {
@Test
public void test1() {
List<String> values = Stream
.of(new String[] { "hoge", "fuga", "foobar" })
.filter(
new Predicate<String>() {
@Override
public boolean test(String value) {
return value.length() == 4;
}
}
)
.sorted()
.collect(Collectors.<String>toList());
assertEquals(2, values.size());
assertEquals("fuga", values.get(0));
assertEquals("hoge", values.get(1));
}
@Test
public void testOptional() {
Optional<String> opt1 = Optional.of("hoge");
assertTrue(opt1.isPresent());
// orElseGet(Supplier)も使える
assertEquals("hoge", opt1.orElse("fuga"));
Optional<String> opt2 = Optional.ofNullable(null);
assertFalse(opt2.isPresent());
assertEquals("fuga", opt2.orElse("fuga"));
}
@Test(expected = NullPointerException.class)
public void testOptional_of_null() {
Optional.of(null);
}
@Test(expected = NoSuchElementException.class)
public void testOptional_get() {
Optional<String> opt = Optional.ofNullable(null);
opt.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment