Skip to content

Commit 197eddb

Browse files
authored
Merge pull request #63 from BobHanson/master
more efficient Java8 doublecolon using Clazz.newLambda(...)
2 parents 52ac79c + a28b00a commit 197eddb

File tree

14 files changed

+345
-127
lines changed

14 files changed

+345
-127
lines changed
-413 Bytes
Binary file not shown.
508 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20180804142820
1+
20180805231348
-413 Bytes
Binary file not shown.
508 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20180804142820
1+
20180805231348

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/Java2ScriptVisitor.java

Lines changed: 129 additions & 75 deletions
Large diffs are not rendered by default.
-413 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/test/Test_.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ public void testInner() {
3333
private int test3() {
3434
// Some random tests
3535

36+
{
37+
long j = 1;
38+
int i = 3;
39+
j &= ~(1L << i);
40+
j = 1L << i;
41+
j = 1L << 3;
42+
j &= 1L << i;
43+
j = ~i;
44+
j = ~(1L << i);
45+
}
46+
3647
System.out.println("abcde".indexOf(99));
3748
assert ("test".contentEquals(new StringBuffer("test")));
3849
int i = "test\2".charAt(4);

sources/net.sf.j2s.java.core/src/test/Test_Java8.java

Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,45 @@
1111
import java.util.function.BiPredicate;
1212
import java.util.function.Consumer;
1313
import java.util.function.Function;
14+
import java.util.function.IntConsumer;
1415

1516
import test.baeldung.doublecolon.Computer;
1617
import test.baeldung.doublecolon.MacbookPro;
1718

1819
public class Test_Java8 extends Test_ implements PropertyChangeListener {
1920

21+
public Test_Java8() {
22+
System.out.println("null constructor");
23+
}
24+
25+
public Test_Java8(String s) {
26+
System.out.println("new Test_Java8(" + s + ")");
27+
}
28+
2029
public static void test() {
2130

2231
}
2332

2433
enum E {
2534
E1, E2;
2635
}
27-
int test1 = 1;
36+
int test1 = 1;
2837

2938
Object test2(Object o) {
3039
System.out.println("test1 is " + test1 + " " + o);
3140
return o;
41+
};
42+
43+
static int test1s = 2;
44+
45+
static Integer test2is(Integer i) {
46+
System.out.println("test1s is " + test1s + " " + i);
47+
return i;
48+
};
49+
50+
Integer test2i(Integer i) {
51+
System.out.println("test1 is " + test1 + " " + i);
52+
return i;
3253
};
3354

3455
public static boolean isMoreThanFifty(int n1, int n2) {
@@ -45,9 +66,6 @@ public static List<Integer> findNumbers(List<Integer> l, BiPredicate<Integer, In
4566
return newList;
4667
}
4768

48-
public Test_Java8() {System.out.println("null constructor");}
49-
public Test_Java8(Object o) {System.out.println("o is " + o);}
50-
5169
@FunctionalInterface
5270
public interface ITest {
5371
Test_Java8 c(Object o);
@@ -56,13 +74,32 @@ public interface ITest {
5674
public static void main(String[] args) {
5775

5876
Comparator comp = Comparator.naturalOrder();
77+
78+
new Test_Java8().propertyChange(null); // checking for nonqualified
79+
80+
ITest t = new ITest() {
81+
@Override
82+
public Test_Java8 c(Object o) {
83+
return new Test_Java8(o + "??");
84+
}
85+
};
86+
Test_Java8 test = t.c("new");
87+
88+
System.out.println("what is test? " + test);
89+
5990

6091

6192
System.out.println("E.E1 is " + E.E1);
6293

94+
95+
6396
final String function = "" + new Date();
6497

65-
int[][][] x = new int[5][][];
98+
99+
// Function
100+
101+
int[][][] a;
102+
66103
Function<Integer, int[][][]> ifi = new Function<Integer, int[][][]>() {
67104

68105
@Override
@@ -71,49 +108,72 @@ public int[][][] apply(Integer t) {
71108
return new int[t.intValue()][][];
72109
}
73110
};
74-
int[][][] a = ifi.apply(5);
111+
a = ifi.apply(5);
112+
System.out.println("a length is " + a.length);
75113

76114
a = (new Function<Integer, int[][][]>() {
77115
@Override
78116
public int[][][] apply(Integer t) {
79117
return new int[t.intValue()][][];
80118
}
81-
}).apply(5);
82-
119+
}).apply(6);
120+
System.out.println("a.length is " + a.length);
121+
122+
// // Lambda_C
123+
83124
Function<Integer, int[][][]> iaCreator = int[][][]::new;
84-
a = iaCreator.apply(5);
125+
a = iaCreator.apply(7);
85126
System.out.println("a.length is " + a.length);
86-
Test_Java8[] atest = new Test_Java8[5];
87-
// following format only works for
88-
Function<Object,Test_Java8> iaCreator2= Test_Java8::new;
127+
128+
Function<String,Test_Java8> iaCreator2= Test_Java8::new;
129+
iaCreator2.apply("testNew");
130+
89131

90-
ITest t;
91-
t = Test_Java8::new;
92-
t = new ITest() {
93-
@Override
94-
public Test_Java8 c(Object o) {
95-
return new Test_Java8(o + "??");
96-
}
97-
};
98-
Test_Java8 test = t.c("new");
99-
100-
System.out.println("what is test? " + test);
101132
Consumer<String> c = s -> System.out.println(s);
133+
c.accept("testingPointer");
134+
102135
c = System.out::println;
103-
c.accept("testing");
136+
c.accept("testingDoubleColon");
104137

105-
106-
107-
Function<Consumer<String>, ?> f = new Test_Java8(null)::test2;
108-
// Q: where is the object in this expression? test.test2.apply(test, [args])
109-
f.apply(c);
138+
Function<Integer, Integer> f1 = new Test_Java8("for f1")::test2i;
139+
f1.apply(new Integer(3));
140+
141+
Function<Integer, Integer> f2 = Test_Java8::test2is;
142+
f2.apply(new Integer(3));
143+
144+
Function<Consumer<String>, ?> f = new Test_Java8("for f")::test2;
145+
f.apply(c);
110146

111147
Test_Java8 t8 = new Test_Java8("new8");
112148
f = t8::test2;
113149
f.apply(c);
150+
f.apply(System.out::println);
151+
152+
// lambda_M
114153

115-
t8.propertyChange(null); // checking for nonqualified
116-
154+
Computer c1 = new Computer(2015, "white", 100);
155+
Computer c2 = new MacbookPro(2009, "black", 100);
156+
List<Computer> inventory = Arrays.asList(c1, c2);
157+
158+
inventory.forEach(System.out::println); // works because System.out.println(Object) exists
159+
160+
// note that YOU CANNOT HAVE BOTH static Computer.turnOnPc(Object) and computer.turnOnPc()
161+
// so it should be sufficient to call one or the other:
162+
// Computer.turnOnPc$Computer.apply(x)
163+
// or
164+
// Computer.prototype.turnOnPc$.apply()
165+
166+
inventory.forEach(Computer::turnOnPc); // works because c.turnOnPc() exists
167+
inventory.forEach(Computer::turnOffPcStatic); // works because Computer.turnOffPcStatic(Computer) exists
168+
169+
170+
171+
// x -> y() lambda_E
172+
173+
Function<Integer, String> f3 = i -> Integer.toHexString(i);
174+
System.out.println("testing->"+ f3.apply(new Integer(300)));
175+
176+
117177
List<Integer> list = new ArrayList<Integer>();
118178
list.add(Integer.valueOf(30));
119179
list.add(Integer.valueOf(50));
@@ -122,15 +182,6 @@ public Test_Java8 c(Object o) {
122182

123183
System.out.println(findNumbers(list, bp).size());
124184

125-
long j = 1;
126-
int i = 3;
127-
j &= ~(1L << i);
128-
j = 1L << i;
129-
j = 1L << 3;
130-
j &= 1L << i;
131-
j = ~i;
132-
j = ~(1L << i);
133-
134185
System.out.println("=== RunnableTest ===");
135186

136187
// Anonymous Runnable
@@ -150,12 +201,6 @@ public void run() {
150201
r2.run();
151202

152203

153-
Computer c1 = new Computer(2015, "white", 100);
154-
Computer c2 = new MacbookPro(2009, "black", 100);
155-
List<Computer> inventory = Arrays.asList(c1, c2);
156-
inventory.forEach(System.out::println);
157-
inventory.forEach(Computer::turnOnPc);
158-
inventory.forEach(Computer::turnOffPcStatic);
159204
System.out.println("Test_Java8 OK");
160205
}
161206

0 commit comments

Comments
 (0)