Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by http://gitignore.io

### IntelliJ ###
*.iml
*.ipr
*.iws
.idea/

### Java ###
*.class

# Package Files #
*.jar
*.war
*.ear

### Maven ###
target/
28 changes: 0 additions & 28 deletions README

This file was deleted.

51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
32 changes: 18 additions & 14 deletions src/test/java/net/openjdk/defaults/DefaultTest.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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());
}

}
5 changes: 3 additions & 2 deletions src/test/java/net/openjdk/defaults/GuavaTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}

}
7 changes: 3 additions & 4 deletions src/test/java/net/openjdk/defaults/StaticInterfaceTest.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
37 changes: 23 additions & 14 deletions src/test/java/net/openjdk/lambda/CaptureTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> cmp = new Comparator<Integer>() {
@Override
Expand All @@ -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<Integer> 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<String> makeCaseUnsensitiveMatcher(String pattern) {
return s -> pattern.equalsIgnoreCase(s);
}

@Test
public void testLambda1() {
public void testPredicate1() {
// Predicate<T> ~ 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<String, Predicate<String>> matcher = s1 -> s2 -> s1.equalsIgnoreCase(s2);

assertTrue(matcher.apply("true").test("TruE"));
assertTrue(matcher.apply("false").test("FalsE"));
assertFalse(matcher.apply("true").test("FalsE"));
}

}
17 changes: 9 additions & 8 deletions src/test/java/net/openjdk/lambda/FibonacciTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
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
// public void test10_bad() { // should not compile
// 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));
// }

}
Loading