Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial binary.ttl incl. mf:multibaseBinary #1149

Merged
merged 3 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions java/dev/enola/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ java_library(
visibility = ["//:__subpackages__"],
deps = [
"//tools/version",
"@enola_maven//:com_google_errorprone_error_prone_annotations",
"@enola_maven//:com_google_guava_guava",
"@enola_maven//:dev_dirs_directories",
"@enola_maven//:org_jspecify_jspecify",
Expand Down
52 changes: 48 additions & 4 deletions java/dev/enola/common/ByteSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,46 @@
*/
package dev.enola.common;

import com.google.errorprone.annotations.Immutable;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.UUID;

/**
* Immutable Sequence of Bytes, of variable length.
* Immutable Sequence of Bytes, of variable (but obviously fixed) length.
*
* <p>Typically intended to be used for "small"(-ish) size, like binary IDs, hashes, cryptographic
* keys, and such things; do not use this for "large BLOBs" (like images or so). The hashCode is
* cached.
* keys, and such things; do not use this for "very large BLOBs". The hashCode is cached.
*
* <p>The <tt>com.google.protobuf.ByteString</tt> is very similar - but we don't want to depend on
* the ProtoBuf library JUST for having a type like this.
*
* <p>This intentionally does <b>not</b> implement <tt>Iterable<Byte></tt> to avoid boxing overhead.
*/
@Immutable
public final class ByteSeq implements Comparable<ByteSeq> {

// TODO Re-think if this is really the same as a com.google.common.io.ByteSource?!
// If concluding that it's not, then update JavaDoc to explain why...

// TODO Should this extend com.google.common.io.ByteSource?!

// TODO Should this have a static from(ByteSource) method?

// TODO Add String toBase64() and static ID fromBase64(String data)
// using https://www.baeldung.com/java-base64-encode-and-decode

// TODO Support substring (slice?) and concatenation, like Protobuf ByteString?

// TODO Add a ByteBuffer asReadOnlyByteBuffer() method?

// TODO Add startsWith(ByteSeq) and endsWith(ByteSeq) methods?

// TODO Add asInputStream() method?

public static final ByteSeq EMPTY = new ByteSeq(new byte[0]);

public static final class Builder {
Expand Down Expand Up @@ -87,7 +113,11 @@ public static ByteSeq from(byte[] bytes) {
* because it avoids accidentally using the (non-fixed) platform default charset.
*/
public static ByteSeq from(String string) {
return new ByteSeq(string.getBytes(StandardCharsets.UTF_8));
return from(string, StandardCharsets.UTF_8);
}

public static ByteSeq from(String string, Charset charset) {
return new ByteSeq(string.getBytes(charset));
}

/*
Expand All @@ -114,13 +144,17 @@ public static ByteSeq from(UUID uuid) {
return builder.build();
}

@SuppressWarnings("Immutable") // Holy promise never to change the bytes!
private final byte[] bytes;

@SuppressWarnings("Immutable") // Holy promise never to use only as cache!
private transient int hashCode;

private ByteSeq(byte[] bytes) {
this.bytes = bytes;
}

// TODO Rename toBytes() to copyIntoNewByteArray() ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider renaming toBytes() to copyIntoNewByteArray() to more clearly indicate that a new byte array is created and returned, avoiding potential confusion about whether the internal byte array is being directly exposed.

Suggested change
// TODO Rename toBytes() to copyIntoNewByteArray() ?
// TODO Rename toBytes() to copyIntoNewByteArray() ?
public byte[] copyIntoNewByteArray() {

public byte[] toBytes() {
return Arrays.copyOf(bytes, bytes.length);
}
Expand All @@ -139,6 +173,7 @@ public ByteString toByteString() {
return ByteString.copyFrom(bytes);
}
*/
// TODO Rename toUUID() to asUUID() for consistency with asString()?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider renaming toUUID() to asUUID() for consistency with the existing asString() method, which provides a more fluent and readable API.

Suggested change
// TODO Rename toUUID() to asUUID() for consistency with asString()?
// TODO Rename toUUID() to asUUID() for consistency with asString()?
public UUID asUUID() {

public UUID toUUID() {
if (bytes.length != 16) {
throw new IllegalStateException(
Expand All @@ -155,6 +190,15 @@ public String asString() {
return new String(bytes, StandardCharsets.UTF_8);
}

/**
* Returns {@code true} if the size is {@code 0}, {@code false} otherwise.
*
* @return true if this is zero bytes long
*/
public boolean isEmpty() {
return size() == 0;
}

/**
* Return debug information about this object.
*
Expand Down
2 changes: 2 additions & 0 deletions java/dev/enola/common/io/resource/ReadableResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

public interface ReadableResource extends AbstractResource {

// TODO toDataURI(), like DataResource

ByteSource byteSource();

// TODO Consider replacing or integrating this with Converter?!
Expand Down
2 changes: 2 additions & 0 deletions java/dev/enola/model/xsd/Datatypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public final class Datatypes {
// TODO Test coverage for this INT RegExp...
"(\\d{4})-(\\d{2})-(\\d{2})T([0-9:]+)(Z|([+-])(\\d{2})(:(\\d{2})?)?)?");

// TODO base64Binary & hexBinary; see binary.ttl for their Pattern

// Beware: The order here matters very much, for DatatypeRepository#match()
public static final Iterable<Datatype<?>> ALL =
ImmutableList.of(DATE_TIME, DATE, BOOLEAN, INT, IRI, STRING);
Expand Down
49 changes: 49 additions & 0 deletions models/enola.dev/binary.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2025 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.

@prefix enola: <https://enola.dev/>.
@prefix java: <https://enola.dev/java/>.
@prefix proto: <https://enola.dev/proto/>.
@prefix mf: <https://multiformats.io/>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.

enola:binary a rdfs:Datatype;
java:type "dev.enola.common.ByteSeq";
proto:type "bytes";
enola:wikidata "Q3775042", "Q218013".

# https://en.m.wikipedia.org/wiki/Binary-to-text_encoding

xsd:base64Binary enola:subDatatypeOf enola:binary;
# https://en.m.wikipedia.org/wiki/Data_URI_scheme
enola:iriTemplate "data:,{IT}";
xsd:pattern "[0-9a-zA-Z+/]*={0,2}".

xsd:hexBinary enola:subDatatypeOf enola:binary;
xsd:pattern "(?:[0-9a-fA-F]{2})*".

enola:UUID enola:subDatatypeOf enola:binary;
xsd:pattern "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}".

# https://github.com/multiformats/multibase/issues/133
mf:multibaseBinary enola:subDatatypeOf enola:binary;
# see dev.enola.common.io.resource.MultibaseResource
enola:iriTemplate "multibase:{IT}";
# https://github.com/multiformats/multibase/blob/master/multibase.csv
xsd:pattern "[0179fFvVtTbBcChkKRzZmMuUpQ/🚀][^\\s]*".

# TODO Should dev.enola/id (?) be a subtype of binary?!
6 changes: 6 additions & 0 deletions models/enola.dev/enola.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ enola:olo a rdf:Property;
enola:wikidata "Q1779327";
enola:emoji "🔓".

enola:subDatatypeOf a rdf:Property;
rdfs:label "Sub-Datatype of"@en;
rdfs:range rdfs:Datatype;
rdfs:domain rdfs:Datatype;
enola:emoji "⬆️".

enola:ID a rdfs:Datatype;
schema:name "IDentifier Datatype.";
schema:description "This is not to be confused with https://schema.org/identifier, which is a [[rdf:Property]], whereas this is a [[rdfs:Datatype]].";
Expand Down
Loading