Class NullSafeEncoding<T>
- Type Parameters:
T
- The associated Java type
- All Implemented Interfaces:
Encoding<T>
,NaturalSortAware
,Serializable
,Comparator<T>
- Direct Known Subclasses:
EnumValueEncoding
,PrimitiveWrapperEncoding
,ReferenceEncoding
Encoding
that wraps any other Encoding
not supporting null values and adds support for null values.
This class pre-pends a 0x01
to the binary encoding of non-null values, and uses a single 0xff
byte to
represent null values. Therefore, null values sort last. Also, the default value is null.
This class will automatically "inline" the 0xff
for null values and omit the 0x01
for non-null values
if the wrapped Encoding
's Encoding.hasPrefix0xff()
method returns false.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionThe innerEncoding
that this instance wraps.static final int
Not null sentinel byte value.static final int
Null sentinel byte value.Fields inherited from class io.permazen.encoding.AbstractEncoding
encodingId, typeToken
Fields inherited from interface io.permazen.encoding.Encoding
MAX_ARRAY_DIMENSIONS
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Order two values.<S> T
boolean
fromString
(String string) Parse a non-null value previously encoded bytoString(T)
.Get the fixed width of this encoding, if any.Get the innerEncoding
that this instance wraps.int
hashCode()
boolean
Determine whether any of this encoding's encoded values start with a0x00
byte.boolean
Always returns true.read
(ByteReader reader) Read a value from the given input.void
skip
(ByteReader reader) Read and discard abyte[]
encoded value from the given input.boolean
Determine if this instance sorts Java instances naturally.boolean
Always returns true.Encode a non-null value as aString
for later decoding byfromString()
.void
write
(ByteWriter writer, T value) Write a value to the given output.Methods inherited from class io.permazen.encoding.AbstractEncoding
getDefaultValue, getDefaultValueBytes, getEncodingId, getTypeToken, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Comparator
reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
Methods inherited from interface io.permazen.encoding.Encoding
decode, encode, getKeyRange, validate, validateAndWrite
-
Field Details
-
NOT_NULL_SENTINEL
public static final int NOT_NULL_SENTINELNot null sentinel byte value. Used only when inlining is not in effect.- See Also:
-
NULL_SENTINEL
public static final int NULL_SENTINELNull sentinel byte value.- See Also:
-
inner
The innerEncoding
that this instance wraps.
-
-
Constructor Details
-
NullSafeEncoding
Constructor.- Parameters:
encodingId
- encoding ID for this encoding, or null to be anonymousinner
- inner type that is not null safe
-
-
Method Details
-
getInnerEncoding
Get the innerEncoding
that this instance wraps.- Returns:
- inner type that is not null safe
-
read
Description copied from interface:Encoding
Read a value from the given input.- Parameters:
reader
- byte input- Returns:
- decoded value (possibly null)
-
write
Description copied from interface:Encoding
Write a value to the given output.- Parameters:
writer
- byte outputvalue
- value to write (possibly null)
-
skip
Description copied from interface:Encoding
Read and discard abyte[]
encoded value from the given input.If the value skipped over is invalid, this method may, but is not required to, throw
IllegalArgumentException
.If the value skipped over is truncated, this method must throw
IndexOutOfBoundsException
.- Parameters:
reader
- byte input
-
fromString
Description copied from interface:Encoding
Parse a non-null value previously encoded bytoString(T)
.- Parameters:
string
- non-null value previously encoded as aString
bytoString(T)
- Returns:
- actual value
-
toString
Description copied from interface:Encoding
Encode a non-null value as aString
for later decoding byfromString()
.Each of the characters in the returned
String
, when decoded as 32-bit Unicode codepoints, must contain only valid XML characters (seeXMLUtil.isValidChar(int)
).- Parameters:
value
- actual value, never null- Returns:
- string encoding of
value
acceptable tofromString()
- See Also:
-
convert
Description copied from interface:Encoding
Attempt to convert a value from the givenEncoding
into a value of thisEncoding
.For a non-null
value
, the implementation inEncoding
first checks whether thevalue
is already a valid value for this encoding; if so, the value is returned. Otherwise, it invokesencoding.
toString(value)
to convertvalue
into aString
, and then attempts to parse that string viathis.
fromString()
; if the parse fails, anIllegalArgumentException
is thrown. Note this means that any value will convert successfully to aString
, as long as it doesn't contain an invalid escape sequence (seeStringEncoding.toString(java.lang.String)
).If
value
is null, the implementation inEncoding
returns null, unless this encoding does not support null values, in which case anIllegalArgumentException
is thrown.Permazen's built-in encodings include the following conversions:
- Non-boolean Primitive types:
- Convert from other non-boolean primitive types as if by the corresponding Java cast
- Convert from boolean by converting to zero (if false) or one (if true)
- Boolean: converts from other primitive types as if by
value != 0
- A
char[]
array and aString
are convertible to each other - A
char
and aString
of length one are convertible to each other (otherString
s are not) - Arrays: converted by converting each array element individually (if possible)
- Type Parameters:
S
- source encoding- Parameters:
type
- theEncoding
ofvalue
value
- the value to convert- Returns:
value
converted to this instance's type
- Non-boolean Primitive types:
-
compare
Description copied from interface:Encoding
Order two values.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 values.If null is a supported Java value, then the this method 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.
-
sortsNaturally
public boolean sortsNaturally()Description copied from interface:NaturalSortAware
Determine if this instance sorts Java instances naturally.This method should return true only if all of the following are true:
- This class also implements
Comparator
for some Java typeT
. - Type
T
has a natural ordering (i.e.,T
itself implementsComparable
). - The ordering implied by this class's
compare()
method is identical toT
's natural ordering.
- Returns:
- true if this instance orders Java values in their natural order
- This class also implements
-
supportsNull
public boolean supportsNull()Always returns true.- Returns:
- true
-
hasPrefix0xff
public boolean hasPrefix0xff()Always returns true.- Returns:
- true
-
hasPrefix0x00
public boolean hasPrefix0x00()Description copied from interface:Encoding
Determine whether any of this encoding's encoded values start with a0x00
byte. Certain optimizations are possible when this is not the case. It is safe for this method to always return true.Note: changing the result of this method may result in an incompatible encoding if this encoding is wrapped in another class.
- Returns:
- true if an encoded value starting with
0x00
exists
-
getFixedWidth
Description copied from interface:Encoding
Get the fixed width of this encoding, if any.Some encodings encode every value into the same number of bytes. For such encodings, this method returns that number. For variable width encodings, this method must return empty.
- Returns:
- the number of bytes of every encoded value, or empty if the encoding length varies
-
hashCode
public int hashCode()- Overrides:
hashCode
in classAbstractEncoding<T>
-
equals
- Specified by:
equals
in interfaceComparator<T>
- Overrides:
equals
in classAbstractEncoding<T>
-