anOperator) {
+ super.replaceAll(anOperator);
+
+ // Remove all instances of our default context URI
+ super.removeAll(List.of(PRESENTATION_CONTEXT_URI));
+
+ // Add back a single instance of our default context URI
+ if (!PRESENTATION_CONTEXT_URI.equals(get(0))) {
+ super.add(0, PRESENTATION_CONTEXT_URI);
+ }
+ }
+
+ /**
+ * Retains the contexts in the supplied collection and the default context (if it's also not included in the
+ * supplied collection).
+ *
+ * @param aCollection A collection of URIs to retain, removing the rest
+ * @return Whether the URIs were successfully retained
+ */
+ @Override
+ public boolean retainAll(final Collection> aCollection) {
+ final boolean result = super.retainAll(aCollection);
+
+ // If we haven't added retained the required context, we add it back
+ if (!PRESENTATION_CONTEXT_URI.equals(get(0))) {
+ super.add(0, PRESENTATION_CONTEXT_URI);
+ }
+
+ return result;
+ }
+
+ /**
+ * Sets the supplied context URI at the supplied index position. The default context can not be set with this
+ * method.
+ *
+ * @param aIndex An index position of the URI to set
+ * @param aURI A URI to set at the supplied index position
+ * @return The context URI that used to be at the supplied index position
+ */
+ @Override
+ public URI set(final int aIndex, final URI aURI) {
+ if (PRESENTATION_CONTEXT_URI.equals(aURI)) {
+ if (aIndex != 0) {
+ throw new IndexOutOfBoundsException(LOGGER.getMessage(MessageCodes.JPA_149));
+ }
+
+ return aURI;
+ }
+ if (aIndex != 0) {
+ return super.set(aIndex, aURI);
+ }
+
+ throw new IndexOutOfBoundsException(LOGGER.getMessage(MessageCodes.JPA_151, aURI));
+ }
+
+ /**
+ * This method is not supported because {@code ContextList} has a prescribed sort order that can not be overridden.
+ *
+ * @param aComparator A comparator that could be used to sort the list
+ */
+ @Override
+ public void sort(final Comparator super URI> aComparator) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * A context list comparator that makes sure the required context is always last in the list.
+ *
+ * Cf. https://iiif.io/api/presentation/3.0/#46-linked-data-context-and-extensions
+ *
+ */
+ private static final class ContextListComparator implements Comparator, Serializable {
+
+ /** The {@code serialVersionUID} for the {@code ContextListComparator}. */
+ private static final long serialVersionUID = 5516185678973318858L;
+
+ @Override
+ public int compare(final URI aFirstURI, final URI aSecondURI) {
+ int result = 0;
+
+ if (!PRESENTATION_CONTEXT_URI.equals(aFirstURI) || !PRESENTATION_CONTEXT_URI.equals(aSecondURI)) {
+ if (PRESENTATION_CONTEXT_URI.equals(aFirstURI)) {
+ result = -1;
+ } else if (PRESENTATION_CONTEXT_URI.equals(aSecondURI)) {
+ result = 1;
+ }
+ }
+
+ return result;
+ }
+
+ }
+}
diff --git a/src/main/java/info/freelibrary/iiif/presentation/v3/Manifest.java b/src/main/java/info/freelibrary/iiif/presentation/v3/Manifest.java
index a6b3ec3a..d9a31167 100644
--- a/src/main/java/info/freelibrary/iiif/presentation/v3/Manifest.java
+++ b/src/main/java/info/freelibrary/iiif/presentation/v3/Manifest.java
@@ -1,10 +1,8 @@
package info.freelibrary.iiif.presentation.v3;
-import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@@ -71,6 +69,7 @@ public class Manifest extends NavigableResource implements Resource aCanvasList) {
- getCanvases().addAll(aCanvasList);
- return this;
- }
-
- /**
- * Adds one or more ranges to the manifest.
- *
- * @param aRangeList A list of ranges to add to the manifest
- * @return The manifest
- */
- public Manifest addRanges(final List aRangeList) {
- getRanges().addAll(aRangeList);
- return this;
- }
-
- /**
- * Adds one or more ranges to the manifest.
- *
- * @param aRangeArray An array of ranges to add to the manifest
- * @return The manifest
- */
- public Manifest addRanges(final Range... aRangeArray) {
- Collections.addAll(getRanges(), aRangeArray);
- return this;
+ getContextList(); // Initializes the context list
}
@Override
@@ -165,7 +121,6 @@ public Optional getAccompanyingCanvas() {
*/
@JsonGetter(JsonKeys.ANNOTATIONS)
@SuppressWarnings({ JDK.UNCHECKED })
- // >
public List> getAnnotations() {
if (myAnnotations == null) {
myAnnotations = new ArrayList<>();
@@ -188,17 +143,6 @@ public List