-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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 { | ||||||||
|
@@ -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)); | ||||||||
} | ||||||||
|
||||||||
/* | ||||||||
|
@@ -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() ? | ||||||||
public byte[] toBytes() { | ||||||||
return Arrays.copyOf(bytes, bytes.length); | ||||||||
} | ||||||||
|
@@ -139,6 +173,7 @@ public ByteString toByteString() { | |||||||
return ByteString.copyFrom(bytes); | ||||||||
} | ||||||||
*/ | ||||||||
// TODO Rename toUUID() to asUUID() for consistency with asString()? | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming
Suggested change
|
||||||||
public UUID toUUID() { | ||||||||
if (bytes.length != 16) { | ||||||||
throw new IllegalStateException( | ||||||||
|
@@ -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. | ||||||||
* | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider renaming
toBytes()
tocopyIntoNewByteArray()
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.