diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8503686
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,18 @@
+# Generated by http://gitignore.io
+
+### IntelliJ ###
+*.iml
+*.ipr
+*.iws
+.idea/
+
+### Java ###
+*.class
+
+# Package Files #
+*.jar
+*.war
+*.ear
+
+### Maven ###
+target/
diff --git a/README b/README
deleted file mode 100644
index 3a46807..0000000
--- a/README
+++ /dev/null
@@ -1,28 +0,0 @@
-JDK8 Lambda Samples
---------------------------------------------------------------------------
-
-**** DISCLAIMER: ****
-**** At the present moment, Project Lambda is in heavy development. ****
-**** These tests may fail to compile, fail to run, or fail the ****
-**** assertions due to syntactic and semantic changes in JDK. ****
-
-One more time time:
- THESE TESTS REFLECT THE CURRENT STATE OF LAMBDA, AND NOT THE FINAL JDK8
-
-These tests were used on local JUGs to highlight different things about
-upcoming Project Lambda (support for lambda expressions in Java 8). The
-tests are by no means extensive or complete, and really are the collateral.
-
-The general discussion about the implementation of Project Lambda:
- http://mail.openjdk.java.net/mailman/listinfo/lambda-dev
-
-JSR335 EG observers lists:
- http://mail.openjdk.java.net/mailman/listinfo/lambda-libs-spec-observers
- http://mail.openjdk.java.net/mailman/listinfo/lambda-spec-observers
-
-Binary builds for JDK8 with Lambda support:
- http://jdk8.java.net/lambda
-
-General information about the project:
- http://openjdk.java.net/projects/lambda/
-
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b7ed57d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,51 @@
+## JDK8 Lambda Samples
+originally created by [@shipilev](https://github.com/shipilev), [repo](https://github.com/shipilev/jdk8-lambda-samples)
+
+#### short notes:
+
+| class | test | description |
+|---|---|---|
+| LambdaTest | testCmpLegacy/testCmpLambda0 | Comparators: lambdas and anonymous classes
+||testCmpLambda1| (int x)->
+||testOneArg| ()->()
+||testOneArg1 & 2| (x)-> aka x->
+||testCmpLambda2 & 3| Integer bigger than 127
+||testConsumer | Consumer in .forEach()
+|UroborosTest| \* | chain call
+|CaptureTest|testLegacy | final fields in anonymous class
+||testLambda | *effective final* with lambda
+||testPredicate1 & 2| Predicates with context
+|ScopingTest|testScoping1| different **this** for anonymous classes and lambdas
+|FibonacciTest| test10 | capture **this** but not field
+|| test10_static| without capture, just link to static method
+|MethodRefTest| testMethodRefStatic| methodRef to static method (as prev)
+||testMethodRefInstance0 & 1 & 2 | *bound* MethodRef
+||testMethodRefInstanceUnbound | *unbound* MethodRef (add **this**)
+||testConstructor | call **new**
+||testConstructor0 | constructor w/o parameters
+||testConstructor1 | constructor w/ parameters
+|ZamTest| \* | Replace lambdas to MethodRef
+|ThreadLocalTest|threadLocalLegacy| legacy variant
+||threadLocalLambda| added method withInitial(Supplier)
+||threadLocalMRef | via MethodRef
+| ▼ Advanced ▼
+|NoiseSampleTest|
+|WeirdFunctionTest|
+
+###Default Methods
+
+| class | test | description |
+|---|---|---|
+|DefaultTest| \* | backward compatibility
+|GuavaTest| testTreeMultiset | usage with third party libraries
+|StaticInterfaceTest| test | functional interface with static method
+
+###Streams (Bulk Operations for Collections)
+
+| class | test | description |
+|---|---|---|
+|ExternalInternalTest| \* |
+||►|highlight the difference between internal and external iteration
+||►|iteration is explicitly serial
+||►|libraries have more headroom optimizing with internal iteration
+||►|sometimes even forEach is not enough: chaining the action within the serial again
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index bdfb5b8..5fd761f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,7 +31,7 @@
junit
junit
- 4.8.1
+ 4.11
test
diff --git a/src/test/java/net/openjdk/defaults/DefaultTest.java b/src/test/java/net/openjdk/defaults/DefaultTest.java
index bc685a7..2ce4552 100644
--- a/src/test/java/net/openjdk/defaults/DefaultTest.java
+++ b/src/test/java/net/openjdk/defaults/DefaultTest.java
@@ -1,25 +1,17 @@
package net.openjdk.defaults;
-import junit.framework.Assert;
import org.junit.Test;
-public class DefaultTest {
-
- @Test
- public void testNew() {
- Assert.assertEquals("Legacy Method from the New Class", new NewClass().legacyMethod());
- Assert.assertEquals("New Method from the New Class", new NewClass().newMethod());
- }
+import static org.junit.Assert.assertEquals;
- @Test
- public void testLegacy() {
- Assert.assertEquals("Legacy Method from the Legacy Class", new LegacyClass().legacyMethod());
- Assert.assertEquals("The beauty is in the eye of the defender", new LegacyClass().newMethod());
- }
+public class DefaultTest {
public interface LegacyInterface {
String legacyMethod();
- default String newMethod() { return "The beauty is in the eye of the defender"; }
+
+ default String newMethod() {
+ return "The beauty is in the eye of the defender";
+ }
}
public class LegacyClass implements LegacyInterface {
@@ -41,4 +33,16 @@ public String newMethod() {
}
}
+ @Test
+ public void testLegacy() {
+ assertEquals("Legacy Method from the Legacy Class", new LegacyClass().legacyMethod());
+ assertEquals("The beauty is in the eye of the defender", new LegacyClass().newMethod());
+ }
+
+ @Test
+ public void testNew() {
+ assertEquals("Legacy Method from the New Class", new NewClass().legacyMethod());
+ assertEquals("New Method from the New Class", new NewClass().newMethod());
+ }
+
}
diff --git a/src/test/java/net/openjdk/defaults/GuavaTest.java b/src/test/java/net/openjdk/defaults/GuavaTest.java
index 9be56ef..ec612b9 100644
--- a/src/test/java/net/openjdk/defaults/GuavaTest.java
+++ b/src/test/java/net/openjdk/defaults/GuavaTest.java
@@ -1,9 +1,10 @@
package net.openjdk.defaults;
import com.google.common.collect.TreeMultiset;
-import junit.framework.Assert;
import org.junit.Test;
+import static org.junit.Assert.*;
+
public class GuavaTest {
@Test
@@ -14,7 +15,7 @@ public void testTreeMultiset() {
strings.add("Baz");
String result = strings.stream().reduce((s1, s2) -> s1 + ", " + s2).get();
- Assert.assertEquals("Bar, Baz, Foo", result);
+ assertEquals("Bar, Baz, Foo", result);
}
}
diff --git a/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java b/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java
index 972714b..9adeafd 100644
--- a/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java
+++ b/src/test/java/net/openjdk/defaults/StaticInterfaceTest.java
@@ -1,15 +1,14 @@
package net.openjdk.defaults;
-import org.junit.Assert;
-import org.junit.Ignore;
import org.junit.Test;
+import static org.junit.Assert.*;
+
public class StaticInterfaceTest {
@Test
public void test() {
- // FIXME: does not run with current public builds
- Assert.assertEquals("toDublin", TicketOffice.defaultOffice().qDublin());
+ assertEquals("toDublin", TicketOffice.defaultOffice().qDublin());
}
public interface TicketOffice {
diff --git a/src/test/java/net/openjdk/lambda/CaptureTest.java b/src/test/java/net/openjdk/lambda/CaptureTest.java
index a0a866a..9ef934b 100644
--- a/src/test/java/net/openjdk/lambda/CaptureTest.java
+++ b/src/test/java/net/openjdk/lambda/CaptureTest.java
@@ -1,18 +1,20 @@
package net.openjdk.lambda;
-import junit.framework.Assert;
import org.junit.Test;
import java.util.Comparator;
+import java.util.function.Function;
import java.util.function.Predicate;
+import static org.junit.Assert.*;
+
public class CaptureTest {
@Test
public void testLegacy() {
final int minus_one = -1;
final int zero = 0;
- final int one = 1;
+ final int one = 1;
Comparator cmp = new Comparator() {
@Override
@@ -22,36 +24,43 @@ public int compare(Integer x, Integer y) {
};
- Assert.assertEquals(0, cmp.compare(0, 0));
- Assert.assertEquals(-1, cmp.compare(-100, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(0, 0));
+ assertEquals(-1, cmp.compare(-100, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
public void testLambda() {
int minus_one = -1;
int zero = 0;
- int one = 1;
+ int one = 1;
Comparator cmp = (x, y) -> (x < y) ? minus_one : ((x > y) ? one : zero);
- Assert.assertEquals(0, cmp.compare(0, 0));
- Assert.assertEquals(-1, cmp.compare(-100, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(0, 0));
+ assertEquals(-1, cmp.compare(-100, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
-
private Predicate makeCaseUnsensitiveMatcher(String pattern) {
return s -> pattern.equalsIgnoreCase(s);
}
@Test
- public void testLambda1() {
+ public void testPredicate1() {
// Predicate ~ boolean test(T t);
- Assert.assertTrue( makeCaseUnsensitiveMatcher("true").test("TruE") );
- Assert.assertTrue( makeCaseUnsensitiveMatcher("false").test("FalsE") );
- Assert.assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE") );
+ assertTrue(makeCaseUnsensitiveMatcher("true").test("TruE"));
+ assertTrue(makeCaseUnsensitiveMatcher("false").test("FalsE"));
+ assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE"));
+ }
+
+ @Test
+ public void testPredicate2() {
+ Function> matcher = s1 -> s2 -> s1.equalsIgnoreCase(s2);
+ assertTrue(matcher.apply("true").test("TruE"));
+ assertTrue(matcher.apply("false").test("FalsE"));
+ assertFalse(matcher.apply("true").test("FalsE"));
}
}
diff --git a/src/test/java/net/openjdk/lambda/FibonacciTest.java b/src/test/java/net/openjdk/lambda/FibonacciTest.java
index 577f838..8137b77 100644
--- a/src/test/java/net/openjdk/lambda/FibonacciTest.java
+++ b/src/test/java/net/openjdk/lambda/FibonacciTest.java
@@ -1,27 +1,28 @@
package net.openjdk.lambda;
-import junit.framework.Assert;
import org.junit.Test;
import java.util.function.IntUnaryOperator;
+import static org.junit.Assert.*;
+
public class FibonacciTest {
// Фибоначчийн тоог хүн төрөлхтөн өөрсдийн ахуй (source: Wikipedia MN)
- private IntUnaryOperator fib =
- (n) -> (n < 2) ? n : fib.applyAsInt(n - 1) + fib.applyAsInt(n - 2);
+ private IntUnaryOperator fib;
+ {fib = (n) -> (n < 2) ? n : fib.applyAsInt(n - 1) + fib.applyAsInt(n - 2);}
@Test
public void test10() {
- Assert.assertEquals(55, fib.applyAsInt(10));
+ assertEquals(55, fib.applyAsInt(10));
}
- private static IntUnaryOperator fib_static =
- (n) -> (n < 2) ? n : fib_static.applyAsInt(n - 1) + fib_static.applyAsInt(n - 2);
+ private static IntUnaryOperator fib_static;
+ static {fib_static = (n) -> (n < 2) ? n : fib_static.applyAsInt(n - 1) + fib_static.applyAsInt(n - 2);}
@Test
public void test10_static() {
- Assert.assertEquals(55, fib_static.applyAsInt(10));
+ assertEquals(55, fib_static.applyAsInt(10));
}
// @Test
@@ -29,7 +30,7 @@ public void test10_static() {
// IntUnaryOperator fib =
// (n) -> (n < 2) ? n : fib.operate(n - 1) + fib.operate(n - 2);
//
-// Assert.assertEquals(55, fib.operateAsInt(10));
+// assertEquals(55, fib.operateAsInt(10));
// }
}
diff --git a/src/test/java/net/openjdk/lambda/LambdaTest.java b/src/test/java/net/openjdk/lambda/LambdaTest.java
index 76626f3..04872dd 100644
--- a/src/test/java/net/openjdk/lambda/LambdaTest.java
+++ b/src/test/java/net/openjdk/lambda/LambdaTest.java
@@ -1,6 +1,5 @@
package net.openjdk.lambda;
-import junit.framework.Assert;
import org.junit.Test;
import java.util.Arrays;
@@ -9,6 +8,8 @@
import java.util.function.Function;
import java.util.function.Supplier;
+import static org.junit.Assert.*;
+
public class LambdaTest {
@Test
@@ -21,65 +22,65 @@ public int compare(Integer x, Integer y) {
};
- Assert.assertEquals(0, cmp.compare(0, 0));
- Assert.assertEquals(-1, cmp.compare(-100, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(0, 0));
+ assertEquals(-1, cmp.compare(-100, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
public void testCmpLambda0() {
Comparator cmp = (x, y) -> (x < y) ? -1 : ((x > y) ? 1 : 0);
- Assert.assertEquals(0, cmp.compare(0, 0));
- Assert.assertEquals(-1, cmp.compare(-100, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(0, 0));
+ assertEquals(-1, cmp.compare(-100, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
public void testCmpLambda1() {
Comparator cmp = (Integer x, Integer y) -> (x < y) ? -1 : ((x > y) ? 1 : 0);
- Assert.assertEquals(0, cmp.compare(0, 0));
- Assert.assertEquals(-1, cmp.compare(-100, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(0, 0));
+ assertEquals(-1, cmp.compare(-100, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
public void testNoArgs() {
// Supplier ~ T get();
Supplier ultimateAnswerFactory = () -> 42;
- Assert.assertTrue(42 == ultimateAnswerFactory.get());
+ assertTrue(42 == ultimateAnswerFactory.get());
}
@Test
public void testOneArg0() {
// Function ~ R apply(T t);
Function f = (String s) -> Integer.parseInt(s);
- Assert.assertEquals(Integer.valueOf(0), f.apply("0"));
- Assert.assertEquals(Integer.valueOf(1), f.apply("1"));
+ assertEquals(Integer.valueOf(0), f.apply("0"));
+ assertEquals(Integer.valueOf(1), f.apply("1"));
}
@Test
public void testOneArg1() {
Function f = (s) -> Integer.parseInt(s);
- Assert.assertEquals(Integer.valueOf(0), f.apply("0"));
- Assert.assertEquals(Integer.valueOf(1), f.apply("1"));
+ assertEquals(Integer.valueOf(0), f.apply("0"));
+ assertEquals(Integer.valueOf(1), f.apply("1"));
}
@Test
public void testOneArg2() {
Function f = s -> Integer.parseInt(s);
- Assert.assertEquals(Integer.valueOf(0), f.apply("0"));
- Assert.assertEquals(Integer.valueOf(1), f.apply("1"));
+ assertEquals(Integer.valueOf(0), f.apply("0"));
+ assertEquals(Integer.valueOf(1), f.apply("1"));
}
@Test
public void testCmpLambda2() { // wrong
Comparator cmp = (x, y) -> (x < y) ? -1 : ((x == y) ? 0 : 1);
- Assert.assertEquals(0, cmp.compare(100, 100));
- Assert.assertEquals(-1, cmp.compare(0, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(100, 100));
+ assertEquals(-1, cmp.compare(0, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
@@ -90,22 +91,16 @@ public void testCmpLambda3() { // right
return (x < y) ? -1 : ((x == y) ? 0 : 1);
};
- Assert.assertEquals(0, cmp.compare(1000, 1000));
- Assert.assertEquals(-1, cmp.compare(0, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(1000, 1000));
+ assertEquals(-1, cmp.compare(0, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
- public void testBlock0() {
+ public void testConsumer() {
// Consumer ~ void accept(T t);
Consumer b = s -> { System.out.println(s);};
Arrays.asList("Foo", "Bar", "Baz", "Baz", "Foo", "Bar").forEach(b);
}
- @Test
- public void testBlock1() {
- Consumer b = s -> System.out.println(s);
- Arrays.asList("Foo", "Bar", "Baz", "Baz", "Foo", "Bar").forEach(b);
- }
-
}
diff --git a/src/test/java/net/openjdk/lambda/MethodRefTest.java b/src/test/java/net/openjdk/lambda/MethodRefTest.java
index 76ca0a1..3eb2845 100644
--- a/src/test/java/net/openjdk/lambda/MethodRefTest.java
+++ b/src/test/java/net/openjdk/lambda/MethodRefTest.java
@@ -1,6 +1,5 @@
package net.openjdk.lambda;
-import junit.framework.Assert;
import org.junit.Test;
import java.util.Arrays;
@@ -10,15 +9,17 @@
import java.util.function.Function;
import java.util.function.Predicate;
+import static org.junit.Assert.*;
+
public class MethodRefTest {
@Test
public void testMethodRefStatic() {
Comparator cmp = Integer::compare;
- Assert.assertEquals(0, cmp.compare(0, 0));
- Assert.assertEquals(-1, cmp.compare(-100, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
+ assertEquals(0, cmp.compare(0, 0));
+ assertEquals(-1, cmp.compare(-100, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
@@ -35,17 +36,17 @@ private Predicate makeCaseUnsensitiveMatcher(String pattern) {
@Test
public void testMethodRefInstance1() {
// Predicate ~ boolean test(T t);
- Assert.assertTrue( makeCaseUnsensitiveMatcher("true").test("TruE") );
- Assert.assertTrue( makeCaseUnsensitiveMatcher("false").test("FalsE") );
- Assert.assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE") );
+ assertTrue( makeCaseUnsensitiveMatcher("true").test("TruE") );
+ assertTrue( makeCaseUnsensitiveMatcher("false").test("FalsE") );
+ assertFalse(makeCaseUnsensitiveMatcher("true").test("FalsE") );
}
@Test
public void testMethodRefInstance2() {
// Predicate ~ boolean test(T t);
Predicate isTrue = "true"::equalsIgnoreCase;
- Assert.assertTrue(isTrue.test("TruE"));
- Assert.assertFalse(isTrue.test("FalsE"));
+ assertTrue(isTrue.test("TruE"));
+ assertFalse(isTrue.test("FalsE"));
}
@Test
@@ -54,24 +55,16 @@ public void testMethodRefInstanceUnbound() {
// Integer ~ int compareTo(Integer anotherInteger)
Comparator cmp = Integer::compareTo;
- Assert.assertEquals(0, cmp.compare(0, 0));
- Assert.assertEquals(-1, cmp.compare(-100, 100));
- Assert.assertEquals(1, cmp.compare(100, -100));
- }
-
-
- @Test
- public void testParseInt(){
- Function f = Integer::parseInt;
- Assert.assertEquals(Integer.valueOf(0), f.apply("0"));
- Assert.assertEquals(Integer.valueOf(1), f.apply("1"));
+ assertEquals(0, cmp.compare(0, 0));
+ assertEquals(-1, cmp.compare(-100, 100));
+ assertEquals(1, cmp.compare(100, -100));
}
@Test
public void testConstructor(){
Function f = Integer::new;
- Assert.assertEquals(Integer.valueOf(0), f.apply("0"));
- Assert.assertEquals(Integer.valueOf(1), f.apply("1"));
+ assertEquals(Integer.valueOf(0), f.apply("0"));
+ assertEquals(Integer.valueOf(1), f.apply("1"));
}
public static class Counter {
@@ -86,11 +79,11 @@ public Counter(int count) {
private int count = 0;
- public int inc() {
+ public int incCount() {
return ++count;
}
- public int get() {
+ public int getCount() {
return count;
}
}
@@ -98,15 +91,15 @@ public int get() {
@Test
public void testConstructor0(){
Supplier f = Counter::new;
- Assert.assertEquals(0, f.get().get());
- Assert.assertEquals(1, f.get().inc());
+ assertEquals(0, f.get().getCount());
+ assertEquals(1, f.get().incCount());
}
@Test
public void testConstructor1(){
Function f = Counter::new;
- Assert.assertEquals(1, f.apply(1).get());
- Assert.assertEquals(42, f.apply(42).get());
+ assertEquals(1, f.apply(1).getCount());
+ assertEquals(42, f.apply(42).getCount());
}
}
diff --git a/src/test/java/net/openjdk/lambda/NoiseSampleTest.java b/src/test/java/net/openjdk/lambda/NoiseSampleTest.java
index de19a3c..1ad83c0 100644
--- a/src/test/java/net/openjdk/lambda/NoiseSampleTest.java
+++ b/src/test/java/net/openjdk/lambda/NoiseSampleTest.java
@@ -1,12 +1,13 @@
package net.openjdk.lambda;
-import junit.framework.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
+import static org.junit.Assert.*;
+
public class NoiseSampleTest {
/**
@@ -85,8 +86,8 @@ public Counter get() {
}
);
- Assert.assertEquals(1, map.get("foo").get("bar").inc());
- Assert.assertEquals(2, map.get("foo").get("bar").inc());
+ assertEquals(1, map.get("foo").get("bar").inc());
+ assertEquals(2, map.get("foo").get("bar").inc());
}
@Test
@@ -94,8 +95,8 @@ public void testL0() {
Map> map =
new ComputeMap>(() -> new ComputeMap<>(() -> new Counter()));
- Assert.assertEquals(1, map.get("foo").get("bar").inc());
- Assert.assertEquals(2, map.get("foo").get("bar").inc());
+ assertEquals(1, map.get("foo").get("bar").inc());
+ assertEquals(2, map.get("foo").get("bar").inc());
}
@Test
@@ -103,8 +104,8 @@ public void testL1() {
Supplier