Enum Class UpgradeConversionPolicy
- All Implemented Interfaces:
Serializable
,Comparable<UpgradeConversionPolicy>
,Constable
Type Changes
Permazen fields are identified by name. Therefore, it's possible
for the "same" field to have two different types in two different schemas. When migrating an object's schema,
Permazen can automatically perform certain conversions of simple field values from the old type to the new type.
For example, an int
field value 1234
can be automatically converted into String
field value
"1234"
.
Automatic upgrade conversion is only supported for simple fields that are not sub-fields of complex fields.
See Encoding.convert(io.permazen.encoding.Encoding<S>, S)
for details about supported conversions between simple encodings. In addition,
Counter
fields may be automatically converted to/from any numeric Java primitive (or primitive wrapper) type.
This class is used to specify whether such automatic conversion should occur when a simple field's type changes, and if so, whether the conversion must always succeed.
References and Enums
Permazen considers Enum
types with different identifier lists as different types. However, automatic
conversion of Enum
values in simple fields will work if the existing value's name (enum identifier)
is valid for the new Enum
type.
Automatic conversion of reference fields works as long as the referenced object's type is assignable to the field's new Java type; otherwise, the field is reset to null.
Conversion Policies
With RESET
, no automatic conversion is attempted: the field is always reset to the default value of
the new type. With ATTEMPT
and REQUIRE
, automatic conversion of field values is attempted.
For some types and/or field values, conversion is not possible. In this case, REQUIRE
generates a
UpgradeConversionException
, while ATTEMPT
just reverts to the behavior of RESET
.
Note that arbitrary conversion logic is always possible using @OnSchemaChange
.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionAttempt automatic conversion of field values to the new type, and if automatic conversion fails, set the value to the new type's default value as wouldRESET
.Attempt automatic conversion of field values to the new type, and if automatic conversion fails, throw aUpgradeConversionException
.Do not attempt to automatically convert values to the new type. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Determine whether this policy should attempt to convert field values from the old type to the new type.boolean
Determine whether failed attempts to convert a field's value from the old type to the new type should be fatal.static UpgradeConversionPolicy
Returns the enum constant of this class with the specified name.static UpgradeConversionPolicy[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
RESET
Do not attempt to automatically convert values to the new type.Instead, during a schema change, the field will be reset to the default value of the field's new type.
-
ATTEMPT
Attempt automatic conversion of field values to the new type, and if automatic conversion fails, set the value to the new type's default value as wouldRESET
. -
REQUIRE
Attempt automatic conversion of field values to the new type, and if automatic conversion fails, throw aUpgradeConversionException
.
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
isConvertsValues
public boolean isConvertsValues()Determine whether this policy should attempt to convert field values from the old type to the new type.If this is false, the field's value will be set to the new type's default value. If this is true, the field's old value will be converted to the field's new type if possible; if the conversion fails, the behavior depends on
isRequireConversion()
.- Returns:
- true if under this policy conversion should be attempted
-
isRequireConversion
public boolean isRequireConversion()Determine whether failed attempts to convert a field's value from the old type to the new type should be fatal.If this is true, a failed conversion attempt results in a
UpgradeConversionException
being thrown. If this is false, a failed conversion attempt results in the field being set to the new type's default value.- Returns:
- true if under this policy conversion is mandatory
-