Class CompositeValueMap

  • All Implemented Interfaces:
    Map<String,​Object>, ValueMap

    public class CompositeValueMap
    extends Object
    implements ValueMap
    An implementation of the ValueMap based on two ValueMaps: - One containing the properties - Another one containing the defaults to use in case the properties map does not contain the values. In case you would like to avoid duplicating properties on multiple resources, you can use a CompositeValueMap to get a concatenated map of properties.
    Since:
    2.3 (Sling API Bundle 2.5.0)
    • Constructor Detail

      • CompositeValueMap

        public CompositeValueMap​(ValueMap properties,
                                 ValueMap defaults)
        Constructor
        Parameters:
        properties - The ValueMap to read from
        defaults - The default ValueMap to use as fallback
      • CompositeValueMap

        public CompositeValueMap​(ValueMap properties,
                                 ValueMap defaults,
                                 boolean merge)
        Constructor
        Parameters:
        properties - The ValueMap to read from
        defaults - The default ValueMap to use as fallback
        merge - Merge flag - If true, getting a key would return the current property map's value (if available), even if the corresponding default does not exist. - If false, getting a key would return null if the corresponding default does not exist
    • Method Detail

      • get

        public <T> T get​(String key,
                         Class<T> type)
        Get a named property and convert it into the given type. This method does not support conversion into a primitive type or an array of a primitive type. It should return null in this case.
        Specified by:
        get in interface ValueMap
        Type Parameters:
        T - The class of the type
        Parameters:
        key - The name of the property
        type - The class of the type
        Returns:
        Return named value converted to type T or null if non existing or can't be converted.
      • get

        public <T> T get​(String key,
                         T defaultValue)
        Get a named property and convert it into the given type. This method does not support conversion into a primitive type or an array of a primitive type. It should return the default value in this case.

        Implementation hint: In the past it was allowed to call this with a 2nd parameter being null. Therefore all implementations should internally call Map.get(Object) when the 2nd parameter has value null.
        Specified by:
        get in interface ValueMap
        Type Parameters:
        T - The expected type
        Parameters:
        key - The name of the property
        defaultValue - The default value to use if the named property does not exist or cannot be converted to the requested type. The default value is also used to define the type to convert the value to. Must not be null. If you want to return null by default rather rely on ValueMap.get(String, Class).
        Returns:
        Return named value converted to type T or the default value if non existing or can't be converted.