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

Commit

Permalink
[android] - implement expression literal on arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Mar 16, 2018
1 parent d59646f commit aba24da
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public static Expression literal(@NonNull Object object) {
return new ExpressionLiteral(object);
}

/**
* 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
*
Expand Down Expand Up @@ -1600,7 +1610,7 @@ public static Expression interpolate(@NonNull Interpolator interpolation,
@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.
*
Expand Down Expand Up @@ -2006,4 +2016,41 @@ private static Expression convert(@NonNull JsonPrimitive jsonPrimitive) {
}
}
}

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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,14 @@ public void testHeatmapDensity() throws Exception {

@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));
}
Expand Down Expand Up @@ -955,12 +955,13 @@ public void testStepExpressionLiteral() throws Exception {

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

Expand Down Expand Up @@ -1064,4 +1065,20 @@ public void testExpressionExponentialToString() throws Exception {
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);
}
}

0 comments on commit aba24da

Please sign in to comment.