Created
July 19, 2014 05:14
-
-
Save mike-neck/3b45aceca4e377f9a789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package shibuya.lt; | |
import org.junit.Ignore; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.experimental.categories.Category; | |
import org.junit.rules.TestName; | |
import shibuya.categories.NormalTests; | |
import shibuya.categories.SlowTests; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.text.SimpleDateFormat; | |
import java.time.LocalDateTime; | |
import java.util.*; | |
import java.util.concurrent.CopyOnWriteArrayList; | |
import java.util.concurrent.atomic.AtomicReference; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
import static org.hamcrest.CoreMatchers.hasItems; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
import static org.junit.Assert.fail; | |
import static org.junit.Assume.assumeThat; | |
/** | |
* Copyright 2014Shinya Mochida | |
* <p> | |
* Licensed under the Apache License,Version2.0(the"License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* <p> | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* <p> | |
* Unless required by applicable law or agreed to in writing,software | |
* Distributed under the License is distributed on an"AS IS"BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
public class ThisIsSample { | |
@Rule | |
public final TestName testName = new TestName(); | |
@Test | |
@Category(NormalTests.class) | |
public void collectionからStreamを作る () { | |
List<String> list = Arrays.asList("foge", "foo", "for"); | |
list.stream() | |
.forEach(item -> assertThat(item.startsWith("fo"), is(true))); | |
} | |
@Test | |
@Category(NormalTests.class) | |
public void 配列からStreamを作る () { | |
Stream.of("Foge", "Foo", "For") | |
.forEach(item -> assertThat(item.startsWith("Fo"), is(true))); | |
} | |
@Test | |
@Category(NormalTests.class) | |
public void supplierによってStreamを作る () { | |
final int m = Integer.parseInt(new SimpleDateFormat("M").format(new Date())); | |
Stream.generate(LocalDateTime::now) | |
.limit(2L) | |
.forEach(item -> assertThat(item.getMonth().getValue(), is(m))); | |
} | |
@Test | |
@Category(NormalTests.class) | |
public void initialValueとUnaryOperatorによってStreamを生成する () { | |
Iterator<Integer> fibonacci = Arrays.asList(1, 2, 3, 5, 8).iterator(); | |
Stream.iterate(new Fibonacci(), Fibonacci::next) | |
.limit(5L) | |
.forEach(fib -> assertThat(fib.value(), is(fibonacci.next()))); | |
} | |
class Fibonacci { | |
private final int old; | |
private final int now; | |
Fibonacci() { | |
this.old = 1; | |
this.now = 1; | |
} | |
Fibonacci (int old, int now) { | |
this.old = now; | |
this.now = old + now; | |
} | |
Fibonacci next() { | |
return new Fibonacci(old, now); | |
} | |
int value () { | |
return now; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof Fibonacci)) return false; | |
Fibonacci fibonacci = (Fibonacci) o; | |
return now == fibonacci.now; | |
} | |
@Override | |
public int hashCode() { | |
return now; | |
} | |
@Override | |
public String toString() { | |
return "Fibonacci[" + now + "]"; | |
} | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void intの配列からStreamを作る () { | |
FibonacciSequence fibonacci = new FibonacciSequence(); | |
IntStream.of(1, 2, 3, 5, 8) | |
.forEach(item -> assertThat(item, is(fibonacci.next().value()))); | |
} | |
class FibonacciSequence { | |
private final AtomicReference<Fibonacci> fibonacci; | |
FibonacciSequence() { | |
fibonacci = new AtomicReference<>(new Fibonacci()); | |
} | |
Fibonacci next () { | |
return fibonacci.getAndSet(fibonacci.get().next()); | |
} | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 範囲を指定してStreamを作る () { | |
Iterator<Integer> items = Arrays.asList(0, 1, 2, 3, 4).iterator(); | |
IntStream.range(0, 5) | |
.forEach(item -> assertThat(item, is(items.next()))); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 範囲を指定してStreamを作る_末端を含む () { | |
Iterator<Integer> items = Arrays.asList(0, 1, 2, 3, 4, 5).iterator(); | |
IntStream.rangeClosed(0, 5) | |
.forEach(item -> assertThat(item, is(items.next()))); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 空のStreamを作る () { | |
long count = IntStream.empty().count(); | |
assertThat(count, is(0L)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void readerから作る () { | |
InputStream stream = getClass().getClassLoader().getResourceAsStream("shibuya.txt"); | |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { | |
reader.lines() | |
.forEach(item -> assertThat(item.contains("groovy"), is(true))); | |
} catch (IOException e) { | |
fail(e.getMessage()); | |
} | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void オブジェクトを変換する () { | |
Iterator<String> stands = Arrays.asList("ハーミットパープル", "スタープラチナ", "ストーンフリー").iterator(); | |
Stream.of(Jojo.values()) | |
.map(Jojo::stand) | |
.forEach(stand -> assertThat(stand, is(stands.next()))); | |
} | |
enum Jojo { | |
ジョセフジョースター { | |
@Override String stand() { | |
return "ハーミットパープル"; | |
} | |
@Override int birthOfYear() { | |
return 1920; | |
} | |
}, 空条承太郎 { | |
@Override String stand() { | |
return "スタープラチナ"; | |
} | |
@Override int birthOfYear() { | |
return 1970; | |
} | |
}, 空条徐倫 { | |
@Override String stand() { | |
return "ストーンフリー"; | |
} | |
@Override int birthOfYear() { | |
return 1992; | |
} | |
}; | |
abstract String stand(); | |
abstract int birthOfYear(); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 要素を選択する_filter () { | |
Iterator<Integer> numbers = Arrays.asList(1, 3, 5, 7, 9).iterator(); | |
IntStream.range(1, 10) | |
.filter(number -> number % 2 != 0) | |
.forEach(number -> assertThat(number, is(numbers.next()))); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 要素を選択する_distinct () { | |
Iterator<Integer> numbers = Arrays.asList(1, 2, 3).iterator(); | |
IntStream.of(1,1,1,2,2,3,1,2,3,1,2,3) | |
.distinct() | |
.forEach(number -> assertThat(number, is(numbers.next()))); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 要素を選択する_skip () { | |
Iterator<Integer> numbers = Arrays.asList(4, 5, 6).iterator(); | |
IntStream.iterate(1, now -> now + 1) | |
.limit(6L) | |
.skip(3L) | |
.forEach(number -> assertThat(number, is(numbers.next()))); | |
} | |
@Ignore | |
@Category(NormalTests.class) | |
@Test | |
public void 並行にする () { | |
Iterator<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5).iterator(); | |
IntStream.range(1, 6) | |
.parallel() | |
.forEach(number -> assertThat(number, is(numbers.next()))); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 逐次にする () { | |
Iterator<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5).iterator(); | |
IntStream.range(1, 6) | |
.parallel() | |
.sequential() | |
.forEach(number -> assertThat(number, is(numbers.next()))); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 並び替える () { | |
Iterator<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5).iterator(); | |
Stream.of(1, 5, 4, 3, 2) | |
.sorted() | |
.forEach(number -> assertThat(number, is(numbers.next()))); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 何もしない () { | |
System.out.println(testName.getMethodName()); | |
List<Integer> list = Arrays.asList(1, 5, 4, 3, 2); | |
Iterator<Integer> numbers = list.iterator(); | |
IntStream.of(1, 5, 4, 3, 2) | |
.peek(number -> assumeThat(list.contains(number), is(true))) | |
.peek(System.out::println) | |
.forEach(number -> assertThat(number, is(numbers.next()))); | |
System.out.println(testName.getMethodName()); | |
} | |
@Category(SlowTests.class) | |
@Test | |
public void 処理をして終了する () { | |
System.out.println(testName.getMethodName()); | |
IntStream.of(5, 4, 2, 3, 1) | |
.parallel() | |
.peek(number -> { | |
try { | |
Thread.sleep(number * 100L); | |
} catch (InterruptedException ignore) { | |
} | |
}) | |
.forEachOrdered(System.out::println); | |
System.out.println(testName.getMethodName()); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 数える () { | |
long count = IntStream.range(1, 5).count(); | |
assertThat(count, is(4L)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 最大値がある () { | |
Optional<Jojo> jojo = Stream.of(Jojo.values()) | |
.max((a, b) -> a.birthOfYear() - b.birthOfYear()); | |
assertThat(jojo.get(), is(Jojo.空条徐倫)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 最小値がない () { | |
Optional<Object> min = Stream.empty() | |
.min((o1, o2) -> 0); | |
assertThat(min.isPresent(), is(false)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 最初の要素を取り出す () { | |
Optional<Jojo> jojoFirst = Stream.of(Jojo.values()).findFirst(); | |
assertThat(jojoFirst.get(), is(Jojo.ジョセフジョースター)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 何か要素を取り出す () { | |
System.out.println(testName.getMethodName()); | |
Optional<Jojo> someJojo = Stream.of(Jojo.values()) | |
.parallel() | |
.findAny(); | |
System.out.println(someJojo); | |
assertThat(someJojo.isPresent(), is(true)); | |
System.out.println(testName.getMethodName()); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void すべて条件を満たす () { | |
boolean allMatch = IntStream.range(1, 10) | |
.limit(4) | |
.allMatch(number -> number < 5); | |
assertThat(allMatch, is(true)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 何か一つ条件を満たす () { | |
boolean anyMatch = IntStream.range(1, 10) | |
.anyMatch(number -> number == 3); | |
assertThat(anyMatch, is(true)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void どれも条件を満たさない () { | |
boolean noneMatch = IntStream.range(1, 10) | |
.skip(5L) | |
.noneMatch(number -> number < 5); | |
assertThat(noneMatch, is(true)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 配列にする () { | |
Jojo[] result = Stream.of(Jojo.values()) | |
.filter(jojo -> jojo.birthOfYear() < 1988) | |
.toArray(Jojo[]::new); | |
List<Jojo> jojos = Arrays.asList(result); | |
assertThat(jojos, hasItems(Jojo.ジョセフジョースター, Jojo.空条承太郎)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void 変換する () { | |
int expected = 1 + 2 + 3 + 5 + 8; | |
Integer total = Stream.iterate(new Fibonacci(), Fibonacci::next) | |
.limit(5L) | |
.reduce(0, (sum, fib) -> sum + fib.value(), (left, right) -> left + right); | |
assertThat(total, is(expected)); | |
} | |
@Category(NormalTests.class) | |
@Test | |
public void mutableに変換する () { | |
List<Integer> result = Stream.iterate(new Fibonacci(), Fibonacci::next) | |
.limit(7L) | |
.parallel() | |
.collect(CopyOnWriteArrayList::new, | |
(list, fib) -> list.add(fib.value()), | |
List::addAll); | |
assertThat(result, hasItems(1, 2, 21)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment