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" ]
.
This class does not support null arrays; wrap in NullSafeEncoding
to get that.
The default value is the empty array.
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
(EncodingId encodingId, Encoding<E> elementEncoding, TypeToken<T> typeToken) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionint
Order two field 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.int
hashCode()
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
getDefaultValueObject, 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, getDefaultValue, getKeyRange, hasPrefix0x00, hasPrefix0xff, read, skip, sortsNaturally, supportsNull, validate, validateAndWrite, withEncodingId, write
-
Constructor Details
-
ArrayEncoding
Constructor.- Parameters:
encodingId
- encoding ID for this encoding, or null to be anonymouselementEncoding
- 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
must be one of the valid XML characters (tab, newline, carriage return,\u0020 - \ud7ff
, and\ue000 - \ufffd
).- 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 field 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 field 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>
-
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
-