From f0f67b4788f967ee34f307312db302cecb68dd0b Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Wed, 11 Apr 2018 12:03:56 -0400 Subject: Add abs, round, floor, ceil operators (#11653) * Add abs, round, floor, ceil operators Port of https://github.com/mapbox/mapbox-gl-js/pull/6496 * [ios, macos] Simplified abs, ceiling, floor expressions * [ios, macos] Added rounding expression function * [android] - binding integration for round, ceil, floor and abs expressions * Update mapbox-gl-js to include non-integer rounding test * Drop extra braces * mapbox-gl-js -> master * Update style-spec docs -> PropertyFactory.java --- .../mapboxsdk/style/expressions/Expression.java | 183 ++++++++++++++++++++- .../mapboxsdk/style/layers/PropertyFactory.java | 8 +- .../style/expressions/ExpressionTest.java | 60 +++++++ 3 files changed, 246 insertions(+), 5 deletions(-) (limited to 'platform/android/MapboxGLAndroidSDK/src') 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 05c67018a4..fa4a802e13 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 @@ -81,7 +81,6 @@ import java.util.List; * ) * } * - * */ public class Expression { @@ -1061,6 +1060,7 @@ public class Expression { * ); * } * + * * @return expression * @see Style specification */ @@ -1081,6 +1081,7 @@ public class Expression { * ); * } * + * * @return expression * @see Style specification */ @@ -2294,6 +2295,185 @@ public class Expression { return max(numberExpression); } + /** + * Rounds the input to the nearest integer. + * Halfway values are rounded away from zero. + * For example `[\"round\", -1.5]` evaluates to -2. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(round(pi()))
+   * );
+   * }
+   * 
+ * + * @param expression number expression to round + * @return expression + * @see Style specification + */ + public static Expression round(Expression expression) { + return new Expression("round", expression); + } + + /** + * Rounds the input to the nearest integer. + * Halfway values are rounded away from zero. + * For example `[\"round\", -1.5]` evaluates to -2. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(round(3.14159265359f))
+   * );
+   * }
+   * 
+ * + * @param number number to round + * @return expression + * @see Style specification + */ + public static Expression round(Number number) { + return round(literal(number)); + } + + /** + * Returns the absolute value of the input. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(abs(subtract(pi())))
+   * );
+   * }
+   * 
+ * + * @param expression number expression to get absolute value from + * @return expression + * @see Style specification + */ + public static Expression abs(Expression expression) { + return new Expression("abs", expression); + } + + /** + * Returns the absolute value of the input. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(abs(-3.14159265359f))
+   * );
+   * }
+   * 
+ * + * @param number number to get absolute value from + * @return expression + * @see Style specification + */ + public static Expression abs(Number number) { + return abs(literal(number)); + } + + /** + * Returns the smallest integer that is greater than or equal to the input. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(ceil(pi()))
+   * );
+   * }
+   * 
+ * + * @param expression number expression to get value from + * @return expression + * @see Style specification + */ + public static Expression ceil(Expression expression) { + return new Expression("ceil", expression); + } + + /** + * Returns the smallest integer that is greater than or equal to the input. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(ceil(3.14159265359))
+   * );
+   * }
+   * 
+ * @param number number to get value from + * @return expression + * @see Style specification + */ + public static Expression ceil(Number number) { + return ceil(literal(number)); + } + + /** + * Returns the largest integer that is less than or equal to the input. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(floor(pi()))
+   * );
+   * }
+   * 
+ * + * @param expression number expression to get value from + * @return expression + * @see Style specification + */ + public static Expression floor(Expression expression) { + return new Expression("floor", expression); + } + + /** + * Returns the largest integer that is less than or equal to the input. + *

+ * Example usage: + *

+ *
+   * {@code
+   * CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
+   * circleLayer.setProperties(
+   *     circleRadius(floor(pi()))
+   * );
+   * }
+   * 
+ * + * @param number number to get value from + * @return expression + * @see Style specification + */ + public static Expression floor(Number number) { + return floor(literal(number)); + } + /** * Returns the input string converted to uppercase. *

@@ -3128,6 +3308,7 @@ public class Expression { * ); * } * + * * @param expression base number expression * @return expression * @see Style specification diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java index 66d78a40e5..4289deeda3 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java @@ -1856,7 +1856,7 @@ public class PropertyFactory { } /** - * Name of image in sprite to use for drawing an image background. Within literal values and zoom functions, property names enclosed in curly brackets (e.g. `{token}`) are replaced with the value of the named property. Expressions and property functions do not support this syntax; for equivalent functionality in expressions, use the [`concat`](#expressions-concat) and [`get`](#expressions-get) operators. + * Name of image in sprite to use for drawing an image background. * * @param value a String value * @return property wrapper around String @@ -1866,7 +1866,7 @@ public class PropertyFactory { } /** - * Name of image in sprite to use for drawing an image background. Within literal values and zoom functions, property names enclosed in curly brackets (e.g. `{token}`) are replaced with the value of the named property. Expressions and property functions do not support this syntax; for equivalent functionality in expressions, use the [`concat`](#expressions-concat) and [`get`](#expressions-get) operators. + * Name of image in sprite to use for drawing an image background. * * @param value a String value * @return property wrapper around String @@ -2036,7 +2036,7 @@ public class PropertyFactory { } /** - * Value to use for a text label. Within literal values and zoom functions, property names enclosed in curly brackets (e.g. `{token}`) are replaced with the value of the named property. Expressions and property functions do not support this syntax; for equivalent functionality in expressions, use the [`concat`](#expressions-concat) and [`get`](#expressions-get) operators. + * Value to use for a text label. * * @param value a String value * @return property wrapper around String @@ -2046,7 +2046,7 @@ public class PropertyFactory { } /** - * Value to use for a text label. Within literal values and zoom functions, property names enclosed in curly brackets (e.g. `{token}`) are replaced with the value of the named property. Expressions and property functions do not support this syntax; for equivalent functionality in expressions, use the [`concat`](#expressions-concat) and [`get`](#expressions-get) operators. + * Value to use for a text label. * * @param value a String value * @return property wrapper around String 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 5694242a0a..45833e8556 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 @@ -6,6 +6,7 @@ import org.junit.Test; import java.util.Arrays; +import static com.mapbox.mapboxsdk.style.expressions.Expression.abs; import static com.mapbox.mapboxsdk.style.expressions.Expression.acos; import static com.mapbox.mapboxsdk.style.expressions.Expression.all; import static com.mapbox.mapboxsdk.style.expressions.Expression.any; @@ -14,6 +15,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.asin; import static com.mapbox.mapboxsdk.style.expressions.Expression.at; import static com.mapbox.mapboxsdk.style.expressions.Expression.atan; import static com.mapbox.mapboxsdk.style.expressions.Expression.bool; +import static com.mapbox.mapboxsdk.style.expressions.Expression.ceil; import static com.mapbox.mapboxsdk.style.expressions.Expression.coalesce; import static com.mapbox.mapboxsdk.style.expressions.Expression.color; import static com.mapbox.mapboxsdk.style.expressions.Expression.concat; @@ -24,6 +26,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.downcase; import static com.mapbox.mapboxsdk.style.expressions.Expression.e; import static com.mapbox.mapboxsdk.style.expressions.Expression.eq; import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential; +import static com.mapbox.mapboxsdk.style.expressions.Expression.floor; import static com.mapbox.mapboxsdk.style.expressions.Expression.geometryType; import static com.mapbox.mapboxsdk.style.expressions.Expression.get; import static com.mapbox.mapboxsdk.style.expressions.Expression.gt; @@ -56,6 +59,7 @@ import static com.mapbox.mapboxsdk.style.expressions.Expression.product; import static com.mapbox.mapboxsdk.style.expressions.Expression.properties; import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba; +import static com.mapbox.mapboxsdk.style.expressions.Expression.round; import static com.mapbox.mapboxsdk.style.expressions.Expression.sin; import static com.mapbox.mapboxsdk.style.expressions.Expression.sqrt; import static com.mapbox.mapboxsdk.style.expressions.Expression.step; @@ -1105,4 +1109,60 @@ public class ExpressionTest { ); expression.toArray(); } + + @Test + public void testRound() { + Object[] expected = new Object[] {"round", 2.2f}; + Object[] actual = round(2.2f).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } + + @Test + public void testRoundLiteral() { + Object[] expected = new Object[] {"round", 2.2f}; + Object[] actual = round(literal(2.2f)).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } + + @Test + public void testAbs() { + Object[] expected = new Object[] {"abs", -2.2f}; + Object[] actual = abs(-2.2f).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } + + @Test + public void testAbsLiteral() { + Object[] expected = new Object[] {"abs", -2.2f}; + Object[] actual = abs(literal(-2.2f)).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } + + @Test + public void testCeil() { + Object[] expected = new Object[] {"ceil", 2.2f}; + Object[] actual = ceil(2.2f).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } + + @Test + public void testCeilLiteral() { + Object[] expected = new Object[] {"ceil", 2.2f}; + Object[] actual = ceil(literal(2.2f)).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } + + @Test + public void testFloor() { + Object[] expected = new Object[] {"floor", 2.2f}; + Object[] actual = floor(2.2f).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } + + @Test + public void testFloorLiteral() { + Object[] expected = new Object[] {"floor", 2.2f}; + Object[] actual = floor(literal(2.2f)).toArray(); + assertTrue("expression should match", Arrays.deepEquals(expected, actual)); + } } \ No newline at end of file -- cgit v1.2.1