Skip to content

Commit

Permalink
feat (core): Outline Thing.Builder2 with add() & addOrdered() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Sep 29, 2024
1 parent c55a197 commit b4bbc56
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
37 changes: 37 additions & 0 deletions java/dev/enola/thing/PredicatesObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ default boolean isIterable(String predicateIRI) {
return value instanceof Iterable;
}

default boolean isOrdered(String predicateIRI) {
var value = get(predicateIRI);
return value instanceof List;
}

default boolean isStruct(String predicateIRI) {
var value = get(predicateIRI);
return value instanceof PredicatesObjects;
Expand Down Expand Up @@ -182,4 +187,36 @@ interface Builder<B extends PredicatesObjects> // skipcq: JAVA-E0169
@Override
B build();
}

// TODO How to best name this, and the equivalent in Thing?
interface Builder2<B extends PredicatesObjects> extends PredicatesObjects.Builder<B> {

/**
* Adds one of possibly several value objects for the given predicate IRI.
*
* <p>This is UNORDERED! Insertion order may NOT be preserved. Duplicates are not allowed
* and cause an error. It is an error if this property has already been set to anything else
* than a {@link Set}.
*/
<@ImmutableTypeParameter T> PredicatesObjects.Builder2<B> add(String predicateIRI, T value);

<@ImmutableTypeParameter T> PredicatesObjects.Builder2<B> add(
String predicateIRI, T value, @Nullable String datatypeIRI);

/**
* Adds one of possibly several value objects for the given predicate IRI - and preserves
* order.
*
* <p>Duplicates ARE allowed. It is an error if this property has already been set to
* anything else than a {@link List}.
*/
<@ImmutableTypeParameter T> PredicatesObjects.Builder2<B> addOrdered(
String predicateIRI, T value);

<@ImmutableTypeParameter T> PredicatesObjects.Builder2<B> addOrdered(
String predicateIRI, T value, @Nullable String datatypeIRI);

@Override
B build();
}
}
22 changes: 21 additions & 1 deletion java/dev/enola/thing/Thing.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
*/
package dev.enola.thing;

import com.google.errorprone.annotations.ImmutableTypeParameter;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import org.jspecify.annotations.Nullable;

/**
* Thing is the central data structure of Enola.
*
Expand All @@ -45,9 +49,25 @@ interface Builder<B extends Thing> extends PredicatesObjects.Builder<B> { // ski

Builder<B> set(String predicateIRI, Object value);

Builder<B> set(String predicateIRI, Object value, String datatypeIRI);
Builder<B> set(String predicateIRI, Object value, @Nullable String datatypeIRI);

@Override
B build();
}

// TODO How to best name this, and the equivalent in PredicatesObjects?
@SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_INTERFACE")
interface Builder2<B extends Thing>
extends Thing.Builder<B>, PredicatesObjects.Builder2<B> { // skipcq: JAVA-E0169

<@ImmutableTypeParameter T> Thing.Builder2<B> add(String predicateIRI, T value);

<@ImmutableTypeParameter T> Thing.Builder2<B> add(
String predicateIRI, T value, @Nullable String datatypeIRI);

<@ImmutableTypeParameter T> Thing.Builder2<B> addOrdered(String predicateIRI, T value);

<@ImmutableTypeParameter T> Thing.Builder2<B> addOrdered(
String predicateIRI, T value, @Nullable String datatypeIRI);
}
}

0 comments on commit b4bbc56

Please sign in to comment.