Skip to content

Commit

Permalink
feat (core): Java ☕ static Thing API
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Jun 22, 2024
1 parent 98907e4 commit 64abfdb
Show file tree
Hide file tree
Showing 24 changed files with 634 additions and 76 deletions.
2 changes: 0 additions & 2 deletions .bazelproject
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ derive_targets_from_directories: true
workspace_type: java

additional_languages:
go
kotlin
typescript
4 changes: 2 additions & 2 deletions java/dev/enola/common/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package dev.enola.common;

public interface Builder<T> {
public interface Builder<B> {

T build();
B build();
}
3 changes: 3 additions & 0 deletions java/dev/enola/common/convert/ObjectToStringBiConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
*/
package dev.enola.common.convert;

import com.google.errorprone.annotations.Immutable;

import java.io.IOException;
import java.util.Optional;

/** Converts objects of type T to & from String, if it can. */
@Immutable
public interface ObjectToStringBiConverter<T>
extends BiConverter<T, String>, ConverterIntoAppendable<T>, ObjectClassConverter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

public class ObjectToStringBiConverterWithFormat implements ObjectToStringBiConverter<Object> {

@SuppressWarnings("Immutable")
private final Format format;

public ObjectToStringBiConverterWithFormat(Format format) {
Expand Down
9 changes: 9 additions & 0 deletions java/dev/enola/common/convert/ObjectToStringBiConverters.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package dev.enola.common.convert;

import java.net.URISyntaxException;
import java.time.Instant;
import java.time.LocalDate;

public final class ObjectToStringBiConverters {
Expand All @@ -29,6 +30,10 @@ public final class ObjectToStringBiConverters {
new ObjectToStringWithToStringBiConverter<>(
Boolean.class, input -> Boolean.valueOf(input));

public static final ObjectToStringBiConverter<Integer> INT =
new ObjectToStringWithToStringBiConverter<>(
Integer.class, input -> Integer.valueOf(input));

public static final ObjectToStringBiConverter<java.net.URI> URI =
new ObjectToStringWithToStringBiConverter<>(
java.net.URI.class,
Expand All @@ -44,5 +49,9 @@ public final class ObjectToStringBiConverters {
new ObjectToStringWithToStringBiConverter<>(
LocalDate.class, input -> LocalDate.parse(input));

public static final ObjectToStringBiConverter<Instant> INSTANT =
new ObjectToStringWithToStringBiConverter<>(
Instant.class, input -> Instant.parse(input));

private ObjectToStringBiConverters() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
public class ObjectToStringWithToStringBiConverter<T> implements ObjectToStringBiConverter<T> {

private final Class<T> from;

@SuppressWarnings("Immutable")
private final Function<String, T> converter;

public ObjectToStringWithToStringBiConverter(Class<T> clazz, Function<String, T> function) {
Expand Down
6 changes: 5 additions & 1 deletion java/dev/enola/datatype/Datatype.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
*/
package dev.enola.datatype;

import com.google.errorprone.annotations.Immutable;

import dev.enola.common.convert.ObjectToStringBiConverter;

import java.lang.reflect.Type;
import java.util.Optional;
import java.util.regex.Pattern;

public interface Datatype<T> /* TODO extends Thing */ {
@Immutable
public interface Datatype<T> {
// NB: NOT extends Thing; there's a DatatypeThing for that!

/** IRI of this datatype. Always present, never null or empty. */
String iri();
Expand Down
6 changes: 2 additions & 4 deletions java/dev/enola/datatype/Datatypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
// https://docs.enola.dev/models/datatypes and have a working HTTP redirector.
public final class Datatypes {

// TODO Move dev.enola.datatype.Datatypes to dev.enola.model.xsd.Datatypes!

// Nota Bene: There's no NULL here - the absence of a value is not a Datatype!

// TODO MAP, LIST etc. from https://yaml.org/type/
Expand Down Expand Up @@ -56,10 +58,6 @@ public final class Datatypes {
URI.class, // TODO Make this URI.class!
IRI_PATTERN);

// TODO public static final Datatype NUMBER = new NumberDatatype();

// TODO TIMESTAMP

// TODO BINARY ... multibase ... with https://github.com/multiformats/java-multibase, or
// https://github.com/filip26/copper-multibase, for https://github.com/multiformats/multibase.
// Replace use of Base64.getEncoder().encodeToString() in MessageToThingConverter#b64()
Expand Down
27 changes: 27 additions & 0 deletions java/dev/enola/model/w3/rdfs/HasLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.model.w3.rdfs;

import dev.enola.thing.KIRI;
import dev.enola.thing.java.IRI;

public interface HasLabel {

@IRI(KIRI.RDFS.LABEL)
String getLabel();
}
17 changes: 17 additions & 0 deletions java/dev/enola/model/xsd/Datatypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import dev.enola.datatype.Datatype;
import dev.enola.datatype.ImmutableDatatype;

import java.time.Instant;
import java.time.LocalDate;

/** Datatypes for <a href="http://www.w3.org/TR/xmlschema-2/">XML Schema (XSD)</a>. */
Expand All @@ -37,5 +38,21 @@ public final class Datatypes {
// https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s07.html
"(?<year>[0-9]{4})-?(?<month>1[0-2]|0[1-9])-?(?<day>3[01]|0[1-9]|[12][0-9])");

public static final Datatype<Integer> INT =
new ImmutableDatatype<>(
"http://www.w3.org/2001/XMLSchema#int",
ObjectToStringBiConverters.INT,
Integer.class,
// TODO Test coverage for this INT RegExp...
"-?0*(?:214748364[0-7]|21474836[0-3]\\d|2147483[0-5]\\d{2}|214748[0-2]\\d{3}|21474[0-7]\\d{4}|2147[0-3]\\d{5}|214[0-6]\\d{6}|21[0-3]\\d{7}|20\\d{8}|1\\d{9}|[1-9]\\d{1,8}|0)");

public static final Datatype<Instant> DATE_TIME =
new ImmutableDatatype<>(
"http://www.w3.org/2001/XMLSchema#dateTime",
ObjectToStringBiConverters.INSTANT,
Instant.class,
// TODO Test coverage for this INT RegExp...
"(\\d{4})-(\\d{2})-(\\d{2})T([0-9:]+)(Z|([+-])(\\d{2})(:(\\d{2})?)?)?");

private Datatypes() {}
}
12 changes: 11 additions & 1 deletion java/dev/enola/thing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ java_library(
name = "thing_java",
srcs = glob(
["**/*.java"],
exclude = ["**/*Test.java"],
exclude = [
"**/*Test.java",
"**/Test*.java",
],
),
plugins = ["//tools/bazel/java_plugin:autoservice"],
visibility = ["//:__subpackages__"],
Expand All @@ -76,6 +79,7 @@ java_library(
"//java/dev/enola/data",
"//java/dev/enola/datatype",
"@maven//:com_github_java_json_tools_uri_template",
"@maven//:com_github_spotbugs_spotbugs_annotations",
"@maven//:com_google_auto_service_auto_service_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_errorprone_error_prone_type_annotations",
Expand All @@ -90,6 +94,10 @@ java_library(
junit_tests(
name = "tests",
srcs = glob(["**/*Test.java"]),
srcs_utils = glob(
["**/Test*.java"],
exclude = ["**/*Test.java"],
),
deps = [
":thing_java",
":thing_java_proto",
Expand All @@ -104,6 +112,8 @@ junit_tests(
"//java/dev/enola/rdf",
"//models",
"//test",
"@maven//:com_github_spotbugs_spotbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:org_jspecify_jspecify",
],
)
2 changes: 2 additions & 0 deletions java/dev/enola/thing/IImmutablePredicatesObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package dev.enola.thing;

import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.ThreadSafe;

@Immutable
@ThreadSafe
public interface IImmutablePredicatesObjects extends PredicatesObjects {}
44 changes: 25 additions & 19 deletions java/dev/enola/thing/ImmutablePredicatesObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,38 @@
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.ThreadSafe;

import org.jspecify.annotations.Nullable;

import java.util.Collection;
import java.util.Objects;

@Immutable
@ThreadSafe
public class ImmutablePredicatesObjects implements IImmutablePredicatesObjects {

// Suppressed because of @ImmutableTypeParameter T in PredicatesObjects.Builder#set:
@SuppressWarnings("Immutable")
private final ImmutableMap<String, Object> properties;

private final ImmutableMap<String, String> datatypes;
protected final ImmutableMap<String, Object> properties;

public static PredicatesObjects.Builder builder() {
return new ImmutablePredicatesObjects.Builder();
}

public static PredicatesObjects.Builder builderWithExpectedSize(int expectedSize) {
return new ImmutablePredicatesObjects.Builder(expectedSize);
}
protected final ImmutableMap<String, String> datatypes;

protected ImmutablePredicatesObjects(
ImmutableMap<String, Object> properties, ImmutableMap<String, String> datatypes) {
this.properties = properties;
this.datatypes = datatypes;
}

public static PredicatesObjects.Builder<? extends ImmutablePredicatesObjects> builder() {
return new ImmutablePredicatesObjects.Builder<>();
}

public static PredicatesObjects.Builder<? extends ImmutablePredicatesObjects>
builderWithExpectedSize(int expectedSize) {
return new ImmutablePredicatesObjects.Builder<>(expectedSize);
}

@Override
public ImmutableMap<String, Object> properties() {
return properties;
Expand All @@ -59,7 +64,7 @@ public Collection<String> predicateIRIs() {

@Override
@SuppressWarnings("unchecked")
public <T> T get(String predicateIRI) {
public <T> @Nullable T get(String predicateIRI) {
return (T) properties.get(predicateIRI);
}

Expand All @@ -68,7 +73,7 @@ public ImmutableMap<String, String> datatypes() {
}

@Override
public String datatype(String predicateIRI) {
public @Nullable String datatype(String predicateIRI) {
return datatypes.get(predicateIRI);
}

Expand All @@ -77,8 +82,7 @@ public boolean equals(Object obj) {
if (obj == this) return true;
// NO NEED: if (obj == null) return false;
// NOT: if (getClass() != obj.getClass()) return false;
if (!(obj instanceof ImmutablePredicatesObjects)) return false;
final ImmutablePredicatesObjects other = (ImmutablePredicatesObjects) obj;
if (!(obj instanceof ImmutablePredicatesObjects other)) return false;
return Objects.equals(this.properties, other.properties)
&& Objects.equals(this.datatypes, other.datatypes);
}
Expand All @@ -97,11 +101,12 @@ public String toString() {
}

@Override
public PredicatesObjects.Builder copy() {
return new ImmutablePredicatesObjects.Builder(properties(), datatypes());
public PredicatesObjects.Builder<? extends ImmutablePredicatesObjects> copy() {
return new Builder<>(properties(), datatypes());
}

private static final class Builder implements PredicatesObjects.Builder {
private static final class Builder<B extends PredicatesObjects>
implements PredicatesObjects.Builder<B> {

private final ImmutableMap.Builder<String, Object> properties;
private final ImmutableMap.Builder<String, String> datatypes;
Expand Down Expand Up @@ -144,8 +149,9 @@ public PredicatesObjects.Builder set(
}

@Override
public PredicatesObjects build() {
return new ImmutablePredicatesObjects(properties.build(), datatypes.build());
public B build() {
// TODO Remove (B) type cast
return (B) new ImmutablePredicatesObjects(properties.build(), datatypes.build());
}
}
}
Loading

0 comments on commit 64abfdb

Please sign in to comment.