Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ksclarke committed Sep 2, 2024
1 parent f42e9ed commit a7ba8ad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import static info.freelibrary.util.Constants.SINGLE_INSTANCE;

import java.io.Serializable;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -12,12 +13,11 @@
import java.util.NoSuchElementException;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import info.freelibrary.util.Logger;
import info.freelibrary.util.LoggerFactory;
import info.freelibrary.util.warnings.PMD;
import info.freelibrary.util.warnings.Sonar;

import info.freelibrary.iiif.presentation.v3.utils.MessageCodes;

Expand All @@ -36,7 +36,7 @@ public class ContextList extends ArrayList<URI> implements List<URI> {
private static final long serialVersionUID = -877433424933939199L;

/** A simple sorter that only moves the required context to the first slot. */
private final Comparator<URI> myComparator;
private final ContextListComparator myComparator;

/**
* Creates a new list of contexts. It includes the IIIF Presentation context by default.
Expand Down Expand Up @@ -136,7 +136,7 @@ public boolean addAll(final int aIndex, final Collection<? extends URI> aUriColl
*
* @param aURI A context URI to add at the beginning of the list
*/
@SuppressWarnings(PMD.MISSING_OVERRIDE)
@SuppressWarnings({ PMD.MISSING_OVERRIDE, Sonar.OVERRIDE_REQUIRED })
public void addFirst(final URI aURI) {
if (!PRESENTATION_CONTEXT_URI.equals(aURI)) {
super.add(1, aURI);
Expand All @@ -149,7 +149,7 @@ public void addFirst(final URI aURI) {
*
* @param aURI A context URI to add at the end of the list
*/
@SuppressWarnings(PMD.MISSING_OVERRIDE)
@SuppressWarnings({ PMD.MISSING_OVERRIDE, Sonar.OVERRIDE_REQUIRED })
public void addLast(final URI aURI) {
if (!PRESENTATION_CONTEXT_URI.equals(aURI)) {
super.add(aURI); // Adding, by default, adds as the last item
Expand Down Expand Up @@ -227,10 +227,8 @@ public boolean remove(final Object aObj) {
@Override
public boolean removeAll(final Collection<?> aCollection) {
if (aCollection.contains(PRESENTATION_CONTEXT_URI)) {
final Stream<?> stream = aCollection.stream().filter(uri -> !PRESENTATION_CONTEXT_URI.equals(uri));
final List<?> removables = stream.collect(Collectors.toList());

return super.removeAll(removables);
final List<?> list = aCollection.stream().filter(uri -> !PRESENTATION_CONTEXT_URI.equals(uri)).toList();
return super.removeAll(list);
}

return super.removeAll(aCollection);
Expand All @@ -243,7 +241,7 @@ public boolean removeAll(final Collection<?> aCollection) {
* @param aFilter A filter to use to remove URIs from the list
* @return True if the URIs were removed
*/
@SuppressWarnings(PMD.MISSING_OVERRIDE)
@SuppressWarnings({ PMD.MISSING_OVERRIDE, Sonar.OVERRIDE_REQUIRED })
public boolean removeIf(final Predicate<? super URI> aFilter) {
final boolean result = super.removeIf(aFilter);

Expand All @@ -262,7 +260,7 @@ public boolean removeIf(final Predicate<? super URI> aFilter) {
* @return The context URI that was removed
* @throws NoSuchElementException If the list only has the required IIIF Presentation context
*/
@SuppressWarnings(PMD.MISSING_OVERRIDE)
@SuppressWarnings({ PMD.MISSING_OVERRIDE, Sonar.OVERRIDE_REQUIRED })
public URI removeLast() {
if (size() == SINGLE_INSTANCE) {
throw new NoSuchElementException(LOGGER.getMessage(MessageCodes.JPA_039, PRESENTATION_CONTEXT_URI));
Expand Down Expand Up @@ -345,7 +343,10 @@ public void sort(final Comparator<? super URI> aComparator) {
* Cf. https://iiif.io/api/presentation/3.0/#46-linked-data-context-and-extensions
* </p>
*/
private static final class ContextListComparator implements Comparator<URI> {
private static final class ContextListComparator implements Comparator<URI>, Serializable {

/** The {@code serialVersionUID} for the {@code ContextListComparator}. */
private static final long serialVersionUID = 5516185678973318858L;

@Override
public int compare(final URI aFirstURI, final URI aSecondURI) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.URI;
import java.util.List;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -41,22 +40,23 @@ protected ContextListDeserializer(final Class<?> aClass) {
}

@Override
public List<URI> deserialize(final JsonParser aParser, final DeserializationContext aContext)
throws IOException, JacksonException {
public List<URI> deserialize(final JsonParser aParser, final DeserializationContext aContext) throws IOException {
final JsonNode currentNode = aParser.getCodec().readTree(aParser);
final ContextList contexts = new ContextList();

if (currentNode.isArray()) {
currentNode.forEach(uri -> {
if (!PRESENTATION_CONTEXT_URI.toString().equals(uri)) {
contexts.add(URI.create(uri.textValue()));
currentNode.forEach(context -> {
final URI uri = URI.create(context.textValue());

if (!PRESENTATION_CONTEXT_URI.equals(uri)) {
contexts.add(uri);
}
});
} else {
final String uri = currentNode.textValue();
final URI uri = URI.create(currentNode.textValue());

if (!PRESENTATION_CONTEXT_URI.toString().equals(uri)) {
contexts.add(URI.create(uri));
if (!PRESENTATION_CONTEXT_URI.equals(uri)) {
contexts.add(uri);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ public void serialize(final ContextList aContextList, final JsonGenerator aJsonG
aJsonGenerator.writeString(aContextList.get(0).toString());
} else {
aJsonGenerator.writeStartArray();

aContextList.stream().map(URI::toString).forEach((ThrowingConsumer<String>) uri -> {
aJsonGenerator.writeString(uri);
});

aContextList.stream().map(URI::toString).forEach((ThrowingConsumer<String>) aJsonGenerator::writeString);
aJsonGenerator.writeEndArray();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ public final void testRemoveAllCollectionOfQ() {
*/
@Test
public final void testRemoveIfPredicateOfQsuperURI() {
// fail("Not yet implemented");
final ContextList contexts = new ContextList(myContexts);
final URI kept = contexts.get(2);

contexts.removeIf(uri -> !uri.equals(kept));
assertEquals(2, contexts.size());
assertTrue(contexts.contains(kept));
}

/**
Expand Down Expand Up @@ -242,7 +247,13 @@ public final void testRemoveObject() {
*/
@Test
public final void testReplaceAllUnaryOperatorOfURI() {
// fail("Not yet implemented");
final ContextList contexts = new ContextList(myContexts);
final URI uri = contexts.get(10);
final UnaryOperator<URI> uOp = u -> URI.create(u.toString().replace(uri.toString(), getURL()));

assertTrue(contexts.contains(uri));
contexts.replaceAll(uOp);
assertFalse(contexts.contains(uri));
}

/**
Expand Down

0 comments on commit a7ba8ad

Please sign in to comment.