-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodReferences.java
More file actions
171 lines (131 loc) · 3.55 KB
/
MethodReferences.java
File metadata and controls
171 lines (131 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
> a constructor reference for an array can be created, as shown below:
type[]::new // type refers to the object being created
> any functional interface that can be used to create an array must contain a method that takes a single int parameter and returns a reference to the array of the specified size
*/
import java.util.function.*;
/*
>> INTERFACES
*/
interface NoParam {
MyIntNum func();
}
interface MultiParam {
MyIntNum func(int v);
}
interface SomeTest<T> {
boolean test(T n, T m);
}
interface Defaults {
default int doMath(int a) {
return a * 2;
}
}
interface IIntPredicate {
// ClassName::staticMethodName
// objRef::methodName
boolean test(int n);
}
interface IMyIntNumPredicate {
// ClassName::instanceMethodName
// first parameter has to match the invoking object
// any other parameter matches the parameter specified by the instance method
boolean test(MyIntNum mv, int n);
}
// Generic Interface that creates an array
interface CreateArray<T> {
T[] func(int n);
}
/*
>> CLASSES
*/
// instance methods
class MyIntNum {
private int v;
MyIntNum (int v) { this.v = v; }
MyIntNum () { v = 1; }
int getNum() { return v; }
boolean isFactor(int n) {
return (v % n) == 0;
}
}
// static methods
class MyIntPredicates {
static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n/i; i++) {
if (n % i == 0) return false;
}
return true;
}
static boolean isEven(int n) {
return (n % 2) == 0;
}
static boolean isPositive(int n) {
return n > 0;
}
static <T> boolean myGenMeth(T x, T y) {
boolean result = false;
// ...
return result;
}
}
/*
>> TESTS
*/
class MethodReferences implements Defaults {
static boolean numTest(IIntPredicate p, int v) {
return p.test(v);
}
public int doMath(int a) {
return a * a;
}
public void test(int value) {
Function<Integer,Integer> oper1 = this::doMath;
out.println("this::doMath() = " + oper1.apply(value));
Function<Integer,Integer> oper2 = Defaults.super::doMath;
out.println("Defaults.super::doMath() = " + oper2.apply(value));
}
public static void main(String[] sth) {
// staticMethods();
// instanceMethods();
// classInstanceMethods();
// (new MethodReferences()).test(10);
// createArray();
constructorMethods();
}
// implement CreateArray<T>
static void createArray() {
CreateArray<Thread> ca = Thread[]::new;
Thread[] thrds = ca.func(5);
}
static void constructorMethods() {
NoParam s1 = MyIntNum::new;
MultiParam s2 = MyIntNum::new;
MyIntNum sth = s1.func();
out.println("For NoParam: " + sth.getNum());
sth = s2.func(3);
out.println("For MultiParam: " + sth.getNum());
}
static void classInstanceMethods() {
var p = new MyIntNum(12);
var x = new MyIntNum(16);
IMyIntNumPredicate inp = MyIntNum::isFactor;
if (inp.test(p, 3)) out.println("3 is a factor of " + p.getNum());
if (!inp.test(x, 3)) out.println("3 is not a factor of " + x.getNum());
}
static void instanceMethods() {
var p = new MyIntNum(12);
var y = new MyIntNum(16);
IIntPredicate pI = p::isFactor;
IIntPredicate yI = y::isFactor;
if (pI.test(3)) System.out.println("3 is a factor of " + p.getNum());
if (!yI.test(5)) System.out.println("5 is not a factor of " + y.getNum());
}
static void staticMethods() {
SomeTest<Integer> sth = MyIntPredicates::<Integer>myGenMeth;
if (numTest(MyIntPredicates::isPrime, 17)) out.println("17 is a prime");
if (numTest(MyIntPredicates::isEven, 18)) out.println("18 is an even number");
if (numTest(MyIntPredicates::isPositive, 20)) out.println("20 is a positive number");
}
}