Package org.mapstruct

Enum CollectionMappingStrategy

  • All Implemented Interfaces:
    Serializable, Comparable<CollectionMappingStrategy>

    public enum CollectionMappingStrategy
    extends Enum<CollectionMappingStrategy>
    Strategy for propagating the value of collection-typed properties from source to target.

    In the table below, the dash - indicates a property name. Next, the trailing s indicates the plural form. The table explains the options and how they are applied to the presence / absence of a set-s, add- and / or get-s method on the target object.

    Collection mapping strategy options
    Option Only target set-s Available Only target add- Available Both set-s/add- Available No set-s/add- Available Existing Target (@TargetType)
    ACCESSOR_ONLY set-s get-s set-s get-s get-s
    SETTER_PREFERRED set-s add- set-s get-s get-s
    ADDER_PREFERRED set-s add- add- get-s get-s
    TARGET_IMMUTABLE set-s exception set-s exception set-s
    Author:
    Sjaak Derksen
    • Enum Constant Detail

      • ACCESSOR_ONLY

        public static final CollectionMappingStrategy ACCESSOR_ONLY
        The setter of the target property will be used to propagate the value: orderDto.setOrderLines(order.getOrderLines).

        If no setter is available but a getter method, this will be used, under the assumption it has been initialized: orderDto.getOrderLines().addAll(order.getOrderLines). This will also be the case when using MappingTarget (updating existing instances).

      • SETTER_PREFERRED

        public static final CollectionMappingStrategy SETTER_PREFERRED
        If present, the setter of the target property will be used to propagate the value: orderDto.setOrderLines(order.getOrderLines).

        If no setter but and adder method is present, that adder will be invoked for each element of the source collection: order.addOrderLine(orderLine() ).

        If neither a setter nor an adder method but a getter for the target property is present, that getter will be used, assuming it returns an initialized collection: If no setter is available, MapStruct will first look for an adder method before resorting to a getter.

      • ADDER_PREFERRED

        public static final CollectionMappingStrategy ADDER_PREFERRED
        Identical to SETTER_PREFERRED, only that adder methods will be preferred over setter methods, if both are present for a given collection-typed property.
      • TARGET_IMMUTABLE

        public static final CollectionMappingStrategy TARGET_IMMUTABLE
        Identical to SETTER_PREFERRED, however the target collection will not be cleared and accessed via addAll in case of updating existing bean instances, see: MappingTarget. Instead the target accessor (e.g. set) will be used on the target bean to set the collection.
    • Method Detail

      • values

        public static CollectionMappingStrategy[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (CollectionMappingStrategy c : CollectionMappingStrategy.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static CollectionMappingStrategy 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