Class ArrayEncoding<T,E>
- Type Parameters:
T
- array typeE
- array element type
- All Implemented Interfaces:
Encoding<T>
,NaturalSortAware
,Serializable
,Comparator<T>
- Direct Known Subclasses:
Base64ArrayEncoding
,BooleanArrayEncoding
,CharacterArrayEncoding
,ObjectArrayEncoding
Encoding
s.
The string form looks like [ "elem1", "elem2", ..., "elemN" ]
.
Null values are not supported by this class and there is no default value.
Arrays sort lexicographically.
- See Also:
-
Field Summary
Fields inherited from class io.permazen.encoding.AbstractEncoding
encodingId, typeToken
Fields inherited from interface io.permazen.encoding.Encoding
MAX_ARRAY_DIMENSIONS
-
Constructor Summary
ModifierConstructorDescriptionprotected
ArrayEncoding
(Encoding<E> elementEncoding, TypeToken<T> typeToken) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionint
Order two values.<S> T
protected abstract T
createArray
(List<E> elements) Create a new array instance containing the given elements.boolean
static String[]
fromArrayString
(String string) fromString
(String string) Parse a non-null value previously encoded bytoString(T)
.protected abstract E
getArrayElement
(T array, int index) Get an element from the given array.protected abstract int
getArrayLength
(T array) Get the length of the given array.Get the element type.Get the fixed width of this encoding, if any.int
hashCode()
boolean
Determine if this instance sorts Java instances naturally.boolean
Determine whether this encoding supports null values.static String
toArrayString
(String[] strings, boolean spacing) Encode a non-null value as aString
for later decoding byfromString()
.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, hasPrefix0x00, hasPrefix0xff, read, skip, validate, validateAndWrite, write
-
Constructor Details
-
ArrayEncoding
Constructor.- Parameters:
elementEncoding
- array element type (possibly also anArrayEncoding
)typeToken
- array type token- Throws:
IllegalArgumentException
- ifelementEncoding
is anArrayEncoding
withEncoding.MAX_ARRAY_DIMENSIONS
dimensions
-
-
Method Details
-
getElementEncoding
Get the element type.- Returns:
- element type
-
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:
array
- actual value, never null- Returns:
- string encoding of
value
acceptable tofromString()
- See Also:
-
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
-
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.
-
hashCode
public int hashCode()- Overrides:
hashCode
in classAbstractEncoding<T>
-
equals
- Specified by:
equals
in interfaceComparator<T>
- Overrides:
equals
in classAbstractEncoding<T>
-
supportsNull
public boolean supportsNull()Description copied from interface:Encoding
Determine whether this encoding supports null values.- Returns:
- true if null is a valid value, otherwise false
-
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
-
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
-
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:
-
toArrayString
- Parameters:
strings
- the individual stringsspacing
- true to include spaces around each element- Returns:
- combined array string
- Throws:
IllegalArgumentException
- ifstrings
is null
-
fromArrayString
Parse a combinedString
representation of an array ofString
s and return the individual strings.This method inverts
ArrayEncoding.toArrayString()
. Extra whitespace is ignored.- Parameters:
string
- the array string- Returns:
- array of original strings
- Throws:
IllegalArgumentException
- ifstring
is null
-
getArrayLength
Get the length of the given array.- Parameters:
array
- non-null array- Returns:
- array length
-
getArrayElement
Get an element from the given array.- Parameters:
array
- non-null arrayindex
- index of target element inarray
- Returns:
- array element at index
index
-
createArray
Create a new array instance containing the given elements.- Parameters:
elements
- content for the new array- Returns:
- newly created array
-