1 package jalview.testutils;
4 import java.util.Objects;
5 import static java.util.Objects.requireNonNullElse;
7 import org.hamcrest.Description;
8 import org.hamcrest.TypeSafeMatcher;
10 import jalview.datamodel.Annotation;
12 public class AnnotationsMatcher extends TypeSafeMatcher<Annotation[]>
14 final List<Annotation> annotations;
16 public AnnotationsMatcher(List<Annotation> annotations)
18 this.annotations = annotations;
22 public boolean matchesSafely(Annotation[] items)
24 if (annotations.size() != items.length)
26 for (int i = 0; i < annotations.size(); i++)
28 var actual = items[i];
29 var expected = annotations.get(i);
30 if (!annotationsEqual(actual, expected))
36 static boolean annotationsEqual(Annotation a, Annotation b)
38 if (a == null && b == null)
40 if ((a == null) != (b == null)) // if one is null but the other is not
42 return a.secondaryStructure == b.secondaryStructure && a.value == b.value
43 && Objects.equals(a.colour, b.colour)
45 .equals(requireNonNullElse(a.displayCharacter, ""),
46 requireNonNullElse(b.displayCharacter, ""))
48 .equals(requireNonNullElse(a.description, ""),
49 requireNonNullElse(b.description, ""));
53 public void describeTo(Description description)
55 description.appendText("annotations ").appendValue(annotations);
59 public void describeMismatchSafely(Annotation[] items,
60 Description description)
62 if (annotations.size() != items.length)
64 description.appendText("but had length ").appendValue(items.length);
68 for (int i = 0; i < annotations.size(); i++)
70 var actual = items[i];
71 var expected = annotations.get(i);
72 if (!annotationsEqual(actual, expected))
75 .appendText(first ? "but " : ", ")
76 .appendText("element [" + i + "] was ")
77 .appendValue(items[i]);