Skip to content

Commit c3a7f17

Browse files
authored
Merge pull request #36 from ccxt/java-helpers-new
Java helpers new
2 parents 6bf5e7b + c020bbb commit c3a7f17

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

tests/integration/java/app/src/main/java/org/example/Helpers.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,16 @@ public static String replaceAll(Object baseString, Object search, Object replace
664664
return s.replace(find, repl); // literal (non-regex) replacement
665665
}
666666

667+
public static String replace(Object baseString, Object search, Object replacement) {
668+
if (baseString == null) {
669+
return null;
670+
}
671+
String s = String.valueOf(baseString);
672+
String find = (search == null) ? "" : String.valueOf(search);
673+
String repl = (replacement == null) ? "" : String.valueOf(replacement);
674+
return s.replaceFirst(find, repl); // literal (non-regex) replacement
675+
}
676+
667677
public static Object getArg(Object[] v, int index, Object def) {
668678
if (v.length <= index) {
669679
return def;
@@ -705,4 +715,32 @@ public static void addElementToObject(Object target, Object... args) {
705715
private static String typeName(Object o) {
706716
return (o == null) ? "null" : o.getClass().getName();
707717
}
718+
719+
public static Object opNeg(Object value) {
720+
if (value == null) {
721+
return null;
722+
}
723+
724+
if (value instanceof Byte) {
725+
byte v = (Byte) value;
726+
return (byte) -v;
727+
}
728+
if (value instanceof Short v) {
729+
return (short) -v;
730+
}
731+
if (value instanceof Integer v) {
732+
return -v;
733+
}
734+
if (value instanceof Long v) {
735+
return -v;
736+
}
737+
if (value instanceof Float v) {
738+
return -v;
739+
}
740+
if (value instanceof Double v) {
741+
return -v;
742+
}
743+
744+
return null;
745+
}
708746
}

tests/integration/java/app/src/main/java/org/example/Transpilable.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ public Object stringifyNumber(Object arg)
1111
}
1212
class Test
1313
{
14+
public void functionWithOptionals(Object a, Object... optionalArgs)
15+
{
16+
Object c = Helpers.getArg(optionalArgs, 0, null);
17+
Object d = Helpers.getArg(optionalArgs, 1, 1);
18+
System.out.println(a);
19+
if (Helpers.isTrue(!Helpers.isEqual(c, null)))
20+
{
21+
System.out.println(c);
22+
}
23+
if (Helpers.isTrue(!Helpers.isEqual(d, null)))
24+
{
25+
System.out.println(d);
26+
}
27+
}
28+
29+
public Object getValue(Object x)
30+
{
31+
return x;
32+
}
33+
34+
public void testJavaScope()
35+
{
36+
Object newObject = new java.util.HashMap<String, Object>() {{
37+
put( "a", Test.this.getValue(5) );
38+
put( "b", Test.this.getValue(Test.this.getValue(Test.this.getValue(2))) );
39+
}};
40+
System.out.println(Helpers.GetValue(newObject, "a")); // should print 5
41+
System.out.println(Helpers.GetValue(newObject, "b")); // should print 2
42+
}
43+
1444
public void test()
1545
{
1646
Object a = 1;
@@ -73,5 +103,15 @@ public void test()
73103
Object baseString = "aabba";
74104
Object replacedAllString = Helpers.replaceAll((String)baseString, (String)"a", (String)"");
75105
System.out.println(replacedAllString); // should print "bb"
106+
this.functionWithOptionals("hello");
107+
this.functionWithOptionals("hello", 5);
108+
this.functionWithOptionals("hello", 5, 1);
109+
Object list3 = new java.util.ArrayList<Object>(java.util.Arrays.asList("empty"));
110+
Helpers.addElementToObject(list3, 0, "first");
111+
System.out.println(Helpers.GetValue(list3, 0)); // should print "first"
112+
Object dict3 = new java.util.HashMap<String, Object>() {{}};
113+
Helpers.addElementToObject(dict3, "key", "value");
114+
System.out.println(Helpers.GetValue(dict3, "key")); // should print "value"
115+
this.testJavaScope();
76116
}
77117
}

0 commit comments

Comments
 (0)