Skip to content
Merged
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
Binary file modified sources/net.sf.j2s.core/dist/SwingJS-site.zip
Binary file not shown.
Binary file modified sources/net.sf.j2s.core/dist/dropins/net.sf.j2s.core.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/dropins/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20180804142820
20180805231348
Binary file modified sources/net.sf.j2s.core/dist/dropins/ver/3.2.2/SwingJS-site.zip
Binary file not shown.
Binary file modified sources/net.sf.j2s.core/dist/dropins/ver/3.2.2/net.sf.j2s.core.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/dropins/ver/3.2.2/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20180804142820
20180805231348

Large diffs are not rendered by default.

Binary file modified sources/net.sf.j2s.java.core/dist/SwingJS-site.zip
Binary file not shown.
11 changes: 11 additions & 0 deletions sources/net.sf.j2s.java.core/src/test/Test_.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public void testInner() {
private int test3() {
// Some random tests

{
long j = 1;
int i = 3;
j &= ~(1L << i);
j = 1L << i;
j = 1L << 3;
j &= 1L << i;
j = ~i;
j = ~(1L << i);
}

System.out.println("abcde".indexOf(99));
assert ("test".contentEquals(new StringBuffer("test")));
int i = "test\2".charAt(4);
Expand Down
137 changes: 91 additions & 46 deletions sources/net.sf.j2s.java.core/src/test/Test_Java8.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,45 @@
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntConsumer;

import test.baeldung.doublecolon.Computer;
import test.baeldung.doublecolon.MacbookPro;

public class Test_Java8 extends Test_ implements PropertyChangeListener {

public Test_Java8() {
System.out.println("null constructor");
}

public Test_Java8(String s) {
System.out.println("new Test_Java8(" + s + ")");
}

public static void test() {

}

enum E {
E1, E2;
}
int test1 = 1;
int test1 = 1;

Object test2(Object o) {
System.out.println("test1 is " + test1 + " " + o);
return o;
};

static int test1s = 2;

static Integer test2is(Integer i) {
System.out.println("test1s is " + test1s + " " + i);
return i;
};

Integer test2i(Integer i) {
System.out.println("test1 is " + test1 + " " + i);
return i;
};

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

public Test_Java8() {System.out.println("null constructor");}
public Test_Java8(Object o) {System.out.println("o is " + o);}

@FunctionalInterface
public interface ITest {
Test_Java8 c(Object o);
Expand All @@ -56,13 +74,32 @@ public interface ITest {
public static void main(String[] args) {

Comparator comp = Comparator.naturalOrder();

new Test_Java8().propertyChange(null); // checking for nonqualified

ITest t = new ITest() {
@Override
public Test_Java8 c(Object o) {
return new Test_Java8(o + "??");
}
};
Test_Java8 test = t.c("new");

System.out.println("what is test? " + test);



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



final String function = "" + new Date();

int[][][] x = new int[5][][];

// Function

int[][][] a;

Function<Integer, int[][][]> ifi = new Function<Integer, int[][][]>() {

@Override
Expand All @@ -71,49 +108,72 @@ public int[][][] apply(Integer t) {
return new int[t.intValue()][][];
}
};
int[][][] a = ifi.apply(5);
a = ifi.apply(5);
System.out.println("a length is " + a.length);

a = (new Function<Integer, int[][][]>() {
@Override
public int[][][] apply(Integer t) {
return new int[t.intValue()][][];
}
}).apply(5);

}).apply(6);
System.out.println("a.length is " + a.length);

// // Lambda_C

Function<Integer, int[][][]> iaCreator = int[][][]::new;
a = iaCreator.apply(5);
a = iaCreator.apply(7);
System.out.println("a.length is " + a.length);
Test_Java8[] atest = new Test_Java8[5];
// following format only works for
Function<Object,Test_Java8> iaCreator2= Test_Java8::new;

Function<String,Test_Java8> iaCreator2= Test_Java8::new;
iaCreator2.apply("testNew");


ITest t;
t = Test_Java8::new;
t = new ITest() {
@Override
public Test_Java8 c(Object o) {
return new Test_Java8(o + "??");
}
};
Test_Java8 test = t.c("new");

System.out.println("what is test? " + test);
Consumer<String> c = s -> System.out.println(s);
c.accept("testingPointer");

c = System.out::println;
c.accept("testing");
c.accept("testingDoubleColon");



Function<Consumer<String>, ?> f = new Test_Java8(null)::test2;
// Q: where is the object in this expression? test.test2.apply(test, [args])
f.apply(c);
Function<Integer, Integer> f1 = new Test_Java8("for f1")::test2i;
f1.apply(new Integer(3));

Function<Integer, Integer> f2 = Test_Java8::test2is;
f2.apply(new Integer(3));

Function<Consumer<String>, ?> f = new Test_Java8("for f")::test2;
f.apply(c);

Test_Java8 t8 = new Test_Java8("new8");
f = t8::test2;
f.apply(c);
f.apply(System.out::println);

// lambda_M

t8.propertyChange(null); // checking for nonqualified

Computer c1 = new Computer(2015, "white", 100);
Computer c2 = new MacbookPro(2009, "black", 100);
List<Computer> inventory = Arrays.asList(c1, c2);

inventory.forEach(System.out::println); // works because System.out.println(Object) exists

// note that YOU CANNOT HAVE BOTH static Computer.turnOnPc(Object) and computer.turnOnPc()
// so it should be sufficient to call one or the other:
// Computer.turnOnPc$Computer.apply(x)
// or
// Computer.prototype.turnOnPc$.apply()

inventory.forEach(Computer::turnOnPc); // works because c.turnOnPc() exists
inventory.forEach(Computer::turnOffPcStatic); // works because Computer.turnOffPcStatic(Computer) exists



// x -> y() lambda_E

Function<Integer, String> f3 = i -> Integer.toHexString(i);
System.out.println("testing->"+ f3.apply(new Integer(300)));


List<Integer> list = new ArrayList<Integer>();
list.add(Integer.valueOf(30));
list.add(Integer.valueOf(50));
Expand All @@ -122,15 +182,6 @@ public Test_Java8 c(Object o) {

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

long j = 1;
int i = 3;
j &= ~(1L << i);
j = 1L << i;
j = 1L << 3;
j &= 1L << i;
j = ~i;
j = ~(1L << i);

System.out.println("=== RunnableTest ===");

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


Computer c1 = new Computer(2015, "white", 100);
Computer c2 = new MacbookPro(2009, "black", 100);
List<Computer> inventory = Arrays.asList(c1, c2);
inventory.forEach(System.out::println);
inventory.forEach(Computer::turnOnPc);
inventory.forEach(Computer::turnOffPcStatic);
System.out.println("Test_Java8 OK");
}

Expand Down
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.java.core/srcjs/js/core/corebottom2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sources/net.sf.j2s.java.core/srcjs/js/core/coretop2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 55 additions & 1 deletion sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// TODO: CharacterSequence does not implement Java 8 default methods chars() or codePoints()
// It is possible that these might be loaded dynamically.

// BH 8/5/2018 3.2.2 adds Clazz.newLambda(...)
// BH 8/4/2018 3.2.2 cleans up String $-qualified methods headless and javax tests pass
// BH 8/1/2018 3.2.2 adds default interface methods as C$.$defaults$
// BH 7/28/2018 3.2.2 upgrade to all-qualified methods.
Expand Down Expand Up @@ -611,6 +612,60 @@ Clazz.newInterface = function (prefix, name, _null1, _null2, interfacez, _0) {
return Clazz.newClass(prefix, name, function(){}, null, interfacez, 0);
};

var lambdaCache = {};

Clazz.newLambda = function(fc, m, isFunc) {
var key = (fc.__CLASS_NAME__ || fc) + "." + m + "." + isFunc;
var ret = lambdaCache[key];
if (ret)
return ret;
// creates a new functional interface
// fc is either an executable method from i -> fc() or a class or object from Class::meth
// meth is the method name
// isFunct == true for Function; false for Consumer
var fAction, fAfter;
if (m) { // Lambda_M
var g = fc[m];
var f = g||fc.prototype[m];
fAction = function(t) {return f.apply(f == g ? fc : t,[t])};
} else { // Lambda_E
fAction = fc;
}
if (isFunc) {
fAfter = function(t) { fAction(t); after.apply$(t); };
} else {
fAfter = function(t) { fAction(t); after.accept$(t); };
}

var andThen = function(after) {
if (!after) throw new NullPointerException();
return fAfter(t);
};

if (isFunc) {
ret = {
apply$: fAction,
andThen$java_util_function_Function: andThen,
compose$java_util_function_Function: function(before) {
if (!before) throw new NullPointerException();
return function(t) { fAction(before.apply$(t)); };
},
identity$: function(t) { return t},
__CLASS_NAME__:"java_util_function_Function"
};


} else {
ret = {accept$: fAction,
andThen$java_util_function_Consumer: andThen,
__CLASS_NAME__:"java_util_function_Consumer"
};
}
return lambdaCache[key] = ret;
};



Clazz.newMeth = function (clazzThis, funName, funBody, modifiers) {

if (!__allowOverwriteClass && clazzThis.prototype[funName])
Expand Down Expand Up @@ -5223,4 +5278,3 @@ var newMethodNotFoundException = function (clazz, method) {
})(Clazz, J2S);
}; // called by external application


Loading