From adccec96c8fa23c9f89b033f35cfc8658a940edf Mon Sep 17 00:00:00 2001 From: Aaron Coburn Date: Tue, 4 Apr 2023 15:04:14 -0400 Subject: [PATCH 1/2] JCL-317: Deprecate use of stream in return types --- .../inrupt/client/solid/SolidContainer.java | 29 ++++++++++++++----- .../inrupt/client/solid/SolidClientTest.java | 6 +++- .../client/solid/SolidRDFSourceTest.java | 8 +++-- .../client/solid/SolidSyncClientTest.java | 6 +++- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/solid/src/main/java/com/inrupt/client/solid/SolidContainer.java b/solid/src/main/java/com/inrupt/client/solid/SolidContainer.java index 845fcf062a5..4cd8ee96deb 100644 --- a/solid/src/main/java/com/inrupt/client/solid/SolidContainer.java +++ b/solid/src/main/java/com/inrupt/client/solid/SolidContainer.java @@ -26,6 +26,9 @@ import com.inrupt.rdf.wrapping.commons.WrapperIRI; import java.net.URI; +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.rdf.api.Dataset; @@ -50,19 +53,31 @@ public SolidContainer(final URI identifier, final Dataset dataset, final Metadat } /** - * Retrieve the resources contained in this SolidContainer. + * Get an immutable collection of resources contained in this SolidContainer. * * @return the contained resources */ - public Stream getContainedResources() { - return new Node(rdf.createIRI(getIdentifier().toString()), getGraph()) - .getContainedResources() - .map(child -> { + public Set getResources() { + final Node node = new Node(rdf.createIRI(getIdentifier().toString()), getGraph()); + try (final Stream stream = node.getResources()) { + return stream.map(child -> { final Metadata.Builder builder = Metadata.newBuilder(); getMetadata().getStorage().ifPresent(builder::storage); child.getTypes().forEach(builder::type); return new SolidResourceReference(URI.create(child.getIRIString()), builder.build()); - }); + }).collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::unmodifiableSet)); + } + } + + /** + * Retrieve the resources contained in this SolidContainer. + * + * @deprecated As of Beta2, replaced by the {@link #getResources()} method. + * @return the contained resources + */ + @Deprecated + public Stream getContainedResources() { + return getResources().stream(); } @SuppressWarnings("java:S2160") // Wrapper equality is correctly delegated to underlying node @@ -73,7 +88,7 @@ static final class Node extends WrapperIRI { super(original, graph); } - Stream getContainedResources() { + Stream getResources() { return objectStream(ldpContains, ValueMappings.as(TypedNode.class)); } diff --git a/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java b/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java index 4ca25b707b2..e1a5dd8ccdc 100644 --- a/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java +++ b/solid/src/test/java/com/inrupt/client/solid/SolidClientTest.java @@ -165,11 +165,15 @@ void testGetContainer() throws IOException, InterruptedException { client.read(uri, SolidContainer.class).thenAccept(container -> { try (final SolidContainer c = container) { assertEquals(uri, c.getIdentifier()); - assertEquals(0, c.getContainedResources().count()); + assertEquals(0, c.getResources().size()); assertEquals(4, c.size()); assertEquals(2, c.stream(Optional.empty(), rdf.createIRI(uri.toString()), rdf.createIRI("https://example.com/song"), null).count()); + @SuppressWarnings("deprecation") + final long count = c.getContainedResources().count(); + assertEquals(0, count); + assertDoesNotThrow(client.update(c).toCompletableFuture()::join); assertDoesNotThrow(client.create(c).toCompletableFuture()::join); assertDoesNotThrow(client.delete(c).toCompletableFuture()::join); diff --git a/solid/src/test/java/com/inrupt/client/solid/SolidRDFSourceTest.java b/solid/src/test/java/com/inrupt/client/solid/SolidRDFSourceTest.java index edbf9a743c0..13ddf2b3c59 100644 --- a/solid/src/test/java/com/inrupt/client/solid/SolidRDFSourceTest.java +++ b/solid/src/test/java/com/inrupt/client/solid/SolidRDFSourceTest.java @@ -136,14 +136,18 @@ void testGetOfSolidContainer() throws IOException, InterruptedException { try (final SolidContainer container = response.body()) { assertEquals(resource, container.getIdentifier()); - assertEquals(3, container.getContainedResources().count()); + assertEquals(3, container.getResources().size()); + + @SuppressWarnings("deprecation") + final long count = container.getContainedResources().count(); + assertEquals(3, count); final Set uris = new HashSet<>(); uris.add(URIBuilder.newBuilder(resource).path("test.txt").build()); uris.add(URIBuilder.newBuilder(resource).path("test2.txt").build()); uris.add(URIBuilder.newBuilder(resource).path("newContainer/").build()); - container.getContainedResources().forEach(child -> + container.getResources().forEach(child -> assertTrue(uris.contains(child.getIdentifier()))); } } diff --git a/solid/src/test/java/com/inrupt/client/solid/SolidSyncClientTest.java b/solid/src/test/java/com/inrupt/client/solid/SolidSyncClientTest.java index 412673674ea..a5e2dfd350a 100644 --- a/solid/src/test/java/com/inrupt/client/solid/SolidSyncClientTest.java +++ b/solid/src/test/java/com/inrupt/client/solid/SolidSyncClientTest.java @@ -138,13 +138,17 @@ void testGetContainer() { try (final SolidContainer container = client.read(uri, SolidContainer.class)) { assertEquals(uri, container.getIdentifier()); - assertEquals(0, container.getContainedResources().count()); + assertEquals(0, container.getResources().size()); assertEquals(4, container.size()); try (final Stream stream = container.stream(Optional.empty(), rdf.createIRI(uri.toString()), rdf.createIRI("https://example.com/song"), null)) { assertEquals(2, stream.count()); } + @SuppressWarnings("deprecation") + final long count = container.getContainedResources().count(); + assertEquals(0, count); + assertDoesNotThrow(() -> client.update(container)); assertDoesNotThrow(() -> client.create(container)); assertDoesNotThrow(() -> client.delete(container)); From 0bb1f366e6ea9a5b027130f9575c07ad94e79508 Mon Sep 17 00:00:00 2001 From: Aaron Coburn Date: Wed, 5 Apr 2023 08:40:59 -0400 Subject: [PATCH 2/2] Migrate examples to use new method --- .../archetype-resources/src/main/java/SolidApp.java | 5 ++--- .../java/com/inrupt/client/examples/cli/SolidApp.java | 9 ++++----- .../com/inrupt/client/examples/webapp/SolidStorage.java | 9 +++++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/archetypes/java/src/main/resources/archetype-resources/src/main/java/SolidApp.java b/archetypes/java/src/main/resources/archetype-resources/src/main/java/SolidApp.java index 7d40d3eb53c..b4baaeea3c9 100644 --- a/archetypes/java/src/main/resources/archetype-resources/src/main/java/SolidApp.java +++ b/archetypes/java/src/main/resources/archetype-resources/src/main/java/SolidApp.java @@ -29,9 +29,8 @@ public List readStorage() { session.getPrincipal().ifPresent(webid -> { try (final var profile = client.read(webid, WebIdProfile.class)) { profile.getStorage().stream().findFirst().ifPresent(storage -> { - try (final var container = client.read(storage, SolidContainer.class); - final var stream = container.getContainedResources()) { - stream.map(Resource::getIdentifier).forEach(resources::add); + try (final var container = client.read(storage, SolidContainer.class)) { + container.getResources().forEach(resource -> resources.add(resource.getIdentifier())); } }); } diff --git a/examples/cli/src/main/java/com/inrupt/client/examples/cli/SolidApp.java b/examples/cli/src/main/java/com/inrupt/client/examples/cli/SolidApp.java index abff6bcae0d..dc4d549f8b1 100644 --- a/examples/cli/src/main/java/com/inrupt/client/examples/cli/SolidApp.java +++ b/examples/cli/src/main/java/com/inrupt/client/examples/cli/SolidApp.java @@ -86,13 +86,12 @@ public int run(final String... args) { profile.getStorage().stream().findFirst().ifPresent(storage -> { printWriter.format("Storage %s ", storage); printWriter.println(); - try (final var container = client.read(storage, SolidContainer.class); - final var stream = container.getContainedResources()) { - printWriter.format("Total number of contained resources are: %s ", - container.getContainedResources().count()); + try (final var container = client.read(storage, SolidContainer.class)) { + final var resources = container.getResources(); + printWriter.format("Total number of contained resources is: %s ", resources.size()); printWriter.println(); - stream.filter(r -> filterResource(r, cmd)).forEach(r -> { + resources.stream().filter(r -> filterResource(r, cmd)).forEach(r -> { printWriter.format("Resource: %s, %s", r.getIdentifier(), principalType(r.getMetadata().getType())); printWriter.println(); diff --git a/examples/webapp/src/main/java/com/inrupt/client/examples/webapp/SolidStorage.java b/examples/webapp/src/main/java/com/inrupt/client/examples/webapp/SolidStorage.java index 4f311b1f3df..ddd73dcafb0 100644 --- a/examples/webapp/src/main/java/com/inrupt/client/examples/webapp/SolidStorage.java +++ b/examples/webapp/src/main/java/com/inrupt/client/examples/webapp/SolidStorage.java @@ -73,10 +73,11 @@ public CompletionStage solid() { return session.read(webid, WebIdProfile.class) .thenCompose(profile -> profile.getStorage().stream().findFirst().map(storage -> session.read(storage, SolidContainer.class).thenApply(container -> { - try (container; final var stream = container.getContainedResources()) { - final var resources = stream.collect(Collectors.groupingBy(c -> - getPrincipalType(c.getMetadata().getType()), Collectors.mapping(c -> - c.getIdentifier().toString(), Collectors.toList()))); + try (container) { + final var resources = container.getResources().stream() + .collect(Collectors.groupingBy(c -> getPrincipalType(c.getMetadata().getType()), + Collectors.mapping(c -> c.getIdentifier().toString(), + Collectors.toList()))); return Templates.profile(profile, resources.get(LDP.BasicContainer), resources.get(LDP.RDFSource)); }