Skip to content

Commit f035b45

Browse files
committed
New file
1 parent 950a8b9 commit f035b45

11 files changed

Lines changed: 374 additions & 0 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//: innerclasses/ArgReturnReferences.java
2+
// Demonstrates method references
3+
import java.util.function.*;
4+
import static net.mindview.util.Print.*;
5+
6+
class Y {
7+
static Y create() { return new Y(); }
8+
static void absorb(Y y) {}
9+
static String transform1(Y y) { return "Y"; }
10+
static String transform2(Y y, String s) {
11+
return "Y" + " " + s;
12+
}
13+
}
14+
15+
public class ArgReturnReferences {
16+
Supplier<Y> supply = Y::create;
17+
Consumer<Y> consume = Y::absorb;
18+
Function<Y, String> f1arg = Y::transform1;
19+
BiFunction<Y, String, String> f2arg =
20+
Y::transform2;
21+
public static void main(String[] args) {
22+
ArgReturnReferences arr =
23+
new ArgReturnReferences();
24+
Y y = arr.supply.get();
25+
arr.consume.accept(y);
26+
print(arr.f1arg.apply(y));
27+
print(arr.f2arg.apply(y, "Howdy"));
28+
}
29+
} ///:~

innerclasses/CtorReference.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//: innerclasses/CtorReference.java
2+
// Demonstrates java.util.function
3+
import java.util.function.*;
4+
import static net.mindview.util.Print.*;
5+
6+
public class CtorReference {
7+
public CtorReference() {
8+
print("Inside CtorReference()");
9+
}
10+
public CtorReference(int i) {
11+
print("Inside CtorReference(i)");
12+
}
13+
public CtorReference(int i, double d) {
14+
print("Inside CtorReference(i, d)");
15+
}
16+
public static void main(String[] args) {
17+
Supplier<CtorReference> cr0 =
18+
CtorReference::new;
19+
CtorReference r0 = cr0.get();
20+
21+
Function<Integer, CtorReference> cr1 =
22+
CtorReference::new;
23+
CtorReference r1 = cr1.apply(1);
24+
25+
BiFunction<Integer, Double, CtorReference> cr2 =
26+
CtorReference::new;
27+
CtorReference r2 = cr2.apply(1, 2.0);
28+
}
29+
} ///:~

innerclasses/MethodReferences.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//: innerclasses/MethodReferences.java
2+
import java.util.*;
3+
4+
public class MethodReferences {
5+
static class Description {
6+
String describe;
7+
public Description(String desc) {
8+
describe = desc;
9+
}
10+
public void show() { System.out.println(describe); }
11+
}
12+
public static void main(String[] args) {
13+
List<String> keys = Arrays.asList(
14+
"Every", "Good", "Boy", "Deserves", "Fudge");
15+
keys.forEach(System.out::println); // (1)
16+
17+
List<Description> descriptions = new ArrayList<>();
18+
for(String k : keys)
19+
descriptions.add(new Description(k));
20+
descriptions.forEach(Description::show); // (2)
21+
}
22+
} ///:~
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//: innerclasses/RunnableMethodReference.java
2+
// Demonstrates method references
3+
import static net.mindview.util.Print.*;
4+
5+
class External {
6+
static void go() { print("External.go()"); }
7+
}
8+
9+
public class RunnableMethodReference {
10+
Runnable robject, rstatic, rexstatic;
11+
void f() { print("f()"); }
12+
static void g() { print("g()"); }
13+
public void assign() {
14+
robject = this::f;
15+
rstatic = RunnableMethodReference::g;
16+
rexstatic = External::go;
17+
}
18+
public void call() {
19+
robject.run();
20+
rstatic.run();
21+
rexstatic.run();
22+
}
23+
public static void main(String[] args) {
24+
RunnableMethodReference rmr =
25+
new RunnableMethodReference();
26+
rmr.assign();
27+
rmr.call();
28+
rmr.robject = rmr::f;
29+
rmr.robject.run();
30+
}
31+
} ///:~
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//: innerclasses/UnboundMethodReference.java
2+
// Method reference without an object.
3+
import java.util.*;
4+
import java.util.function.*;
5+
import static net.mindview.util.Print.*;
6+
7+
class X {
8+
String f() { return "X.f()"; }
9+
}
10+
11+
public class UnboundMethodReference {
12+
public static void main(String[] args) {
13+
Function<String, Integer> len = String::length;
14+
print(len.apply("UnboundMethodReference"));
15+
16+
List<String> words =
17+
Arrays.asList("Rain", "Spain", "Plain");
18+
words.forEach(System.out::println);
19+
20+
Function<X, String> xfr = X::f;
21+
print(xfr.apply(new X()));
22+
}
23+
} ///:~

net/mindview/util/PrintArray.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//: net/mindview/util/PrintArray.java
2+
// Display an array of double
3+
package net.mindview.util;
4+
5+
public class PrintArray {
6+
public static void printArray(double[] array) {
7+
for(int i = 0; i < array.length; i++) {
8+
System.out.print(array[i]);
9+
if(i != array.length -1)
10+
System.out.print(", ");
11+
}
12+
System.out.println();
13+
}
14+
} ///:~

