Skip to content

Commit 48f3ce1

Browse files
committed
Более удобная проверка аргументов функций
1 parent 23f0b34 commit 48f3ce1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+93
-96
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.annimon.ownlang.lib;
2+
3+
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
4+
5+
public final class Arguments {
6+
7+
public static void check(int expected, int got) {
8+
if (got != expected) throw new ArgumentsMismatchException(String.format(
9+
"%d %s expected, got %d", expected, pluralize(expected), got));
10+
}
11+
12+
public static void checkAtLeast(int expected, int got) {
13+
if (got < expected) throw new ArgumentsMismatchException(String.format(
14+
"At least %d %s expected, got %d", expected, pluralize(expected), got));
15+
}
16+
17+
public static void checkOrOr(int expectedOne, int expectedTwo, int got) {
18+
if (expectedOne != got && expectedTwo != got)
19+
throw new ArgumentsMismatchException(String.format(
20+
"%d or %d arguments expected, got %d", expectedOne, expectedTwo, got));
21+
}
22+
23+
public static void checkRange(int from, int to, int got) {
24+
if (from > got || got > to)
25+
throw new ArgumentsMismatchException(String.format(
26+
"From %d to %d arguments expected, got %d", from, to, got));
27+
}
28+
29+
private static String pluralize(int count) {
30+
return (count == 1) ? "argument" : "arguments";
31+
}
32+
}

src/com/annimon/ownlang/lib/modules/canvas.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static void clip(int x, int y, int w, int h) {
8888

8989
private static Function intConsumer4Convert(IntConsumer4 consumer) {
9090
return args -> {
91-
if (args.length != 4) throw new ArgumentsMismatchException("Four args expected");
91+
Arguments.check(4, args.length);
9292
consumer.accept(args[0].asInt(), args[1].asInt(), args[2].asInt(), args[3].asInt());
9393
return NumberValue.ZERO;
9494
};
@@ -181,7 +181,7 @@ private static class DrawString implements Function {
181181

182182
@Override
183183
public Value execute(Value... args) {
184-
if (args.length != 3) throw new ArgumentsMismatchException("Three args expected");
184+
Arguments.check(3, args.length);
185185
int x = args[1].asInt();
186186
int y = args[2].asInt();
187187
graphics.drawString(args[0].asString(), x, y);

src/com/annimon/ownlang/lib/modules/files.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private static class fopen implements Function {
6969

7070
@Override
7171
public Value execute(Value... args) {
72-
if (args.length < 1) throw new ArgumentsMismatchException("At least one argument expected");
72+
Arguments.checkAtLeast(1, args.length);
7373

7474
final File file = new File(args[0].asString());
7575
try {

src/com/annimon/ownlang/lib/modules/functions/functional_combine.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65

76
public final class functional_combine implements Function {
87

98
@Override
109
public Value execute(Value... args) {
11-
if (args.length < 1) throw new ArgumentsMismatchException("At least one arg expected");
12-
10+
Arguments.checkAtLeast(1, args.length);
1311
Function result = null;
1412
for (Value arg : args) {
1513
if (arg.type() != Types.FUNCTION) {

src/com/annimon/ownlang/lib/modules/functions/functional_filter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65
import java.util.ArrayList;
@@ -12,11 +11,11 @@ public final class functional_filter implements Function {
1211

1312
@Override
1413
public Value execute(Value... args) {
15-
if (args.length < 2) throw new ArgumentsMismatchException("At least two args expected");
16-
14+
Arguments.check(2, args.length);
1715
if (args[1].type() != Types.FUNCTION) {
18-
throw new TypeException("Function expected in second arg");
16+
throw new TypeException("Function expected in second argument");
1917
}
18+
2019
final Value container = args[0];
2120
final Function consumer = ((FunctionValue) args[1]).getValue();
2221
if (container.type() == Types.ARRAY) {

src/com/annimon/ownlang/lib/modules/functions/functional_flatmap.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65
import java.util.ArrayList;
@@ -10,8 +9,7 @@ public final class functional_flatmap implements Function {
109

1110
@Override
1211
public Value execute(Value... args) {
13-
if (args.length < 2) throw new ArgumentsMismatchException("At least two arguments expected");
14-
12+
Arguments.check(2, args.length);
1513
if (args[0].type() != Types.ARRAY) {
1614
throw new TypeException("Array expected in first argument");
1715
}

src/com/annimon/ownlang/lib/modules/functions/functional_foreach.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65

@@ -10,7 +9,7 @@ public final class functional_foreach implements Function {
109

1110
@Override
1211
public Value execute(Value... args) {
13-
if (args.length != 2) throw new ArgumentsMismatchException("Two args expected");
12+
Arguments.check(2, args.length);
1413

1514
if (args[1].type() != Types.FUNCTION) {
1615
throw new TypeException("Function expected in second arg");

src/com/annimon/ownlang/lib/modules/functions/functional_map.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65

@@ -10,7 +9,7 @@ public final class functional_map implements Function {
109

1110
@Override
1211
public Value execute(Value... args) {
13-
if (args.length < 2) throw new ArgumentsMismatchException("At least two args expected");
12+
Arguments.checkOrOr(2, 3, args.length);
1413

1514
final Value container = args[0];
1615
if (container.type() == Types.ARRAY) {

src/com/annimon/ownlang/lib/modules/functions/functional_reduce.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65

@@ -10,10 +9,10 @@ public final class functional_reduce implements Function {
109

1110
@Override
1211
public Value execute(Value... args) {
13-
if (args.length != 3) throw new ArgumentsMismatchException("Three args expected");
12+
Arguments.check(3, args.length);
1413

1514
if (args[2].type() != Types.FUNCTION) {
16-
throw new TypeException("Function expected in third arg");
15+
throw new TypeException("Function expected in third argument");
1716
}
1817
final Value container = args[0];
1918
final Value identity = args[1];

src/com/annimon/ownlang/lib/modules/functions/functional_sortby.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.annimon.ownlang.lib.modules.functions;
22

3-
import com.annimon.ownlang.exceptions.ArgumentsMismatchException;
43
import com.annimon.ownlang.exceptions.TypeException;
54
import com.annimon.ownlang.lib.*;
65
import java.util.Arrays;
@@ -9,8 +8,7 @@ public final class functional_sortby implements Function {
98

109
@Override
1110
public Value execute(Value... args) {
12-
if (args.length != 2) throw new ArgumentsMismatchException("Two arguments expected");
13-
11+
Arguments.check(2, args.length);
1412
if (args[0].type() != Types.ARRAY) {
1513
throw new TypeException("Array expected in first argument");
1614
}

0 commit comments

Comments
 (0)