Skip to content

Commit

Permalink
Java: Add docs to GlideString (#1827)
Browse files Browse the repository at this point in the history
* Address PR comments.
  • Loading branch information
Yury-Fridlyand authored Jul 6, 2024
1 parent 3fcaba0 commit 108fbff
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions java/client/src/main/java/glide/api/models/GlideString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,42 @@
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.Getter;

// TODO docs for the god of docs
/**
* Represents a Valkey string type. Since Valkey stores strings as <code>byte[]</code>, such strings
* can contain non-UTF8 compatible symbols or even arbitrary binary data BLOBs.<br>
* This class stores data <code>byte[]</code> too, but provides API to represent data as a {@link
* String} if conversion is possible.
*
* @see <a href="@see https://valkey.io/docs/topics/strings/">valkey.io</a> for more details.
*/
public class GlideString implements Comparable<GlideString> {

/** The Valkey string as a binary representation. */
@Getter private byte[] bytes;

/**
* Stores a string when it is possible.<br>
* {@link String} representation of the value is only stored if conversion via {@link
* #canConvertToString()} is possible. The conversion is lazy, and only converted on the first
* call {@link #toString()}, {@link #getString()}, or {@link #canConvertToString()}.
*/
private String string = null;

/** Flag whether possibility to convert to string was checked. */
private final AtomicBoolean conversionChecked = new AtomicBoolean(false);

/** Constructor is private - use {@link #gs} or {@link #of} to instantiate an object. */
private GlideString() {}

/** Create a GlideString using a {@link String}. */
public static GlideString of(String string) {
var res = new GlideString();
res.string = string;
res.bytes = string.getBytes(StandardCharsets.UTF_8);
return res;
}

/** Create a GlideString using a byte array. */
public static GlideString of(byte[] bytes) {
var res = new GlideString();
res.bytes = bytes;
Expand All @@ -46,19 +64,23 @@ public static <ArgType> GlideString of(ArgType o) {
}
}

/** Create a GlideString using a {@link String}. */
public static GlideString gs(String string) {
return GlideString.of(string);
}

/** Create a GlideString using a byte array. */
public static GlideString gs(byte[] bytes) {
return GlideString.of(bytes);
}

/** Converts stored data to a human-friendly {@link String} if it is possible. */
@Override
public String toString() {
return getString();
}

/** Converts stored data to a human-friendly {@link String} if it is possible. */
public String getString() {
if (string != null) {
return string;
Expand All @@ -70,10 +92,12 @@ public String getString() {
return String.format("Value not convertible to string: byte[] %d", Arrays.hashCode(bytes));
}

/** Compare with another GlideString. */
public int compareTo(GlideString o) {
return Arrays.compare(this.bytes, o.bytes);
}

/** Check whether stored data could be converted to a {@link String}. */
public boolean canConvertToString() {
if (string != null) {
return true;
Expand All @@ -86,7 +110,7 @@ public boolean canConvertToString() {
synchronized (this) {
if (conversionChecked.get()) {
return false;
} else
} else {
try {
// TODO find a better way to check this
// Detect whether `bytes` could be represented by a `String` without data corruption
Expand All @@ -100,6 +124,7 @@ public boolean canConvertToString() {
} finally {
conversionChecked.set(true);
}
}
}
}
}
Expand Down

0 comments on commit 108fbff

Please sign in to comment.