Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
faster String.toCharArray(); Differences.txt
  • Loading branch information
hansonr authored and hansonr committed Aug 16, 2019
commit 5a59be06def92390f75f2fab17eed146f700b143
Binary file modified sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20190727175738
20190814182015
Binary file modified sources/net.sf.j2s.core/dist/swingjs/ver/3.2.4/SwingJS-site.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/ver/3.2.4/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20190727175738
20190814182015
Binary file modified sources/net.sf.j2s.java.core/dist/SwingJS-site.zip
Binary file not shown.
41 changes: 27 additions & 14 deletions sources/net.sf.j2s.java.core/doc/Differences.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Notes
=====
updated 8/16/19 -- minor typos and added summary paragraph
updated 7/19/19 -- clarification that AWT and Swing classes are supported directly
updated 5/13/19 -- Mandarin U+79D8 reserved character; Missing Math methods; int and long
updated 5/10/19 -- adds a section on static issues in multi-(duplicate)-applet pages
updated 1/4/19 -- nio
updated 9/15/18 -- adds integer 1/0 == 0
updated 9/15/18 -- adds integer 1/0 == Infinity
updated 7/24/18 -- most classes replaced with https://github.com/frohoff/jdk8u-jdk
updated 6/5/17 -- reserved package name "window"
updated 3/11/17 -- myClass.getField
Expand Down Expand Up @@ -537,14 +538,14 @@ qualified field and method names

In order to minimize the chance of added SwingJS field and method names colliding with ones
developers might use in subclassing Java classes, we have added U+79D8 (first character of Mandarin
"secrect") to the characters already disrecommended by Java documentation ("$" and "_"). The only problem
"secret") to the characters already disrecommended by Java documentation ("$" and "_"). The only problem
would be if you use that character followed by certain English words in certain classes. For example
\u79D8canvas for JComponents (in java.awt.JSComponent) and \u79D8byte (in java.io.File).

missing Math methods
--------------------

java.lang.Math is worked out, but some methods are missing, eithr because they
java.lang.Math is worked out, but some methods are missing, either because they
involve long integer value that are inaccessible in JavaScript, or because I just
didn't implement them. This is a result of continued Java development.
It is easy enough to add these methods if you have the source. They go into j2sClazz.js,
Expand All @@ -566,7 +567,7 @@ For example, this will definitely NOT work in SwingJS:

this.paint(getGraphics())

and really should not in Java, either, as it is technically a resource memory leak.
and really should not work in Java, either, as it is technically a resource memory leak.

Instead, if you really do not want to use repaint(), use this:

Expand Down Expand Up @@ -601,7 +602,7 @@ Static classes such as:

which are created using Class.forName are implemented using classes in the swingjs package.

AWTAccessor and AwtContext need to be customized
AWTAccessor is not implemented.


AWT component peers and component "ui" user interfaces
Expand Down Expand Up @@ -739,7 +740,7 @@ to BigInteger:
* the integer storage bit length to 24, giving 48 for long and leaving
* the last 16 bits clear for the exponent of the double number. This should
* not affect performance significantly. It does increase the storage
* size by about 33%. By bring an "int" to 3 bytes, we can easily construct
* size by about 33%. By bringing an "int" to 3 bytes, we can easily construct
* and use byte[] data intended for the original BitSet.

"Easily" may be a bit strong there. This was a serious challenge.
Expand Down Expand Up @@ -772,13 +773,11 @@ JEditorPane (JavaScript <div>)

For the initial implementation, we don't implement infinite undo/redo, and the abstract
document model is much less elaborate. Only PlainDocument (in the form of JSPlainDocument)
is implemented.
The Document returned by JTextField.getDocument() is a javax.swing.text.Document.
is implemented. The Document returned by JTextField.getDocument() is a javax.swing.text.Document.

all scrolling is handled by HTML5
javax.swing.AutoScroller is not implemented
All scrolling is handled by HTML5. javax.swing.AutoScroller is not implemented.
public static methods .stop, .isRunning, .processMouseDragged require true Java threading
javax.swing.text.View and its subclasses are not implemented.
and so are not implmented. javax.swing.text.View and its subclasses are not implemented.

The JS document model does not allow two text fields to address the same underlying document.

Expand All @@ -792,9 +791,23 @@ Matcher.start(groupID) is not supported.

java.util.Formatter will function correctly for all standard %... patterns.

