Cleanup
diff --git a/lottie/src/main/java/com/airbnb/lottie/parser/GradientColorParser.java b/lottie/src/main/java/com/airbnb/lottie/parser/GradientColorParser.java
index ad4c582..50a52e3 100644
--- a/lottie/src/main/java/com/airbnb/lottie/parser/GradientColorParser.java
+++ b/lottie/src/main/java/com/airbnb/lottie/parser/GradientColorParser.java
@@ -234,34 +234,22 @@
int aIndex = 0;
int bIndex = 0;
- int mergedIndex = 0;
int numDuplicates = 0;
// This will be the merged list but may be longer than what is needed if there are duplicates.
// If there are, the 0 elements at the end need to be truncated.
float[] mergedNotTruncated = new float[arrayA.length + arrayB.length];
for (int i = 0; i < mergedNotTruncated.length; i++) {
- final float a;
- if (aIndex < arrayA.length) {
- a = arrayA[aIndex];
- } else {
- a = Float.NaN;
- }
-
- final float b;
- if (bIndex < arrayB.length) {
- b = arrayB[bIndex];
- } else {
- b = Float.NaN;
- }
+ final float a = aIndex < arrayA.length ? arrayA[aIndex] : Float.NaN;
+ final float b = bIndex < arrayB.length ? arrayB[bIndex] : Float.NaN;
if (Float.isNaN(b) || a < b) {
- mergedNotTruncated[mergedIndex++] = a;
+ mergedNotTruncated[i] = a;
aIndex++;
} else if (Float.isNaN(a) || b < a) {
- mergedNotTruncated[mergedIndex++] = b;
+ mergedNotTruncated[i] = b;
bIndex++;
} else {
- mergedNotTruncated[mergedIndex++] = a;
+ mergedNotTruncated[i] = a;
aIndex++;
bIndex++;
numDuplicates++;
@@ -273,8 +261,6 @@
}
- float[] merged = new float[mergedNotTruncated.length - numDuplicates];
- System.arraycopy(mergedNotTruncated, 0, merged, 0, merged.length);
- return merged;
+ return Arrays.copyOf(mergedNotTruncated, mergedNotTruncated.length - numDuplicates);
}
}
\ No newline at end of file
diff --git a/lottie/src/test/java/com/airbnb/lottie/parser/GradientColorParserTest.java b/lottie/src/test/java/com/airbnb/lottie/parser/GradientColorParserTest.java
index 5d88bbe..79682e1 100644
--- a/lottie/src/test/java/com/airbnb/lottie/parser/GradientColorParserTest.java
+++ b/lottie/src/test/java/com/airbnb/lottie/parser/GradientColorParserTest.java
@@ -1,4 +1,32 @@
-import static org.junit.Assert.*;
+package com.airbnb.lottie.parser;
+
+import static org.junit.Assert.assertArrayEquals;
+
+import org.junit.Test;
+
public class GradientColorParserTest {
-
+
+ @Test public void testNoDistinctShort() {
+ assertMerged(new float[]{1}, new float[]{2}, new float[]{1, 2});
+ }
+
+ @Test public void testNoDistinct() {
+ assertMerged(new float[]{1, 2, 3}, new float[]{4, 5, 6}, new float[]{1, 2, 3, 4, 5, 6});
+ }
+
+ @Test public void testWithDistinct() {
+ assertMerged(new float[]{1, 2, 3, 5}, new float[]{4, 5, 6}, new float[]{1, 2, 3, 4, 5, 6});
+ }
+
+ @Test public void testWithDistinctInterleavingValues() {
+ assertMerged(new float[]{2, 4, 5, 6, 8, 10}, new float[]{1, 3, 4, 5, 7, 9}, new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
+ }
+
+ @Test public void testIdentical() {
+ assertMerged(new float[]{2, 3}, new float[]{2, 3}, new float[]{2, 3});
+ }
+
+ private void assertMerged(float[] arrayA, float[] arrayB, float[] merged) {
+ assertArrayEquals(merged, GradientColorParser.mergeUniqueElements(arrayA, arrayB), 0f);
+ }
}
\ No newline at end of file