Skip to content

Commit

Permalink
clean(core): Make ThingsBuilder implement ThingsRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Sep 24, 2024
1 parent 1e64f96 commit f561034
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
21 changes: 20 additions & 1 deletion java/dev/enola/thing/repo/ThingsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@
*/
package dev.enola.thing.repo;

import dev.enola.thing.KIRI;
import dev.enola.thing.Thing;
import dev.enola.thing.impl.ImmutableThing;
import dev.enola.thing.impl.OnlyIRIThing;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
* Set of {@link Thing.Builder}s.
*
* <p>This is intended to be used "short-lived", e.g., during incremental thing creation.
*
* <p>For memory efficiency, do NOT "keep this around".
*/
// @NotThreadSafe
public class ThingsBuilder {
public class ThingsBuilder implements ThingsRepository {

private final Map<String, Thing.Builder<?>> map = new HashMap<>();

Expand All @@ -35,4 +45,13 @@ public Thing.Builder<?> getBuilder(String iri) {
public Iterable<Thing.Builder<?>> builders() {
return map.values();
}

@Override
public Stream<Thing> getThings(String iri) {
return switch (iri) {
case KIRI.E.LIST_THINGS -> map.values().stream().map(Thing.Builder::build);
case KIRI.E.LIST_IRIS -> map.keySet().stream().map(OnlyIRIThing::new);
default -> Stream.of(getBuilder(iri).build());
};
}
}
4 changes: 4 additions & 0 deletions java/dev/enola/thing/repo/ThingsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

public interface ThingsProvider {

// TODO Rethink this... this doesn't make sense, there should only ever be 1 Thing per IRI?!
// KIRI.E.LIST_THINGS & KIRI.E.LIST_IRIS just needs to return a Thing with 1 list property...
// Switch callers to existing old ThingProvider (NB singular Thing, not ThingsProvider).

// TODO Reactive Stream, or Mutiny Multi, instead of JDK Stream
Stream<Thing> getThings(String iri);
}
2 changes: 2 additions & 0 deletions java/dev/enola/thing/repo/ThingsRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

public interface ThingsRepository extends ThingRepository, ThingsProvider {

// TODO See note in ThingsProvider, and also get rid of replace this with ThingRepository

@Override
default Iterable<Thing> list() {
return getThings(KIRI.E.LIST_THINGS).toList();
Expand Down

0 comments on commit f561034

Please sign in to comment.