Package org.mapstruct

Enum NullValuePropertyMappingStrategy

java.lang.Object
java.lang.Enum<NullValuePropertyMappingStrategy>
org.mapstruct.NullValuePropertyMappingStrategy
All Implemented Interfaces:
Serializable, Comparable<NullValuePropertyMappingStrategy>, java.lang.constant.Constable

public enum NullValuePropertyMappingStrategy extends Enum<NullValuePropertyMappingStrategy>
Strategy for dealing with null or not present properties in the source bean. The NullValuePropertyMappingStrategy can be defined on MapperConfig, Mapper, BeanMapping and Mapping. Precedence is arranged in the reverse order. So Mapping will override BeanMapping, will override Mapper The enum only applies to update methods: methods that update a pre-existing target (annotated with @MappingTarget).

Note: some types of mappings (collections, maps), in which MapStruct is instructed to use a getter or adder as target accessor see CollectionMappingStrategy, MapStruct will always generate a source property null check, regardless the value of the NullValuePropertyMappingStrategy to avoid addition of null to the target collection or map. Since the target is assumed to be initialised this strategy will not be applied.

Since:
1.3
Author:
Sjaak Derksen
  • Enum Constant Details

    • SET_TO_NULL

      public static final NullValuePropertyMappingStrategy SET_TO_NULL
      If a source bean property equals null the target bean property will be set explicitly to null or Optional.empty() in case the target type is an Optional.
    • SET_TO_DEFAULT

      public static final NullValuePropertyMappingStrategy SET_TO_DEFAULT
      If a source bean property equals null the target bean property will be set to its default value.

      This means:

      1. For Optional MapStruct generates an Optional.empty()
      2. For List MapStruct generates an ArrayList
      3. For Map a HashMap
      4. For arrays an empty array
      5. For String ""
      6. for primitive / boxed types a representation of 0 or false
      7. For all other objects an new instance is created, requiring an empty constructor.

      Make sure that a Mapping.defaultValue() is defined if no empty constructor is available on the default value.

    • IGNORE

      public static final NullValuePropertyMappingStrategy IGNORE
      If a source bean property equals null the target bean property will be ignored and retain its existing value.
    • CLEAR

      public static final NullValuePropertyMappingStrategy CLEAR
      If a source bean property equals null the target bean property will be cleared. This strategy is only applicable to target properties that are of type Collection or Map.
  • Method Details

    • values

      public static NullValuePropertyMappingStrategy[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      public static NullValuePropertyMappingStrategy valueOf(String name)
      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (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 type has no constant with the specified name
      NullPointerException - if the argument is null