patterns/CommandPattern2.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//: patterns/CommandPattern2.java
2+
// Using the Runnable functional interface
3+
import java.util.*;
4+
5+
class Command2 {
6+
public Runnable execute;
7+
}
8+
9+
class Hello2 extends Command2 {
10+
Hello2() {
11+
execute = () -> System.out.print("Hello ");
12+
}
13+
}
14+
15+
class World2 extends Command2 {
16+
World2() {
17+
execute = () -> System.out.print("World! ");
18+
}
19+
}
20+
21+
class IAm2 extends Command2 {
22+
IAm2() {
23+
execute = () ->
24+
System.out.print("I'm the command pattern!");
25+
}
26+
}
27+
28+
class Macro2 extends Command2 {
29+
private List<Command2> commands =
30+
new ArrayList<>();
31+
public void add(Command2 c) { commands.add(c); }
32+
Macro2() {
33+
execute = () -> {
34+
for(Command2 c: commands) c.execute.run();
35+
};
36+
}
37+
}
38+
39+
public class CommandPattern2 {
40+
public static void main(String args[]) {
41+
Macro2 macro = new Macro2();
42+
macro.add(new Hello2());
43+
macro.add(new World2());
44+
macro.add(new IAm2());
45+
macro.execute.run();
46+
}
47+
} ///:~

patterns/CommandPattern3.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//: patterns/CommandPattern3.java
2+
// Just implement the Runnable interface!
3+
import java.util.*;
4+
5+
class Hello3 implements Runnable {
6+
public void run() { System.out.print("Hello "); }
7+
}
8+
9+
class World3 implements Runnable {
10+
public void run() { System.out.print("World! "); }
11+
}
12+
13+
class IAm3 implements Runnable {
14+
public void run() {
15+
System.out.print("I'm the command pattern!");
16+
}
17+
}
18+
19+
class Macro3 implements Runnable {
20+
public List<Runnable> commands = new ArrayList<>();
21+
public void run() {
22+
for(Runnable c: commands) c.run();
23+
}
24+
}
25+
26+
public class CommandPattern3 {
27+
public static void main(String args[]) {
28+
Macro3 macro = new Macro3();
29+
macro.commands.add(new Hello3());
30+
macro.commands.add(new World3());
31+
macro.commands.add(new IAm3());
32+
macro.run();
33+
}
34+
} ///:~
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//: patterns/absfactory/GameEnvironment2.java
2+
// Using the Supplier<> Functional Interface.
3+
package patterns.absfactory;
4+
import java.util.function.*;
5+
6+
class GameElementFactory2 {
7+
Supplier<Player> player;
8+
Supplier<Obstacle> obstacle;
9+
}
10+
11+
// Concrete factories:
12+
class KittiesAndPuzzles2
13+
extends GameElementFactory2 {
14+
KittiesAndPuzzles2() {
15+
player = Kitty::new;
16+
obstacle = Puzzle::new;
17+
}
18+
}
19+
20+
class KillAndDismember2
21+
extends GameElementFactory2 {
22+
KillAndDismember2() {
23+
player = KungFuGuy::new;
24+
obstacle = NastyWeapon::new;
25+
}
26+
}
27+
28+
public class GameEnvironment2 {
29+
private Player p;
30+
private Obstacle ob;
31+
public GameEnvironment2(
32+
GameElementFactory2 factory) {
33+
p = factory.player.get();
34+
ob = factory.obstacle.get();
35+
}
36+
public void play() {
37+
p.interactWith(ob);
38+
}
39+
public static void main(String args[]) {
40+
GameElementFactory2
41+
kp = new KittiesAndPuzzles2(),
42+
kd = new KillAndDismember2();
43+
GameEnvironment2
44+
g1 = new GameEnvironment2(kp),
45+
g2 = new GameEnvironment2(kd);
46+
g1.play();
47+
g2.play();
48+
}
49+
} ///:~
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//: patterns/chain/ChainOfResponsibility2.java
2+
// Using the Functional interface.
3+
package patterns.chain;
4+
import java.util.*;
5+
import java.util.function.*;
6+
import static net.mindview.util.PrintArray.*;
7+
8+
class FindMinima2 {
9+
public static Result leastSquares(double[] line) {
10+
System.out.println("LeastSquares.algorithm");
11+
boolean weSucceed = false;
12+
if(weSucceed) // Actual test/calculation here
13+
return new Result(new double[] { 1.1, 2.2 });
14+
else // Try the next one in the chain:
15+
return new Fail();
16+
}
17+
public static Result perturbation(double[] line) {
18+
System.out.println("Perturbation.algorithm");
19+
boolean weSucceed = false;
20+
if(weSucceed) // Actual test/calculation here
21+
return new Result(new double[] { 3.3, 4.4 });
22+
else
23+
return new Fail();
24+
}
25+
public static Result bisection(double[] line) {
26+
System.out.println("Bisection.algorithm");
27+
boolean weSucceed = true;
28+
if(weSucceed) // Actual test/calculation here
29+
return new Result(new double[] { 5.5, 6.6 });
30+
else
31+
return new Fail();
32+
}
33+
static List<Function<double[], Result>> algorithms =
34+
Arrays.asList(
35+
FindMinima2::leastSquares,
36+
FindMinima2::perturbation,
37+
FindMinima2::bisection
38+
);
39+
public static Result minima(double[] line) {
40+
for (Function<double[], Result> alg : algorithms) {
41+
Result result = alg.apply(line);
42+
if(result.success)
43+
return result;
44+
}
45+
return new Fail();
46+
}
47+
}
48+
49+
public class ChainOfResponsibility2 {
50+
public static void main(String args[]) {
51+
FindMinima solver = new FindMinima();
52+
double[] line = {
53+
1.0, 2.0, 1.0, 2.0, -1.0,
54+
3.0, 4.0, 5.0, 4.0 };
55+
Result result = solver.minima(line);
56+
if(result.success)
57+
printArray(result.line);
58+
else
59+
System.out.println("No algorithm found");
60+
}
61+
} ///:~

0 commit comments

Comments
 (0)