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
Encodings.
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, typeTokenFields inherited from interface io.permazen.encoding.Encoding
MAX_ARRAY_DIMENSIONS -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedArrayEncoding(Encoding<E> elementEncoding, TypeToken<T> typeToken) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionintOrder two values.<S> Tprotected abstract TcreateArray(List<E> elements) Create a new array instance containing the given elements.booleanstatic String[]fromArrayString(String string) fromString(String string) Parse a non-null value previously encoded bytoString(T).protected abstract EgetArrayElement(T array, int index) Get an element from the given array.protected abstract intgetArrayLength(T array) Get the length of the given array.Get the element type.Get the fixed width of this encoding, if any.inthashCode()booleanDetermine if this instance sorts Java instances naturally.booleanDetermine whether this encoding supports null values.static StringtoArrayString(String[] strings, boolean spacing) Encode a non-null value as aStringfor later decoding byfromString().Methods inherited from class io.permazen.encoding.AbstractEncoding
getDefaultValue, getDefaultValueBytes, getEncodingId, getTypeToken, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Comparator
reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongMethods inherited from interface io.permazen.encoding.Encoding
decode, encode, 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- ifelementEncodingis anArrayEncodingwithEncoding.MAX_ARRAY_DIMENSIONSdimensions
-
-
Method Details
-
getElementEncoding
Get the element type.- Returns:
- element type
-
toString
Description copied from interface:EncodingEncode a non-null value as aStringfor 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
valueacceptable tofromString() - See Also:
-
fromString
Description copied from interface:EncodingParse a non-null value previously encoded bytoString(T).- Parameters:
string- non-null value previously encoded as aStringbytoString(T)- Returns:
- actual value
-
compare
Description copied from interface:EncodingOrder 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
Comparatorinterface normally requires).Note: by convention, null values usually sort last.
-
hashCode
public int hashCode()- Overrides:
hashCodein classAbstractEncoding<T>
-
equals
- Specified by:
equalsin interfaceComparator<T>- Overrides:
equalsin classAbstractEncoding<T>
-
supportsNull
public boolean supportsNull()Description copied from interface:EncodingDetermine whether this encoding supports null values.- Returns:
- true if null is a valid value, otherwise false
-
sortsNaturally
public boolean sortsNaturally()Description copied from interface:NaturalSortAwareDetermine if this instance sorts Java instances naturally.This method should return true only if all of the following are true:
- This class also implements
Comparatorfor some Java typeT. - Type
Thas a natural ordering (i.e.,Titself 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:EncodingGet 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:EncodingAttempt to convert a value from the givenEncodinginto a value of thisEncoding.For a non-null
value, the implementation inEncodingfirst checks whether thevalueis already a valid value for this encoding; if so, the value is returned. Otherwise, it invokesencoding.toString(value)to convertvalueinto aString, and then attempts to parse that string viathis.fromString(); if the parse fails, anIllegalArgumentExceptionis 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
valueis null, the implementation inEncodingreturns null, unless this encoding does not support null values, in which case anIllegalArgumentExceptionis 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 aStringare convertible to each other - A
charand aStringof length one are convertible to each other (otherStrings are not) - Arrays: converted by converting each array element individually (if possible)
- Type Parameters:
S- source encoding- Parameters:
type- theEncodingofvaluevalue- the value to convert- Returns:
valueconverted 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- ifstringsis null
-
fromArrayString
Parse a combinedStringrepresentation of an array ofStrings 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- ifstringis 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
-