diff --git a/src/main/java/gov/usgs/earthquake/nshmp/data/Sequences.java b/src/main/java/gov/usgs/earthquake/nshmp/data/Sequences.java index d17c131ff8920473288be8d729bc7573f03805e1..33a78723cacc1a930504186d6f3bd0033ae84ae5 100644 --- a/src/main/java/gov/usgs/earthquake/nshmp/data/Sequences.java +++ b/src/main/java/gov/usgs/earthquake/nshmp/data/Sequences.java @@ -5,6 +5,8 @@ import static com.google.common.base.Preconditions.checkState; import static gov.usgs.earthquake.nshmp.data.DoubleData.areMonotonic; import java.util.Arrays; +import java.util.Collection; +import java.util.function.Predicate; /** * Static methods we don't want in public interfaces. @@ -46,4 +48,33 @@ class Sequences { return mutable ? new MutableArrayXySequence(xs, ys) : new ArrayXySequence(xs, ys); } + /** + * Returns {@code true} if all {@code XySequence} in {@code Collection} have + * the same x-values. Returns {@code true} if {@code Collection} size is 1. + * + * @throws IllegalStateException if {@code Collection<XySequence>} is empty + * + * @param xySequences {@code Collection<XySequence>} to be tested for + * similarity + * + * @return {@code true} if all {@code XySequence}s in specified collection + * share the same x-values + */ + static boolean areSimilar(Collection<XySequence> xySequences) { + checkState(!xySequences.isEmpty()); + if (xySequences.size() == 1) { + return true; + } + // Safe covariant cast + ArrayXySequence firstXy = (ArrayXySequence) xySequences.iterator().next(); + Predicate<XySequence> firstXsPredicate = other -> { + return Arrays.equals( + firstXy.xs, + ((ArrayXySequence) other).xs); + }; + return xySequences.stream() + .skip(1) + .allMatch(firstXsPredicate); + } + } diff --git a/src/test/java/gov/usgs/earthquake/nshmp/data/SequencesTests.java b/src/test/java/gov/usgs/earthquake/nshmp/data/SequencesTests.java new file mode 100644 index 0000000000000000000000000000000000000000..53be21dafd087505aaef8d3961699f6be4201fb3 --- /dev/null +++ b/src/test/java/gov/usgs/earthquake/nshmp/data/SequencesTests.java @@ -0,0 +1,70 @@ +package gov.usgs.earthquake.nshmp.data; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.google.common.collect.ImmutableList; + +class SequencesTests { + + private static double[] xs1 = new double[] { 0, 1, 2, 3 }; + private static double[] xs2 = new double[] { 1, 2, 3, 4 }; + private static double[] xs3 = new double[] { 6, 7, 8 }; + + private static double[] ys1 = new double[] { 10, 9, 8, 7 }; + private static double[] ys2 = new double[] { 5, 4, 3, 2 }; + private static double[] ys3 = new double[] { 10, 9, 8 }; + + private static XySequence xyBase = XySequence.create(xs1, ys1); + private static XySequence xySameXs = XySequence.create(xs1, ys2); + private static XySequence xyDifferentXs = XySequence.create(xs2, ys2); + private static XySequence xyShorter = XySequence.create(xs3, ys3); + + private static List<XySequence> emptyCollection = new ImmutableList.Builder<XySequence>().build(); + private static List<XySequence> singleSequences = + new ImmutableList.Builder<XySequence>() + .add(xyBase) + .build(); + private static List<XySequence> identicalSequences = + new ImmutableList.Builder<XySequence>() + .add(xyBase) + .add(xyBase) + .build(); + private static List<XySequence> sameXs = + new ImmutableList.Builder<XySequence>() + .add(xyBase) + .add(xySameXs) + .build(); + private static List<XySequence> differentXs = + new ImmutableList.Builder<XySequence>() + .add(xyBase) + .add(xySameXs) + .add(xyDifferentXs) + .build(); + private static List<XySequence> withShorterXs = + new ImmutableList.Builder<XySequence>() + .add(xyBase) + .add(xySameXs) + .add(xyDifferentXs) + .add(xyShorter) + .build(); + + @Test + final void areSimilarTests() { + + assertThrows(IllegalStateException.class, () -> { + Sequences.areSimilar(emptyCollection); + }); + assertTrue(Sequences.areSimilar(singleSequences)); + assertTrue(Sequences.areSimilar(identicalSequences)); + assertTrue(Sequences.areSimilar(sameXs)); + assertFalse(Sequences.areSimilar(differentXs)); + assertFalse(Sequences.areSimilar(withShorterXs)); + } + +}