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
This commit is contained in:
Anand Thakker
2018-04-11 12:03:56 -04:00
committed by GitHub
parent 797486bc5f
commit f0f67b4788
11 changed files with 317 additions and 33 deletions

View File

@@ -81,7 +81,6 @@ import java.util.List;
* )
* }
* </pre>
*
*/
public class Expression {
@@ -1061,6 +1060,7 @@ public class Expression {
* );
* }
* </pre>
*
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-properties">Style specification</a>
*/
@@ -1081,6 +1081,7 @@ public class Expression {
* );
* }
* </pre>
*
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-geometry-types">Style specification</a>
*/
@@ -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.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(round(pi()))
* );
* }
* </pre>
*
* @param expression number expression to round
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-round">Style specification</a>
*/
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.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(round(3.14159265359f))
* );
* }
* </pre>
*
* @param number number to round
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-round">Style specification</a>
*/
public static Expression round(Number number) {
return round(literal(number));
}
/**
* Returns the absolute value of the input.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(abs(subtract(pi())))
* );
* }
* </pre>
*
* @param expression number expression to get absolute value from
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-abs">Style specification</a>
*/
public static Expression abs(Expression expression) {
return new Expression("abs", expression);
}
/**
* Returns the absolute value of the input.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(abs(-3.14159265359f))
* );
* }
* </pre>
*
* @param number number to get absolute value from
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-abs">Style specification</a>
*/
public static Expression abs(Number number) {
return abs(literal(number));
}
/**
* Returns the smallest integer that is greater than or equal to the input.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(ceil(pi()))
* );
* }
* </pre>
*
* @param expression number expression to get value from
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-abs">Style specification</a>
*/
public static Expression ceil(Expression expression) {
return new Expression("ceil", expression);
}
/**
* Returns the smallest integer that is greater than or equal to the input.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(ceil(3.14159265359))
* );
* }
* </pre>
* @param number number to get value from
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-abs">Style specification</a>
*/
public static Expression ceil(Number number) {
return ceil(literal(number));
}
/**
* Returns the largest integer that is less than or equal to the input.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(floor(pi()))
* );
* }
* </pre>
*
* @param expression number expression to get value from
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-abs">Style specification</a>
*/
public static Expression floor(Expression expression) {
return new Expression("floor", expression);
}
/**
* Returns the largest integer that is less than or equal to the input.
* <p>
* Example usage:
* </p>
* <pre>
* {@code
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleRadius(floor(pi()))
* );
* }
* </pre>
*
* @param number number to get value from
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-abs">Style specification</a>
*/
public static Expression floor(Number number) {
return floor(literal(number));
}
/**
* Returns the input string converted to uppercase.
* <p>
@@ -3128,6 +3308,7 @@ public class Expression {
* );
* }
* </pre>
*
* @param expression base number expression
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-interpolate">Style specification</a>

View File

@@ -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

View File

@@ -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));
}
}