diff --git a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKey.java b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKey.java index 4cecf6df..93bd080c 100644 --- a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKey.java +++ b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKey.java @@ -30,7 +30,9 @@ public sealed interface JObjectKey extends Serializable, Comparable } static JObjectKey fromByteBuffer(ByteBuffer buff) { - return new JObjectKeyImpl(StandardCharsets.ISO_8859_1.decode(buff).toString()); + byte[] bytes = new byte[buff.remaining()]; + buff.get(bytes); + return new JObjectKeyImpl(bytes); } @Override @@ -39,8 +41,6 @@ public sealed interface JObjectKey extends Serializable, Comparable @Override String toString(); - byte[] bytes(); - ByteBuffer toByteBuffer(); String value(); diff --git a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyImpl.java b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyImpl.java index 39e79104..a22edc2a 100644 --- a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyImpl.java +++ b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyImpl.java @@ -2,10 +2,25 @@ package com.usatiuk.objects; import com.usatiuk.dhfs.supportlib.UninitializedByteBuffer; +import java.io.Serial; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.util.Objects; + +public final class JObjectKeyImpl implements JObjectKey { + @Serial + private static final long serialVersionUID = 0L; + private final String value; + private transient ByteBuffer _bb = null; + + public JObjectKeyImpl(String value) { + this.value = value; + } + + public JObjectKeyImpl(byte[] bytes) { + this.value = new String(bytes, StandardCharsets.ISO_8859_1); + } -public record JObjectKeyImpl(String value) implements JObjectKey { @Override public int compareTo(JObjectKey o) { switch (o) { @@ -27,17 +42,36 @@ public record JObjectKeyImpl(String value) implements JObjectKey { } @Override - public byte[] bytes() { - return value.getBytes(StandardCharsets.ISO_8859_1); + public ByteBuffer toByteBuffer() { + if (_bb != null) return _bb; + + synchronized (this) { + if (_bb != null) return _bb; + var bytes = value.getBytes(StandardCharsets.ISO_8859_1); + var directBb = UninitializedByteBuffer.allocateUninitialized(bytes.length); + directBb.put(bytes); + directBb.flip(); + _bb = directBb; + return directBb; + } } @Override - public ByteBuffer toByteBuffer() { - var heapBb = StandardCharsets.ISO_8859_1.encode(value); - if (heapBb.isDirect()) return heapBb; - var directBb = UninitializedByteBuffer.allocateUninitialized(heapBb.remaining()); - directBb.put(heapBb); - directBb.flip(); - return directBb; + public String value() { + return value; } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (JObjectKeyImpl) obj; + return Objects.equals(this.value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + } diff --git a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMax.java b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMax.java index 9dc5ed85..982e08b2 100644 --- a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMax.java +++ b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMax.java @@ -18,11 +18,6 @@ public record JObjectKeyMax() implements JObjectKey { } } - @Override - public byte[] bytes() { - throw new UnsupportedOperationException(); - } - @Override public ByteBuffer toByteBuffer() { throw new UnsupportedOperationException(); diff --git a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMin.java b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMin.java index 82e1091e..4a8bbc69 100644 --- a/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMin.java +++ b/dhfs-parent/objects/src/main/java/com/usatiuk/objects/JObjectKeyMin.java @@ -18,11 +18,6 @@ public record JObjectKeyMin() implements JObjectKey { } } - @Override - public byte[] bytes() { - throw new UnsupportedOperationException(); - } - @Override public ByteBuffer toByteBuffer() { throw new UnsupportedOperationException();