summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorTobrun <tobrun.van.nuland@gmail.com>2018-03-15 11:12:09 +0100
committerTobrun <tobrun@mapbox.com>2018-03-16 17:09:35 +0100
commitaba24da180e657d8f6386443d02581cef3661f84 (patch)
tree642336ffea6a889e4c152187959d174a2cf98115 /platform
parentd59646fc1f80caa7420eb4ada61a5d238505c3f6 (diff)
downloadqtlocation-mapboxgl-aba24da180e657d8f6386443d02581cef3661f84.tar.gz
[android] - implement expression literal on arrays
Diffstat (limited to 'platform')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java49
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java33
2 files changed, 73 insertions, 9 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java
index f6caa0b8e3..8af140019f 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/expressions/Expression.java
@@ -116,6 +116,16 @@ public class Expression {
}
/**
+ * Create a literal array expression
+ *
+ * @param array the array
+ * @return the expression
+ */
+ public static Expression literal(@NonNull Object[] array) {
+ return new ExpressionArray(array);
+ }
+
+ /**
* Expression literal utility method to convert a color int to an color expression
*
* @param color the int color
@@ -1600,7 +1610,7 @@ public class Expression {
@NonNull Expression number, Stop... stops) {
return interpolate(interpolation, number, Stop.toExpressionArray(stops));
}
-
+
/**
* interpolates linearly between the pair of stops just less than and just greater than the input.
*
@@ -2006,4 +2016,41 @@ public class Expression {
}
}
}
+
+ private static class ExpressionArray extends Expression {
+
+ private Object[] array;
+
+ ExpressionArray(Object[] array) {
+ this.array = array;
+ }
+
+ @NonNull
+ @Override
+ public Object[] toArray() {
+ return new Object[] {
+ "literal", array
+ };
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder("[\"literal\"], [");
+ Object argument;
+ for (int i = 0; i < array.length; i++) {
+ argument = array[i];
+ if (argument instanceof String) {
+ builder.append("\"").append(argument).append("\"");
+ } else {
+ builder.append(argument);
+ }
+
+ if (i != array.length - 1) {
+ builder.append(", ");
+ }
+ }
+ builder.append("]]");
+ return builder.toString();
+ }
+ }
} \ No newline at end of file
diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java
index 937755a640..a2cf34738d 100644
--- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java
+++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/style/expressions/ExpressionTest.java
@@ -365,14 +365,14 @@ public class ExpressionTest {
@Test
public void testAt() throws Exception {
- Object[] expected = new Object[] {"at", 3, new Object[] {"one", "two"}};
+ Object[] expected = new Object[] {"at", 3, new Object[] {"literal", new Object[] {"one", "two"}}};
Object[] actual = at(literal(3), literal(new Object[] {"one", "two"})).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@Test
public void testAtLiteral() throws Exception {
- Object[] expected = new Object[] {"at", 3, new Object[] {"one", "two"}};
+ Object[] expected = new Object[] {"at", 3, new Object[] {"literal", new Object[] {"one", "two"}}};
Object[] actual = at(3, literal(new Object[] {"one", "two"})).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@@ -955,12 +955,13 @@ public class ExpressionTest {
@Test
public void testLinear() throws Exception {
- Object[] stopZero = new Object[] {0, 1};
- Object[] stopOne = new Object[] {1, 2};
- Object[] stopTwo = new Object[] {2, 3};
- Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12, stopZero, stopOne, stopTwo};
- Object[] actual = interpolate(linear(), literal(12),
- literal(stopZero), literal(stopOne), literal(stopTwo)).toArray();
+ Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12, 0, 1, 1, 2, 2, 3};
+ Object[] actual = interpolate(
+ linear(), literal(12),
+ literal(0), literal(1),
+ literal(1), literal(2),
+ literal(2), literal(3))
+ .toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
@@ -1064,4 +1065,20 @@ public class ExpressionTest {
get(literal("x")), literal(0), literal(100), literal(100), literal(200)).toString();
assertEquals("toString should match", expected, actual);
}
+
+ @Test
+ public void testLiteralArray() throws Exception {
+ Object[] array = new Object[] {1, "text"};
+ Object[] expected = new Object[] {"literal", array};
+ Object[] actual = literal(array).toArray();
+ assertTrue("expression should match", Arrays.deepEquals(expected, actual));
+ }
+
+ @Test
+ public void testLiteralArrayString() throws Exception {
+ Object[] array = new Object[] {1, "text"};
+ String expected = "[\"literal\"], [1, \"text\"]]";
+ String actual = literal(array).toString();
+ assertEquals("literal array should match", expected, actual);
+ }
} \ No newline at end of file