[android] - implement expression literal on arrays

This commit is contained in:
Tobrun
2018-03-15 11:12:09 +01:00
committed by Tobrun
parent d59646fc1f
commit aba24da180
2 changed files with 73 additions and 9 deletions

View File

@@ -115,6 +115,16 @@ public class Expression {
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
*
@@ -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();
}
}
}

View File

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