Skip to content

Commit 3a21089

Browse files
committed
Совместимость с Android-версией
1 parent 8a000cd commit 3a21089

File tree

13 files changed

+120
-69
lines changed

13 files changed

+120
-69
lines changed

src/main/java/com/annimon/ownlang/lib/Converters.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public interface VoidToIntFunction {
2020
int apply();
2121
}
2222

23+
public interface VoidToLongFunction {
24+
long apply();
25+
}
26+
2327
public interface VoidToFloatFunction {
2428
float apply();
2529
}
@@ -28,6 +32,10 @@ public interface VoidToDoubleFunction {
2832
double apply();
2933
}
3034

35+
public interface VoidToCharSequenceFunction {
36+
CharSequence apply();
37+
}
38+
3139
public interface VoidToStringFunction {
3240
String apply();
3341
}
@@ -44,6 +52,14 @@ public interface IntToVoidFunction {
4452
void apply(int i);
4553
}
4654

55+
public interface IntToLongFunction {
56+
long apply(int i);
57+
}
58+
59+
public interface Int2ToVoidFunction {
60+
void apply(int i1, int i2);
61+
}
62+
4763
public interface Int4ToVoidFunction {
4864
void apply(int i1, int i2, int i3, int i4);
4965
}
@@ -68,6 +84,10 @@ public interface Double4ToVoidFunction {
6884
void apply(double d1, double d2, double d3, double d4);
6985
}
7086

87+
public interface CharSequenceToVoidFunction {
88+
void apply(CharSequence s);
89+
}
90+
7191
public interface StringToVoidFunction {
7292
void apply(String s);
7393
}
@@ -88,13 +108,21 @@ public static FunctionValue voidToInt(VoidToIntFunction f) {
88108
return new FunctionValue(args -> NumberValue.of(f.apply()));
89109
}
90110

111+
public static FunctionValue voidToLong(VoidToLongFunction f) {
112+
return new FunctionValue(args -> NumberValue.of(f.apply()));
113+
}
114+
91115
public static FunctionValue voidToFloat(VoidToFloatFunction f) {
92116
return new FunctionValue(args -> NumberValue.of(f.apply()));
93117
}
94118

95119
public static FunctionValue voidToDouble(VoidToDoubleFunction f) {
96120
return new FunctionValue(args -> NumberValue.of(f.apply()));
97121
}
122+
123+
public static FunctionValue voidToCharSequence(VoidToCharSequenceFunction f) {
124+
return new FunctionValue(args -> new StringValue(f.apply().toString()));
125+
}
98126

99127
public static FunctionValue voidToString(VoidToStringFunction f) {
100128
return new FunctionValue(args -> new StringValue(f.apply()));
@@ -131,6 +159,22 @@ public static FunctionValue intToVoid(IntToVoidFunction f) {
131159
});
132160
}
133161

