Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Literal array conversion of primitive arrays #11500

Merged
merged 2 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public static Expression literal(boolean bool) {
* @return the expression
*/
public static Expression literal(@NonNull Object object) {
if (object.getClass().isArray()) {
return literal(ExpressionArray.toObjectArray(object));
}
return new ExpressionLiteral(object);
}

Expand Down Expand Up @@ -2033,6 +2036,11 @@ public Object[] toArray() {
};
}

/**
* Convert the expression array to a string representation.
*
* @return the string representation of the expression array
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("[\"literal\"], [");
Expand All @@ -2053,4 +2061,20 @@ public String toString() {
return builder.toString();
}
}

/**
* Converts an object that is a primitive array to an Object[]
*
* @param object the object to convert to an object array
* @return the converted object array
*/
static Object[] toObjectArray(Object object) {
// object is a primitive array
int len = java.lang.reflect.Array.getLength(object);
Object[] objects = new Object[len];
for (int i = 0; i < len; i++) {
objects[i] = java.lang.reflect.Array.get(object, i);
}
return objects;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1081,4 +1081,12 @@ public void testLiteralArrayString() throws Exception {
String actual = literal(array).toString();
assertEquals("literal array should match", expected, actual);
}

@Test
public void testLiteralPrimitiveArrayConversion() throws Exception {
float[] array = new float[] {0.2f, 0.5f};
Object[] expected = new Object[] {"literal", new Object[] {0.2f, 0.5f}};
Object[] actual = literal(array).toArray();
assertEquals("primitive array should be convered", expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@
import static com.mapbox.mapboxsdk.style.expressions.Expression.pi;
import static com.mapbox.mapboxsdk.style.expressions.Expression.product;
import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba;
import static com.mapbox.mapboxsdk.style.expressions.Expression.step;
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
import static com.mapbox.mapboxsdk.style.expressions.Expression.string;
import static com.mapbox.mapboxsdk.style.expressions.Expression.upcase;
import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom;
import static com.mapbox.mapboxsdk.style.layers.Property.ICON_ANCHOR_BOTTOM;
import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
Expand Down Expand Up @@ -265,7 +268,10 @@ public void onDataLoaded(@NonNull FeatureCollection featureCollection) {
iconAllowOverlap(false),
iconSize(iconSizeExpression),
iconAnchor(ICON_ANCHOR_BOTTOM),
iconOffset(new Float[] {0.0f, -5.0f}),
iconOffset(step(zoom(), literal(new float[] {0f, 0f}),
literal(1), literal(new Float[] {0f, 0f}),
literal(10), literal(new Float[] {0f, -35f})
)),

// text field configuration
textField(textFieldExpression),
Expand Down