public class StringType extends NonNullFieldType<String>
String
type. Null values are not supported by this class.
Strings are encoded as a sequence of characters followed by 0x00
, where each character is encoded via
UnsignedIntEncoder
, with the special exception that the characters 0x0000
and 0x0001
are prefixed with a 0x01
byte to avoid writing a 0x00
. We rely on the fact that UnsignedIntEncoder
encodes 0
and 1
as 0x00
and 0x01
, respectively.
name, NAME_PATTERN, REFERENCE_TYPE_NAME, signature, typeToken
Constructor and Description |
---|
StringType() |
Modifier and Type | Method and Description |
---|---|
int |
compare(String string1,
String string2)
Order two field values.
|
<S> String |
convert(FieldType<S> type,
S value)
|
String |
fromParseableString(ParseContext ctx)
Parse a value previously encoded by
toParseableString() as a self-delimited String
and positioned at the start of the given parsing context. |
String |
fromString(String string)
Parse a non-null value previously encoded by
toString(T) . |
boolean |
hasPrefix0xff()
Determine whether any of this field type's encoded values start with a
0xff byte. |
String |
read(ByteReader reader)
Read a value from the given input.
|
void |
skip(ByteReader reader)
Read and discard a value from the given input.
|
String |
toParseableString(String value)
Encode a possibly null value as a
String for later decoding by fromParseableString() . |
String |
toString(String value)
Encode a non-null value as a
String for later decoding by fromString() . |
void |
write(ByteWriter writer,
String value)
Write a value to the given output.
|
validate
equals, genericizeForIndex, getDefaultValue, getDefaultValueObject, getEncodingSignature, getKeyRange, getName, getTypeToken, hashCode, hasPrefix0x00, toString, validateAndWrite
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
comparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
public String read(ByteReader reader)
FieldType
public void write(ByteWriter writer, String value)
FieldType
public void skip(ByteReader reader)
FieldType
public String toString(String value)
FieldType
String
for later decoding by fromString()
.
Each of the characters in the returned String
must be one of the valid XML characters
(tab, newline, carriage return, \u0020 - \ud7ff
, and \ue000 - \fffdf
).
The implementation in FieldType
checks that value
is not null, then delegates to FieldType.toParseableString(T)
.
Subclasses that override this method should also override fromString()
.
toString
in class FieldType<String>
value
- actual value, never nullvalue
acceptable to fromString()
public String fromString(String string)
FieldType
toString(T)
.
The implementation in FieldType
creates a new ParseContext
based on string
,
delegates to FieldType.toParseableString(T)
to parse it, and verifies that all of string
was consumed
during the parse. Subclasses that override this method should also override toString(T)
.
fromString
in class FieldType<String>
string
- non-null value previously encoded as a String
by toString(T)
public String toParseableString(String value)
FieldType
String
for later decoding by fromParseableString()
.
The string value must be self-delimiting, i.e., decodable even when followed by arbitrary additional characters,
and must not start with whitespace or closing square bracket ("]"
).
In addition, each of the characters in the returned String
must be one of the valid XML characters
(tab, newline, carriage return, \u0020 - \ud7ff
, and \ue000 - \fffdf
).
toParseableString
in class FieldType<String>
value
- actual value (possibly null)value
acceptable to fromParseableString()
public String fromParseableString(ParseContext ctx)
FieldType
toParseableString()
as a self-delimited String
and positioned at the start of the given parsing context.fromParseableString
in class FieldType<String>
ctx
- parse context starting with a string previously encoded via toParseableString()
public boolean hasPrefix0xff()
FieldType
0xff
byte.
Certain optimizations are possible when this is not the case. It is safe for this method to always return true.
Note: changing the return value of this method usually means changing the binary encoding, resulting in an incompatible type.
The implementation in FieldType
returns true
.
hasPrefix0xff
in class FieldType<String>
0xff
existspublic int compare(String string1, String string2)
FieldType
This method must provide a total ordering of all supported Java values that is consistent with the database ordering,
i.e., the unsigned lexicographical ordering of the corresponding byte[]
encoded field values.
If null is a supported Java value, then the returned Comparator
must accept null parameters without
throwing an exception (note, this is a stronger requirement than the Comparator
interface normally requires).
Note: by convention, null values usually sort last.
public <S> String convert(FieldType<S> type, S value)
FieldType
FieldType
into a value of this FieldType
.
For a non-null value
, the implementation in FieldType
first checks whether the value
is already
a valid value for this type; if so, the value is returned. Otherwise, it invokes type.
toString(value)
to convert value
into a String
, and then attempts to parse that string via
this.
fromString()
; if the parse fails, an IllegalArgumentException
is thrown.
If value
is null, the implementation in FieldType
returns null, unless this type does not support null
values, in which case an IllegalArgumentException
is thrown.
Special handling also exists for certain conversions between built-in types:
value != 0
char
and a String
of length one are convertible (other String
s are not)char[]
array and a String
are convertibleCopyright © 2022. All rights reserved.