162+
public static FunctionValue intToLong(IntToLongFunction f) {
163+
return new FunctionValue(args -> {
164+
Arguments.check(1, args.length);
165+
return NumberValue.of(f.apply(args[0].asInt()));
166+
});
167+
}
168+
169+
public static FunctionValue int2ToVoid(Int2ToVoidFunction f) {
170+
return new FunctionValue(args -> {
171+
Arguments.check(4, args.length);
172+
f.apply(args[0].asInt(),
173+
args[1].asInt());
174+
return NumberValue.ZERO;
175+
});
176+
}
177+
134178
public static FunctionValue int4ToVoid(Int4ToVoidFunction f) {
135179
return new FunctionValue(args -> {
136180
Arguments.check(4, args.length);
@@ -186,6 +230,23 @@ public static FunctionValue double4ToVoid(Double4ToVoidFunction f) {
186230
});
187231
}
188232

233+
public static FunctionValue charSequenceToVoid(CharSequenceToVoidFunction f) {
234+
return charSequenceToVoid(f, false);
235+
}
236+
237+
public static FunctionValue charSequenceToVoid(CharSequenceToVoidFunction f, boolean emptyAsNull) {
238+
return new FunctionValue(args -> {
239+
Arguments.check(1, args.length);
240+
final String text = args[0].asString();
241+
if (emptyAsNull && (text != null) && (text.isEmpty())) {
242+
f.apply(null);
243+
} else {
244+
f.apply(text);
245+
}
246+
return NumberValue.ZERO;
247+
});
248+
}
249+
189250
public static FunctionValue stringToVoid(StringToVoidFunction f) {
190251
return new FunctionValue(args -> {
191252
Arguments.check(1, args.length);

src/main/java/com/annimon/ownlang/lib/ValueUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.annimon.ownlang.lib;
22

33
import com.annimon.ownlang.exceptions.TypeException;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Modifier;
46
import java.util.Iterator;
57
import java.util.Map;
68
import org.json.JSONArray;
@@ -111,4 +113,17 @@ public static Function consumeFunction(Value value, int argumentNumber) {
111113
}
112114
return ((FunctionValue) value).getValue();
113115
}
116+
117+
public static <T extends Number> MapValue collectNumberConstants(Class<?> clazz, Class<T> type) {
118+
MapValue result = new MapValue(20);
119+
for (Field field : clazz.getDeclaredFields()) {
120+
if (!Modifier.isStatic(field.getModifiers())) continue;
121+
if (!field.getType().equals(type)) continue;
122+
try {
123+
result.set(field.getName(), NumberValue.of((T) field.get(type)));
124+
} catch (IllegalAccessException ignore) {
125+
}
126+
}
127+
return result;
128+
}
114129
}

src/main/java/com/annimon/ownlang/modules/functional/functional_chain.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.annimon.ownlang.modules.functional;
22

3-
import com.annimon.ownlang.exceptions.TypeException;
43
import com.annimon.ownlang.lib.Arguments;
54
import com.annimon.ownlang.lib.Function;
6-
import com.annimon.ownlang.lib.FunctionValue;
7-
import com.annimon.ownlang.lib.Types;
85
import com.annimon.ownlang.lib.Value;
6+
import com.annimon.ownlang.lib.ValueUtils;
97

108
public final class functional_chain implements Function {
119

@@ -15,11 +13,7 @@ public Value execute(Value... args) {
1513

1614
Value result = args[0];
1715
for (int i = 1; i < args.length; i += 2) {
18-
final Value arg = args[i];
19-
if (arg.type() != Types.FUNCTION) {
20-
throw new TypeException(arg.toString() + " is not a function");
21-
}
22-
final Function function = ((FunctionValue) arg).getValue();
16+
final Function function = ValueUtils.consumeFunction(args[i], i);
2317
result = function.execute(result, args[i+1]);
2418
}
2519
return result;

src/main/java/com/annimon/ownlang/modules/functional/functional_dropwhile.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.annimon.ownlang.modules.functional;
22

33
import com.annimon.ownlang.exceptions.TypeException;
4-
import com.annimon.ownlang.lib.*;
4+
import com.annimon.ownlang.lib.Arguments;
5+
import com.annimon.ownlang.lib.ArrayValue;
6+
import com.annimon.ownlang.lib.Function;
7+
import com.annimon.ownlang.lib.NumberValue;
8+
import com.annimon.ownlang.lib.Types;
9+
import com.annimon.ownlang.lib.Value;
10+
import com.annimon.ownlang.lib.ValueUtils;
511

612
public final class functional_dropwhile implements Function {
713

@@ -11,12 +17,8 @@ public Value execute(Value... args) {
1117
if (args[0].type() != Types.ARRAY) {
1218
throw new TypeException("Array expected in first argument");
1319
}
14-
if (args[1].type() != Types.FUNCTION) {
15-
throw new TypeException("Function expected in second argument");
16-
}
17-
1820
final Value container = args[0];
19-
final Function predicate = ((FunctionValue) args[1]).getValue();
21+
final Function predicate = ValueUtils.consumeFunction(args[1], 1);
2022
return dropWhileArray((ArrayValue) container, predicate);
2123
}
2224

src/main/java/com/annimon/ownlang/modules/functional/functional_filter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ public functional_filter(boolean takeWhile) {
1717
@Override
1818
public Value execute(Value... args) {
1919
Arguments.check(2, args.length);
20-
if (args[1].type() != Types.FUNCTION) {
21-
throw new TypeException("Function expected in second argument");
22-
}
23-
2420
final Value container = args[0];
25-
final Function predicate = ((FunctionValue) args[1]).getValue();
21+
final Function predicate = ValueUtils.consumeFunction(args[1], 1);
2622
if (container.type() == Types.ARRAY) {
2723
return filterArray((ArrayValue) container, predicate, takeWhile);
2824
}
@@ -55,4 +51,4 @@ private Value filterMap(MapValue map, Function predicate, boolean takeWhile) {
5551
}
5652
return result;
5753
}
58-
}
54+
}

src/main/java/com/annimon/ownlang/modules/functional/functional_map.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.annimon.ownlang.lib.Arguments;
55
import com.annimon.ownlang.lib.ArrayValue;
66
import com.annimon.ownlang.lib.Function;
7-
import com.annimon.ownlang.lib.FunctionValue;
87
import com.annimon.ownlang.lib.MapValue;
98
import com.annimon.ownlang.lib.Types;
109
import com.annimon.ownlang.lib.Value;
10+
import com.annimon.ownlang.lib.ValueUtils;
1111
import java.util.Map;
1212

1313
public final class functional_map implements Function {
@@ -18,22 +18,13 @@ public Value execute(Value... args) {
1818

1919
final Value container = args[0];
2020
if (container.type() == Types.ARRAY) {
21-
if (args[1].type() != Types.FUNCTION) {
22-
throw new TypeException("Function expected in second arg");
23-
}
24-
final Function mapper = ((FunctionValue) args[1]).getValue();
21+
final Function mapper = ValueUtils.consumeFunction(args[1], 1);
2522
return mapArray((ArrayValue) container, mapper);
2623
}
2724

2825
if (container.type() == Types.MAP) {
29-
if (args[1].type() != Types.FUNCTION) {
30-
throw new TypeException("Function expected in second arg");
31-
}
32-
if (args[2].type() != Types.FUNCTION) {
33-
throw new TypeException("Function expected in third arg");
34-
}
35-
final Function keyMapper = ((FunctionValue) args[1]).getValue();
36-
final Function valueMapper = ((FunctionValue) args[2]).getValue();
26+
final Function keyMapper = ValueUtils.consumeFunction(args[1], 1);
27+
final Function valueMapper = ValueUtils.consumeFunction(args[2], 2);
3728
return mapMap((MapValue) container, keyMapper, valueMapper);
3829
}
3930

src/main/java/com/annimon/ownlang/modules/functional/functional_reduce.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.annimon.ownlang.lib.Arguments;
55
import com.annimon.ownlang.lib.ArrayValue;
66
import com.annimon.ownlang.lib.Function;
7-
import com.annimon.ownlang.lib.FunctionValue;
87
import com.annimon.ownlang.lib.MapValue;
98
import com.annimon.ownlang.lib.Types;
109
import com.annimon.ownlang.lib.Value;
10+
import com.annimon.ownlang.lib.ValueUtils;
1111
import java.util.Map;
1212

1313
public final class functional_reduce implements Function {
@@ -16,12 +16,9 @@ public final class functional_reduce implements Function {
1616
public Value execute(Value... args) {
1717
Arguments.check(3, args.length);
1818

19-
if (args[2].type() != Types.FUNCTION) {
20-
throw new TypeException("Function expected in third argument");
21-
}
2219
final Value container = args[0];
2320
final Value identity = args[1];
24-
final Function accumulator = ((FunctionValue) args[2]).getValue();
21+
final Function accumulator = ValueUtils.consumeFunction(args[2], 2);
2522
if (container.type() == Types.ARRAY) {
2623
Value result = identity;
2724
final ArrayValue array = (ArrayValue) container;

src/main/java/com/annimon/ownlang/modules/functional/functional_sortby.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public final class functional_sortby implements Function {
1515
public Value execute(Value... args) {
1616
Arguments.check(2, args.length);
1717
if (args[0].type() != Types.ARRAY) {
18-
throw new TypeException("Array expected in first argument");
18+
throw new TypeException("Array expected at first argument");
1919
}
20+
2021
final Value[] elements = ((ArrayValue) args[0]).getCopyElements();
2122
final Function function = ValueUtils.consumeFunction(args[1], 1);
2223
Arrays.sort(elements, (o1, o2) -> function.execute(o1).compareTo(function.execute(o2)));

src/main/java/com/annimon/ownlang/modules/functional/functional_stream.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ private Value sorted(Value... args) {
9696
Arrays.sort(elements);
9797
break;
9898
case 1:
99-
if (args[0].type() != Types.FUNCTION) {
100-
throw new TypeException("Function expected in second argument");
101-
}
102-
final Function comparator = ((FunctionValue) args[0]).getValue();
99+
final Function comparator = ValueUtils.consumeFunction(args[0], 0);
103100
Arrays.sort(elements, (o1, o2) -> comparator.execute(o1, o2).asInt());
104101
break;
105102
default:
@@ -111,10 +108,7 @@ private Value sorted(Value... args) {
111108

112109
private Value custom(Value... args) {
113110
Arguments.check(1, args.length);
114-
if (args[0].type() != Types.FUNCTION) {
115-
throw new TypeException("Function expected in first argument");
116-
}
117-
final Function f = ((FunctionValue) args[0]).getValue();
111+
final Function f = ValueUtils.consumeFunction(args[0], 0);
118112
final Value result = f.execute(container);
119113
if (result.type() == Types.ARRAY) {
120114
return new StreamValue((ArrayValue) result);
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.annimon.ownlang.modules.std;
22

3-
import com.annimon.ownlang.exceptions.TypeException;
4-
import com.annimon.ownlang.lib.*;
3+
import com.annimon.ownlang.lib.Arguments;
4+
import com.annimon.ownlang.lib.Function;
5+
import com.annimon.ownlang.lib.FunctionValue;
6+
import com.annimon.ownlang.lib.NumberValue;
7+
import com.annimon.ownlang.lib.Value;
8+
import com.annimon.ownlang.lib.ValueUtils;
59
import java.util.concurrent.BlockingQueue;
610
import java.util.concurrent.LinkedBlockingQueue;
711

@@ -10,9 +14,6 @@ public final class std_sync implements Function {
1014
@Override
1115
public Value execute(Value... args) {
1216
Arguments.check(1, args.length);
13-
if (args[0].type() != Types.FUNCTION) {
14-
throw new TypeException(args[0].toString() + " is not a function");
15-
}
1617

1718
final BlockingQueue<Value> queue = new LinkedBlockingQueue<>(2);
1819
final Function synchronizer = (sArgs) -> {
@@ -23,7 +24,7 @@ public Value execute(Value... args) {
2324
}
2425
return NumberValue.ZERO;
2526
};
26-
final Function callback = ((FunctionValue) args[0]).getValue();
27+
final Function callback = ValueUtils.consumeFunction(args[0], 0);
2728
callback.execute(new FunctionValue(synchronizer));
2829

2930
try {
@@ -34,4 +35,4 @@ public Value execute(Value... args) {
3435
}
3536
}
3637

37-
}
38+
}

0 commit comments

Comments
 (0)