Class DefaultEncodingRegistry
- All Implemented Interfaces:
EncodingRegistry
EncodingRegistry
.
Instances automatically register all of Permazen's built-in Encoding
s (see below).
Array Types
Because this class is a subclass of SimpleEncodingRegistry
, encodings for array types
(other than primitive arrays) are created on demand, so they don't need to be explicitly registered.
Enum Types
Encodings for Enum
types are not registered in an EncodingRegistry
. Instead, Enum
values are represented as EnumValue
instances, and Enum
fields are specially
defined in the schema with an explicit identifier list. In turn, EnumValue
's are
encoded by EnumValueEncoding
.
Custom Encodings
During construction, instances scan the class path for custom EncodingRegistry
implementations
and will delegate to them when an encoding is not found. Custom EncodingRegistry
implementations are specified via
META-INF/services/io.permazen.encoding.EncodingRegistry
files or by module exports; see ServiceLoader
.
Custom implementations are only queried for non-array types.
If multiple custom EncodingRegistry
implementations advertise the same encoding, one will be
chosen arbitrarily.
Built-in Encodings
Permazen provides built-in Encoding
s covering the following Java types:
- Primitive types (
boolean
,int
, etc.) - Primitive array types (
boolean[]
,int[]
, etc.) - Primitive wrapper types (
Boolean
,Integer
, etc.) String
BigDecimal
BigInteger
BitSet
Date
UUID
IntSummaryStatistics
LongSummaryStatistics
DoubleSummaryStatistics
URI
File
InetAddress
Inet4Address
Inet6Address
Pattern
java.time.*
InternetAddress
(if present on the classpath)
-
Field Summary
Fields inherited from class io.permazen.encoding.SimpleEncodingRegistry
log
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
Register Permazen's built-in encodings.protected void
addOptionalBuiltinEncoding
(String name, Function<EncodingId, ? extends Encoding<?>> builder) Register a built-in encoding, but only if its target class is found on the classpath.protected void
Register Permazen's optional built-in encodings.protected void
Register Permazen's standard built-in encodings.aliasForId
(EncodingId encodingId) Get the alias (or "nickname") for the given encoding ID in this registry, if any.protected void
Scan the class path (viaServiceLoader
and the current thread's context class loader) for customEncodingRegistry
implementationsEncoding<?>
getEncoding
(EncodingId encodingId) Get theEncoding
with the given encoding ID in this registry.getEncodings
(TypeToken<T> typeToken) Get all of theEncoding
s in this registry that supports values of the given Java type.idForAlias
(String alias) Get the encoding ID corresponding to the given alias (or "nickname"), if any.protected void
Initialize this instance.Methods inherited from class io.permazen.encoding.SimpleEncodingRegistry
add, addNullSafe, buildArrayEncoding, register
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.permazen.encoding.EncodingRegistry
getEncoding, getEncoding
-
Field Details
-
customEncodingRegistries
-
-
Constructor Details
-
DefaultEncodingRegistry
public DefaultEncodingRegistry()Constructor.This constructor invokes
initialize()
.
-
-
Method Details
-
idForAlias
Description copied from interface:EncodingRegistry
Get the encoding ID corresponding to the given alias (or "nickname"), if any.See
aliasForId()
for details on aliases.If
alias
is a valid alias, this method should return the correspondingEncodingId
. Ifalias
is not a valid alias, but is a valid encoding ID in string form, this method should return the correspondingEncodingId
as if bynew EncodingId(alias)
. Otherwise, this method should throwIllegalArgumentException
.Note: the
EncodingId
corresponding to an alias does not need to be actually registered with this instance in order for the alias to be valid.The implementation in
EncodingRegistry
just invokesnew EncodingId(alias)
.- Parameters:
alias
- encoding ID alias- Returns:
- corresponding encoding ID, never null
-
aliasForId
Description copied from interface:EncodingRegistry
Get the alias (or "nickname") for the given encoding ID in this registry, if any.An
EncodingRegistry
may support aliases for some of its encoding ID's. Aliases are simply more friendly names forEncodingId
strings, which are formatted as Uniform Resource Names (URNs).Whereas
EncodingId
's are globally unique, aliases are only meaningful to the particularEncodingRegistry
instance being queried. When a schema is recorded in a database, actualEncodingId
's are always used.In Permazen's
DefaultEncodingRegistry
, the built-in encodings all have aliases; for example,"int"
is an alias for"urn:fdc:permazen.io:2020:int"
. Permazen's built-in encoding aliases are available viaEncodingIds.aliasForId()
andEncodingIds.idForAlias()
.If no alias is known for
encodingId
, this method should returnEncodingId.getId()
.Note: an
EncodingId
does not need to be actually registered with this instance in order for it to have an alias.The implementation in
EncodingRegistry
always returnsEncodingId.getId()
.- Parameters:
encodingId
- encoding ID- Returns:
- corresponding alias, if any, otherwise
EncodingId.getId()
- See Also:
-
getEncoding
Description copied from interface:EncodingRegistry
Get theEncoding
with the given encoding ID in this registry.- Specified by:
getEncoding
in interfaceEncodingRegistry
- Overrides:
getEncoding
in classSimpleEncodingRegistry
- Parameters:
encodingId
- encoding ID- Returns:
- corresponding
Encoding
, if any, otherwise null
-
getEncodings
Description copied from interface:EncodingRegistry
Get all of theEncoding
s in this registry that supports values of the given Java type.The Java type must exactly match the
Encoding
's supported Java type.- Specified by:
getEncodings
in interfaceEncodingRegistry
- Overrides:
getEncodings
in classSimpleEncodingRegistry
- Type Parameters:
T
- encoding value type- Parameters:
typeToken
- encoding value type- Returns:
- unmodifiable list of
Encoding
s supporting Java values of typetypeToken
, possibly empty
-
initialize
protected void initialize()Initialize this instance.This method is invoked by the default constructor. The implementation in
DefaultEncodingRegistry
invokesaddBuiltinEncodings()
and thenfindCustomEncodingRegistries()
. -
addBuiltinEncodings
protected void addBuiltinEncodings()Register Permazen's built-in encodings.The implementation in
DefaultEncodingRegistry
invokesaddStandardBuiltinEncodings()
and thenaddOptionalBuiltinEncodings()
. -
addStandardBuiltinEncodings
protected void addStandardBuiltinEncodings()Register Permazen's standard built-in encodings. -
addOptionalBuiltinEncodings
protected void addOptionalBuiltinEncodings()Register Permazen's optional built-in encodings.The optional built-in encodings are ones that depend on optional dependencies. An example is
InternetAddressEncoding
which depends on the Jakarta Mail API. -
addOptionalBuiltinEncoding
protected void addOptionalBuiltinEncoding(String name, Function<EncodingId, ? extends Encoding<?>> builder) Register a built-in encoding, but only if its target class is found on the classpath.The implementation in
DefaultEncodingRegistry
invokes the givenbuilder
but will catch and ignore anyNoClassDefFoundError
thrown. Otherwise, the encoding is registered.No attempt to initialize the encoding class should have occurred prior to invoking this method.
- Parameters:
name
- builtin encoding ID suffixbuilder
- builder for encoding
-
findCustomEncodingRegistries
protected void findCustomEncodingRegistries()Scan the class path (viaServiceLoader
and the current thread's context class loader) for customEncodingRegistry
implementations
-