Skip to content

Commit

Permalink
JCL-317: Deprecate use of stream in return types (#400)
Browse files Browse the repository at this point in the history
* JCL-317: Deprecate use of stream in return types

* Migrate examples to use new method
  • Loading branch information
acoburn authored Apr 5, 2023
1 parent 520fb69 commit 6acfe3a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public List<URI> 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()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ public CompletionStage<TemplateInstance> 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));
}
Expand Down
29 changes: 22 additions & 7 deletions solid/src/main/java/com/inrupt/client/solid/SolidContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<SolidResource> getContainedResources() {
return new Node(rdf.createIRI(getIdentifier().toString()), getGraph())
.getContainedResources()
.map(child -> {
public Set<SolidResource> getResources() {
final Node node = new Node(rdf.createIRI(getIdentifier().toString()), getGraph());
try (final Stream<Node.TypedNode> 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<SolidResource> getContainedResources() {
return getResources().stream();
}

@SuppressWarnings("java:S2160") // Wrapper equality is correctly delegated to underlying node
Expand All @@ -73,7 +88,7 @@ static final class Node extends WrapperIRI {
super(original, graph);
}

Stream<TypedNode> getContainedResources() {
Stream<TypedNode> getResources() {
return objectStream(ldpContains, ValueMappings.as(TypedNode.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<URI> 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())));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Quad> 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));
Expand Down

0 comments on commit 6acfe3a

Please sign in to comment.