integer 1/0 == 0
----------------
integer 1/0 == Infinity
-----------------------

1/0 in Java throws "java.lang.ArithmeticException: / by zero", but in JavaScript is just Infinity.




Summary
-------

These are all the known limitations of SwingJS. We have not found any of these limitations
to be show-stoppers. The primary issue for newcomers to SwingJS is having the source code.
You must check that source code for all your library jar files is available or, if you
choose, you will need to decompile those classes. We have used decompilation on some projects,
and it works just fine. So, technically, all we really need are JAR/class files. But the
source is by far superior. It's generally prettier, and it has the license information that
may or may not be present with the JAR or class files. Use class files at your own risk.

Bob Hanson
2019.08.16
2 changes: 2 additions & 0 deletions sources/net.sf.j2s.java.core/src/test/Test_Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ public static void main(String[] args) {
double[] da = new double[3];
System.out.println(da[iI]);
assert (ii3[2] == 1 && j == 1);


System.out.println("Test_Array OK");
}


}
223 changes: 223 additions & 0 deletions sources/net.sf.j2s.java.core/src/test/Test_Interval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

class Test_Interval extends Test_ {

static int[] i0 = new int[0];

static int[][] a = new int[][] {
{ 1, 7 },
{ 2, 4 },
{ 3, 5 },
{ 3, 6 },
{ 4, 5 },
{ 7, 8 },
{ 9, 10 }

};

public static void main(String[] args) {

// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
int[] ap = new int[] { 10, 20, 30, 40, 50, 50, 50, 60, 70, 80, 90, 100, 110, 120 };

System.out.println(findClosestPoint(ap, 11, true)); // 0
System.out.println(findClosestPoint(ap, 11, false)); // 1
System.out.println(findClosestPoint(ap, 51, true)); // 6
System.out.println(findClosestPoint(ap, 51, false)); // 7
System.out.println(findClosestPoint(ap, 130, true));// -1
System.out.println(findClosestPoint(ap, 130, false)); // -1
System.out.println(findClosestPoint(ap, 0, true)); // -1
System.out.println(findClosestPoint(ap, 0, false)); // -1
System.out.println(findClosestPoint(ap, 30, true)); // 2
System.out.println(findClosestPoint(ap, 60, true)); // 7
System.out.println(findClosestPoint(ap, 50, true)); // 6
System.out.println(findClosestPoint(ap, 50, false)); // 4

assert (findClosestPoint(ap, 11, true) == 0);
assert (findClosestPoint(ap, 11, false) == 1);
assert (findClosestPoint(ap, 51, true) == 6);
assert (findClosestPoint(ap, 51, false) == 7);
assert (findClosestPoint(ap, 130, true) == -1);
assert (findClosestPoint(ap, 130, false) == -1);
assert (findClosestPoint(ap, 0, true) == -1);
assert (findClosestPoint(ap, 0, false) == -1);
assert (findClosestPoint(ap, 30, true) == 2);
assert (findClosestPoint(ap, 60, true) == 7);
assert (findClosestPoint(ap, 50, true) == 6);
assert (findClosestPoint(ap, 50, false) == 4);

new Test_Interval().findIntervals(a, 3);
new Test_Interval().findIntervals(a, 5);
new Test_Interval().findIntervals(a, 9);

System.out.println("Test_Interval OK");
}

private static Interval[] ints;

private void findIntervals(int[][] a, int pos) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Random rand = new Random();
int n = a.length;
Interval[] intervals = new Interval[n];
for (int i = 0; i < n;) {
int r = rand.nextInt(n);
if (intervals[r] == null) {
intervals[r] = new Interval(a[i][0], a[i][1], i);
i++;
}
}
System.out.println("=====");
//dumpIntervals(intervals);
System.out.println("-----");
ints = intervals;
Arrays.sort(intervals, new IComparator());
linkIntervals(intervals);
//dumpIntervals(intervals);
List<Interval> result = new ArrayList<>();
findInterval(intervals, pos, result);
for (int i = 0; i < result.size(); i++)
System.out.println(pos + " " + result.get(i));

}

private void findInterval(Interval[] intervals, int pos, List<Interval> result) {
Interval ithis = findClosestInterval(intervals, pos);
while (ithis != null) {
if (ithis.end >= pos)
result.add(ithis);
ithis = ithis.containedBy;
}
}

