Package com.unboundid.ldap.sdk.persist
Class DefaultObjectEncoder
java.lang.Object
com.unboundid.ldap.sdk.persist.ObjectEncoder
com.unboundid.ldap.sdk.persist.DefaultObjectEncoder
- All Implemented Interfaces:
Serializable
@NotMutable
@ThreadSafety(level=COMPLETELY_THREADSAFE)
public final class DefaultObjectEncoder
extends ObjectEncoder
This class provides the default implementation of an
The following basic types will be supported, with the following encodings:
In addition, arrays of all of the above types are also supported, in which case each element of the array will be a separate value in the corresponding LDAP attribute. Lists (including
Note that you should be careful when using primitive types, since they cannot be unassigned and therefore will always have a value. When using an LDAP entry to initialize an object any fields with primitive types which are associated with LDAP attributes not present in the entry will have the default value assigned to them in the zero-argument constructor, or will have the JVM-supplied default value if no value was assigned to it in the constructor. If the associated object is converted back to an LDAP entry, then those fields will be included in the entry that is generated, even if they were not present in the original entry. To avoid this problem, you can use the object types rather than the primitive types (e.g.,
ObjectEncoder
object that will be used when encoding and decoding fields to be written to
or read from an LDAP directory server.
The following basic types will be supported, with the following encodings:
- Any kind of enumeration -- Encoded using the name of the enum value
java.util.concurrent.atomic.AtomicInteger-- Encoded using the string representation of the valuejava.util.concurrent.atomic.AtomicLong-- Encoded using the string representation of the valuejava.math.BigDecimal-- Encoded using the string representation of the valuejava.math.BigInteger-- Encoded using the string representation of the valueboolean-- Encoded as either "TRUE" or "FALSE"java.lang.Boolean-- Encoded as either "TRUE" or "FALSE"byte[]-- Encoded as the raw bytes contained in the arraychar[]-- Encoded as a string containing the characters in the arrayjava.util.Date-- Encoded using the generalized time syntaxcom.unboundid.ldap.sdk.DN-- Encoded using the string representation of the valuedouble-- Encoded using the string representation of the valuejava.lang.Double-- Encoded using the string representation of the valuecom.unboundid.ldap.sdk.Filter-- Encoded using the string representation of the valuefloat-- Encoded using the string representation of the valuejava.lang.Float-- Encoded using the string representation of the valueint-- Encoded using the string representation of the valuejava.lang.Integer-- Encoded using the string representation of the valuecom.unboundid.ldap.sdk.LDAPURL-- Encoded using the string representation of the valuelong-- Encoded using the string representation of the valuejava.lang.Long-- Encoded using the string representation of the valuecom.unboundid.ldap.sdk.RDN-- Encoded using the string representation of the valueshort-- Encoded using the string representation of the valuejava.lang.Short-- Encoded using the string representation of the valuejava.lang.String-- Encoded using the valuejava.lang.StringBuffer-- Encoded using the string representation of the valuejava.lang.StringBuilder-- Encoded using the string representation of the valuejava.net.URI-- Encoded using the string representation of the value.java.net.URL-- Encoded using the string representation of the value.java.util.UUID-- Encoded using the string representation of the value
writeObject, readObject, and
readObjectNoData methods that use the desired encoding. Alternately,
you may create a custom ObjectEncoder implementation for that object
type, or use getter/setter methods that convert between string/byte[]
representations and the desired object types.
In addition, arrays of all of the above types are also supported, in which case each element of the array will be a separate value in the corresponding LDAP attribute. Lists (including
ArrayList, LinkedList, and
CopyOnWriteArrayList) and sets (including HashSet,
LinkedHashSet, TreeSet, and CopyOnWriteArraySet) of
the above types are also supported.
Note that you should be careful when using primitive types, since they cannot be unassigned and therefore will always have a value. When using an LDAP entry to initialize an object any fields with primitive types which are associated with LDAP attributes not present in the entry will have the default value assigned to them in the zero-argument constructor, or will have the JVM-supplied default value if no value was assigned to it in the constructor. If the associated object is converted back to an LDAP entry, then those fields will be included in the entry that is generated, even if they were not present in the original entry. To avoid this problem, you can use the object types rather than the primitive types (e.g.,
java.lang.Boolean instead of the boolean primitive), in which
case any fields associated with attributes that are not present in the entry
being de-serialized will be explicitly set to null.- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConstructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value of the specified field.Constructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value returned by the specified method.voiddecodeField(Field field, Object object, Attribute attribute) Updates the provided object to assign a value for the specified field from the contents of the given attribute.encodeFieldValue(Field field, Object value, String name) Encodes the provided field to an LDAP attribute.encodeMethodValue(Method method, Object value, String name) Encodes the provided method to an LDAP attribute.voidinvokeSetter(Method method, Object object, Attribute attribute) Updates the provided object to invoke the specified method to set a value from the contents of the given attribute.booleansupportsMultipleValues(Field field) Indicates whether the provided field can hold multiple values.booleansupportsMultipleValues(Method method) Indicates whether the provided setter method takes an argument that can hold multiple values.booleansupportsType(Type t) Indicates whether this object encoder may be used to encode or decode objects of the specified type.Methods inherited from class com.unboundid.ldap.sdk.persist.ObjectEncoder
constructAttributeType, constructAttributeType, setNull, setNull
-
Constructor Details
-
DefaultObjectEncoder
public DefaultObjectEncoder()Creates a new instance of this encoder.
-
-
Method Details
-
supportsType
Indicates whether this object encoder may be used to encode or decode objects of the specified type.- Specified by:
supportsTypein classObjectEncoder- Parameters:
t- The type of object for which to make the determination.- Returns:
trueif this object encoder may be used for objects of the specified type, orfalseif not.
-
constructAttributeType
@NotNull public AttributeTypeDefinition constructAttributeType(@NotNull Field f, @NotNull OIDAllocator a) throws LDAPPersistException Constructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value of the specified field.- Specified by:
constructAttributeTypein classObjectEncoder- Parameters:
f- The field for which to construct an LDAP attribute type definition. It will include theLDAPFieldannotation type.a- The OID allocator to use to generate the object identifier. It must not benull.- Returns:
- The constructed attribute type definition.
- Throws:
LDAPPersistException- If this object encoder does not support encoding values for the associated field type.
-
constructAttributeType
@NotNull public AttributeTypeDefinition constructAttributeType(@NotNull Method m, @NotNull OIDAllocator a) throws LDAPPersistException Constructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value returned by the specified method. Note that the object identifier used for the constructed attribute type definition is not required to be valid or unique.- Specified by:
constructAttributeTypein classObjectEncoder- Parameters:
m- The method for which to construct an LDAP attribute type definition. It will include theLDAPGetterannotation type.a- The OID allocator to use to generate the object identifier. It must not benull.- Returns:
- The constructed attribute type definition.
- Throws:
LDAPPersistException- If this object encoder does not support encoding values for the associated method type.
-
supportsMultipleValues
Indicates whether the provided field can hold multiple values.- Specified by:
supportsMultipleValuesin classObjectEncoder- Parameters:
field- The field for which to make the determination. It must be marked with theLDAPFieldannotation.- Returns:
trueif the provided field can hold multiple values, orfalseif not.
-
supportsMultipleValues
Indicates whether the provided setter method takes an argument that can hold multiple values.- Specified by:
supportsMultipleValuesin classObjectEncoder- Parameters:
method- The setter method for which to make the determination. It must be marked with theLDAPSetterannotation type and conform to the constraints associated with that annotation.- Returns:
trueif the provided method takes an argument that can hold multiple values, orfalseif not.
-
encodeFieldValue
@NotNull public Attribute encodeFieldValue(@NotNull Field field, @NotNull Object value, @NotNull String name) throws LDAPPersistException Encodes the provided field to an LDAP attribute.- Specified by:
encodeFieldValuein classObjectEncoder- Parameters:
field- The field to be encoded.value- The value for the field in the object to be encoded.name- The name to use for the constructed attribute.- Returns:
- The attribute containing the encoded representation of the provided field.
- Throws:
LDAPPersistException- If a problem occurs while attempting to construct an attribute for the field.
-
encodeMethodValue
@NotNull public Attribute encodeMethodValue(@NotNull Method method, @NotNull Object value, @NotNull String name) throws LDAPPersistException Encodes the provided method to an LDAP attribute.- Specified by:
encodeMethodValuein classObjectEncoder- Parameters:
method- The method to be encoded.value- The value returned by the method in the object to be encoded.name- The name to use for the constructed attribute.- Returns:
- The attribute containing the encoded representation of the provided method value.
- Throws:
LDAPPersistException- If a problem occurs while attempting to construct an attribute for the method.
-
decodeField
public void decodeField(@NotNull Field field, @NotNull Object object, @NotNull Attribute attribute) throws LDAPPersistException Updates the provided object to assign a value for the specified field from the contents of the given attribute.- Specified by:
decodeFieldin classObjectEncoder- Parameters:
field- The field to update in the provided object.object- The object to be updated.attribute- The attribute whose value(s) should be used to update the specified field in the given object.- Throws:
LDAPPersistException- If a problem occurs while attempting to assign a value to the specified field.
-
invokeSetter
public void invokeSetter(@NotNull Method method, @NotNull Object object, @NotNull Attribute attribute) throws LDAPPersistException Updates the provided object to invoke the specified method to set a value from the contents of the given attribute.- Specified by:
invokeSetterin classObjectEncoder- Parameters:
method- The method to invoke in the provided object.object- The object to be updated.attribute- The attribute whose value(s) should be used to update the specified method in the given object.- Throws:
LDAPPersistException- If a problem occurs while attempting to determine the value or invoke the specified method.
-