|
1 | | -package com.annimon.ownlang.lib; |
2 | | - |
3 | | -import com.annimon.ownlang.exceptions.TypeException; |
4 | | -import java.lang.reflect.Field; |
5 | | -import java.lang.reflect.Modifier; |
6 | | -import java.util.Iterator; |
7 | | -import java.util.Map; |
8 | | -import org.json.JSONArray; |
9 | | -import org.json.JSONObject; |
10 | | - |
11 | | -public final class ValueUtils { |
12 | | - |
13 | | - private ValueUtils() { } |
14 | | - |
15 | | - public static Object toObject(Value val) { |
16 | | - switch (val.type()) { |
17 | | - case Types.ARRAY: |
18 | | - return toObject((ArrayValue) val); |
19 | | - case Types.MAP: |
20 | | - return toObject((MapValue) val); |
21 | | - case Types.NUMBER: |
22 | | - return val.raw(); |
23 | | - case Types.STRING: |
24 | | - return val.asString(); |
25 | | - default: |
26 | | - return JSONObject.NULL; |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - public static Object toObject(MapValue map) { |
31 | | - final JSONObject result = new JSONObject(); |
32 | | - for (Map.Entry<Value, Value> entry : map) { |
33 | | - final String key = entry.getKey().asString(); |
34 | | - final Object value = toObject(entry.getValue()); |
35 | | - result.put(key, value); |
36 | | - } |
37 | | - return result; |
38 | | - } |
39 | | - |
40 | | - public static Object toObject(ArrayValue array) { |
41 | | - final JSONArray result = new JSONArray(); |
42 | | - for (Value value : array) { |
43 | | - result.put(toObject(value)); |
44 | | - } |
45 | | - return result; |
46 | | - } |
47 | | - |
48 | | - public static Value toValue(Object obj) { |
49 | | - if (obj instanceof JSONObject) { |
50 | | - return toValue((JSONObject) obj); |
51 | | - } |
52 | | - if (obj instanceof JSONArray) { |
53 | | - return toValue((JSONArray) obj); |
54 | | - } |
55 | | - if (obj instanceof String) { |
56 | | - return new StringValue((String) obj); |
57 | | - } |
58 | | - if (obj instanceof Number) { |
59 | | - return NumberValue.of(((Number) obj)); |
60 | | - } |
61 | | - if (obj instanceof Boolean) { |
62 | | - return NumberValue.fromBoolean((Boolean) obj); |
63 | | - } |
64 | | - // NULL or other |
65 | | - return NumberValue.ZERO; |
66 | | - } |
67 | | - |
68 | | - public static MapValue toValue(JSONObject json) { |
69 | | - final MapValue result = new MapValue(json.length()); |
70 | | - final Iterator<String> it = json.keys(); |
71 | | - while(it.hasNext()) { |
72 | | - final String key = it.next(); |
73 | | - final Value value = toValue(json.get(key)); |
74 | | - result.set(new StringValue(key), value); |
75 | | - } |
76 | | - return result; |
77 | | - } |
78 | | - |
79 | | - public static ArrayValue toValue(JSONArray json) { |
80 | | - final int length = json.length(); |
81 | | - final ArrayValue result = new ArrayValue(length); |
82 | | - for (int i = 0; i < length; i++) { |
83 | | - final Value value = toValue(json.get(i)); |
84 | | - result.set(i, value); |
85 | | - } |
86 | | - return result; |
87 | | - } |
88 | | - |
89 | | - public static Number getNumber(Value value) { |
90 | | - if (value.type() == Types.NUMBER) return ((NumberValue) value).raw(); |
91 | | - return value.asInt(); |
92 | | - } |
93 | | - |
94 | | - public static float getFloatNumber(Value value) { |
95 | | - if (value.type() == Types.NUMBER) return ((NumberValue) value).raw().floatValue(); |
96 | | - return (float) value.asNumber(); |
97 | | - } |
98 | | - |
99 | | - public static byte[] toByteArray(ArrayValue array) { |
100 | | - final int size = array.size(); |
101 | | - final byte[] result = new byte[size]; |
102 | | - for (int i = 0; i < size; i++) { |
103 | | - result[i] = (byte) array.get(i).asInt(); |
104 | | - } |
105 | | - return result; |
106 | | - } |
107 | | - |
108 | | - public static Function consumeFunction(Value value, int argumentNumber) { |
109 | | - return consumeFunction(value, " at argument " + (argumentNumber + 1)); |
110 | | - } |
111 | | - |
112 | | - public static Function consumeFunction(Value value, String errorMessage) { |
113 | | - final int type = value.type(); |
114 | | - if (type != Types.FUNCTION) { |
115 | | - throw new TypeException("Function expected" + errorMessage |
116 | | - + ", but found " + Types.typeToString(type)); |
117 | | - } |
118 | | - return ((FunctionValue) value).getValue(); |
119 | | - } |
120 | | - |
121 | | - public static <T extends Number> MapValue collectNumberConstants(Class<?> clazz, Class<T> type) { |
122 | | - MapValue result = new MapValue(20); |
123 | | - for (Field field : clazz.getDeclaredFields()) { |
124 | | - if (!Modifier.isStatic(field.getModifiers())) continue; |
125 | | - if (!field.getType().equals(type)) continue; |
126 | | - try { |
127 | | - result.set(field.getName(), NumberValue.of((T) field.get(type))); |
128 | | - } catch (IllegalAccessException ignore) { |
129 | | - } |
130 | | - } |
131 | | - return result; |
132 | | - } |
133 | | -} |
| 1 | +package com.annimon.ownlang.lib; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.TypeException; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.lang.reflect.Modifier; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.Map; |
| 8 | +import org.json.JSONArray; |
| 9 | +import org.json.JSONObject; |
| 10 | + |
| 11 | +public final class ValueUtils { |
| 12 | + |
| 13 | + private ValueUtils() { } |
| 14 | + |
| 15 | + public static Object toObject(Value val) { |
| 16 | + switch (val.type()) { |
| 17 | + case Types.ARRAY: |
| 18 | + return toObject((ArrayValue) val); |
| 19 | + case Types.MAP: |
| 20 | + return toObject((MapValue) val); |
| 21 | + case Types.NUMBER: |
| 22 | + return val.raw(); |
| 23 | + case Types.STRING: |
| 24 | + return val.asString(); |
| 25 | + default: |
| 26 | + return JSONObject.NULL; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public static JSONObject toObject(MapValue map) { |
| 31 | + final JSONObject result = new JSONObject(); |
| 32 | + for (Map.Entry<Value, Value> entry : map) { |
| 33 | + final String key = entry.getKey().asString(); |
| 34 | + final Object value = toObject(entry.getValue()); |
| 35 | + result.put(key, value); |
| 36 | + } |
| 37 | + return result; |
| 38 | + } |
| 39 | + |
| 40 | + public static JSONArray toObject(ArrayValue array) { |
| 41 | + final JSONArray result = new JSONArray(); |
| 42 | + for (Value value : array) { |
| 43 | + result.put(toObject(value)); |
| 44 | + } |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + public static Value toValue(Object obj) { |
| 49 | + if (obj instanceof JSONObject) { |
| 50 | + return toValue((JSONObject) obj); |
| 51 | + } |
| 52 | + if (obj instanceof JSONArray) { |
| 53 | + return toValue((JSONArray) obj); |
| 54 | + } |
| 55 | + if (obj instanceof String) { |
| 56 | + return new StringValue((String) obj); |
| 57 | + } |
| 58 | + if (obj instanceof Number) { |
| 59 | + return NumberValue.of(((Number) obj)); |
| 60 | + } |
| 61 | + if (obj instanceof Boolean) { |
| 62 | + return NumberValue.fromBoolean((Boolean) obj); |
| 63 | + } |
| 64 | + // NULL or other |
| 65 | + return NumberValue.ZERO; |
| 66 | + } |
| 67 | + |
| 68 | + public static MapValue toValue(JSONObject json) { |
| 69 | + final MapValue result = new MapValue(json.length()); |
| 70 | + final Iterator<String> it = json.keys(); |
| 71 | + while(it.hasNext()) { |
| 72 | + final String key = it.next(); |
| 73 | + final Value value = toValue(json.get(key)); |
| 74 | + result.set(new StringValue(key), value); |
| 75 | + } |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + public static ArrayValue toValue(JSONArray json) { |
| 80 | + final int length = json.length(); |
| 81 | + final ArrayValue result = new ArrayValue(length); |
| 82 | + for (int i = 0; i < length; i++) { |
| 83 | + final Value value = toValue(json.get(i)); |
| 84 | + result.set(i, value); |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + public static Number getNumber(Value value) { |
| 90 | + if (value.type() == Types.NUMBER) return ((NumberValue) value).raw(); |
| 91 | + return value.asInt(); |
| 92 | + } |
| 93 | + |
| 94 | + public static float getFloatNumber(Value value) { |
| 95 | + if (value.type() == Types.NUMBER) return ((NumberValue) value).raw().floatValue(); |
| 96 | + return (float) value.asNumber(); |
| 97 | + } |
| 98 | + |
| 99 | + public static byte[] toByteArray(ArrayValue array) { |
| 100 | + final int size = array.size(); |
| 101 | + final byte[] result = new byte[size]; |
| 102 | + for (int i = 0; i < size; i++) { |
| 103 | + result[i] = (byte) array.get(i).asInt(); |
| 104 | + } |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + public static Function consumeFunction(Value value, int argumentNumber) { |
| 109 | + return consumeFunction(value, " at argument " + (argumentNumber + 1)); |
| 110 | + } |
| 111 | + |
| 112 | + public static Function consumeFunction(Value value, String errorMessage) { |
| 113 | + final int type = value.type(); |
| 114 | + if (type != Types.FUNCTION) { |
| 115 | + throw new TypeException("Function expected" + errorMessage |
| 116 | + + ", but found " + Types.typeToString(type)); |
| 117 | + } |
| 118 | + return ((FunctionValue) value).getValue(); |
| 119 | + } |
| 120 | + |
| 121 | + public static <T extends Number> MapValue collectNumberConstants(Class<?> clazz, Class<T> type) { |
| 122 | + MapValue result = new MapValue(20); |
| 123 | + for (Field field : clazz.getDeclaredFields()) { |
| 124 | + if (!Modifier.isStatic(field.getModifiers())) continue; |
| 125 | + if (!field.getType().equals(type)) continue; |
| 126 | + try { |
| 127 | + result.set(field.getName(), NumberValue.of((T) field.get(type))); |
| 128 | + } catch (IllegalAccessException ignore) { |
| 129 | + } |
| 130 | + } |
| 131 | + return result; |
| 132 | + } |
| 133 | +} |
0 commit comments