private Interval findClosestInterval(Interval[] l, int pos) {
int low = 0;
int high = l.length - 1;
int mid = 0;
while (low <= high) {
mid = (low + high) >>> 1;
int f = l[mid].begin;
switch (Long.signum(f - pos)) {
case -1:
low = mid + 1;
continue;
case 1:
high = mid - 1;
continue;
case 0:
while (++mid <= high && l[mid].begin == pos) {
;
}
mid--;
return l[mid];
}
}
return (high < 0 || low >= l.length ? null : l[high]);
}

static void dumpIntervals(Interval[] intervals) {
for (int i = 0; i < intervals.length; i++) {
System.out.println(intervals[i]);
}
System.out.println("");
}

public void linkIntervals(Interval[] intervals) {
if (intervals.length < 2)
return;
int maxEnd = intervals[0].end;
for (int i = 1, n = intervals.length; i < n; i++) {
Interval ithis = intervals[i];
if (ithis.begin <= maxEnd)
ithis.containedBy = getContainedBy(intervals, i);
if (ithis.end > maxEnd)
maxEnd = ithis.end;
}

}

private Interval getContainedBy(Interval[] intervals, int i) {
Interval ithis = intervals[i];
while (--i >= 0) {
Interval ilast = intervals[i];
if (ithis.begin <= ilast.end) {
return ilast;
}
}
return null;
}

class Interval {
int begin, end, index;
Interval containedBy;

Interval(int begin, int end, int index) {
this.begin = begin;
this.end = end;
this.index = index;
}

@Override
public String toString() {
return "I" + index + " begin:" + begin + " end:" + end + " cont:"
+ (containedBy == null ? "?" : containedBy.index);
}

}

class IComparator implements Comparator<Interval> {

@Override
public int compare(Interval a, Interval b) {
int val = (a.begin < b.begin ? -1 : a.begin > b.begin ? 1 : a.end > b.end ? 1 : a.end < b.end ? -1 : 0);
return val;
}

}

static private int findClosestPoint(int[] l, int pos, boolean isStart) {
int low = 0;
int high = l.length - 1;
int mid = 0;
while (low <= high) {
mid = (low + high) >>> 1;
int f = l[mid];
switch (Long.signum(f - pos)) {
case -1:
low = mid + 1;
continue;
case 1:
high = mid - 1;
continue;
case 0:
if (isStart) {
while (--mid >= low && (f = l[mid]) != -1 && f == pos) {
;
}
++mid;
} else {
while (++mid <= high && (f = l[mid]) != -1 && f == pos) {
;
}
mid--;
}
return mid;
}
}
// -1 here?
System.out.println(isStart + " " + low + " " + mid + " " + high);
return (high < 0 || low >= l.length ? -1 : isStart ? high : low);
}

}
7 changes: 2 additions & 5 deletions sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js
Original file line number Diff line number Diff line change
Expand Up @@ -4424,11 +4424,8 @@ sp.contains$S = function(a) {return this.indexOf(a) >= 0} // bh added
sp.compareTo$ = sp.compareTo$S = sp.compareTo$TT = function(a){return this > a ? 1 : this < a ? -1 : 0} // bh added

sp.toCharArray$=function(){
var result=new Array(this.length);
for(var i=0;i<this.length;i++){
result[i]=this.charAt(i);
}
return Clazz.array(Character.TYPE, -1, result);
var result = this.split("");
return setArray(result, Character.TYPE, "CA", -1);
};

String.valueOf$ = String.valueOf$Z = String.valueOf$C = String.valueOf$CA
Expand Down
7 changes: 2 additions & 5 deletions sources/net.sf.j2s.java.core/srcjs/swingjs2.js
Original file line number Diff line number Diff line change
Expand Up @@ -18146,11 +18146,8 @@ sp.contains$S = function(a) {return this.indexOf(a) >= 0} // bh added
sp.compareTo$ = sp.compareTo$S = sp.compareTo$TT = function(a){return this > a ? 1 : this < a ? -1 : 0} // bh added

sp.toCharArray$=function(){
var result=new Array(this.length);
for(var i=0;i<this.length;i++){
result[i]=this.charAt(i);
}
return Clazz.array(Character.TYPE, -1, result);
var result = this.split("");
return setArray(result, Character.TYPE, "CA", -1);
};

String.valueOf$ = String.valueOf$Z = String.valueOf$C = String.valueOf$CA
Expand Down