Class Config<S>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Config.FieldPresence
      Indicator of whether a configuration JSON field is required.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected com.fasterxml.jackson.databind.node.ArrayNode array  
      protected ConfigApplyDelegate delegate  
      protected String key  
      protected com.fasterxml.jackson.databind.ObjectMapper mapper  
      protected com.fasterxml.jackson.databind.JsonNode node  
      protected com.fasterxml.jackson.databind.node.ObjectNode object  
      protected S subject  
    • Constructor Summary

      Constructors 
      Constructor Description
      Config()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void apply()
      Applies any configuration changes made via this configuration.
      protected Config<S> clear​(String name)
      Clears the specified property.
      protected boolean get​(String name, boolean defaultValue)
      Gets the specified property as a boolean.
      protected double get​(String name, double defaultValue)
      Gets the specified property as a double.
      protected int get​(String name, int defaultValue)
      Gets the specified property as an integer.
      protected long get​(String name, long defaultValue)
      Gets the specified property as a long.
      protected <E extends Enum<E>>
      E
      get​(String name, E defaultValue, Class<E> enumClass)
      Gets the specified property as an enum.
      protected String get​(String name, String defaultValue)
      Gets the specified property as a string.
      protected <T> List<T> getList​(String name, Function<String,​T> function)
      Gets the specified array property as a list of items.
      protected <T> List<T> getList​(String name, Function<String,​T> function, List<T> defaultValue)
      Gets the specified array property as a list of items.
      protected boolean hasField​(com.fasterxml.jackson.databind.node.ObjectNode node, String name)
      Returns true if the given node contains a field with the given name.
      protected boolean hasField​(String name)
      Returns true if this config contains a field with the given name.
      protected boolean hasFields​(com.fasterxml.jackson.databind.node.ObjectNode node, String... mandatoryFields)
      Indicates whether all specified fields are present in a particular JSON object.
      protected boolean hasFields​(String... mandatoryFields)
      Indicates whether all specified fields are present in the backing JSON.
      protected boolean hasOnlyFields​(com.fasterxml.jackson.databind.node.ObjectNode node, String... allowedFields)
      Indicates whether only the specified fields are present in a particular JSON object.
      protected boolean hasOnlyFields​(String... allowedFields)
      Indicates whether only the specified fields are present in the backing JSON.
      void init​(S subject, String key, com.fasterxml.jackson.databind.JsonNode node, com.fasterxml.jackson.databind.ObjectMapper mapper, ConfigApplyDelegate delegate)
      Initializes the configuration behaviour with necessary context.
      protected boolean isBoolean​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence)
      Indicates whether the specified field of a particular node holds a valid boolean value.
      protected boolean isBoolean​(String field, Config.FieldPresence presence)
      Indicates whether the specified field holds a valid boolean value.
      protected boolean isConnectPoint​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence)
      Indicates whether the specified field of a particular node holds a valid connect point string.
      protected boolean isConnectPoint​(String field, Config.FieldPresence presence)
      Indicates whether the specified field holds a valid connect point string.
      protected boolean isDecimal​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence, double... minMax)
      Indicates whether the specified field of a particular node holds a valid decimal number.
      protected boolean isDecimal​(String field, Config.FieldPresence presence, double... minMax)
      Indicates whether the specified field holds a valid decimal number.
      protected boolean isIntegralNumber​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence, long... minMax)
      Indicates whether the specified field of a particular node holds a valid integer.
      protected boolean isIntegralNumber​(String field, Config.FieldPresence presence, long... minMax)
      Indicates whether the specified field holds a valid integer.
      protected boolean isIpAddress​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence)
      Indicates whether the specified field of a particular node holds a valid IP address.
      protected boolean isIpAddress​(String field, Config.FieldPresence presence)
      Indicates whether the specified field holds a valid IP address.
      protected boolean isIpPrefix​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence)
      Indicates whether the specified field of a particular node holds a valid IP prefix.
      protected boolean isIpPrefix​(String field, Config.FieldPresence presence)
      Indicates whether the specified field holds a valid IP prefix.
      protected boolean isMacAddress​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence)
      Indicates whether the specified field of a particular node holds a valid MAC address.
      protected boolean isMacAddress​(String field, Config.FieldPresence presence)
      Indicates whether the specified field holds a valid MAC address.
      protected boolean isNumber​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence, long... minMax)
      Indicates whether the specified field of a particular node holds a valid number.
      protected boolean isNumber​(String field, Config.FieldPresence presence, long... minMax)
      Indicates whether the specified field holds a valid number.
      protected boolean isString​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence, String... pattern)
      Indicates whether the specified field on a particular node holds a valid string value.
      protected boolean isString​(String field, Config.FieldPresence presence, String... pattern)
      Indicates whether the specified field holds a valid string value.
      protected boolean isTpPort​(com.fasterxml.jackson.databind.node.ObjectNode objectNode, String field, Config.FieldPresence presence)
      Indicates whether the specified field of a particular node holds a valid transport layer port.
      protected boolean isTpPort​(String field, Config.FieldPresence presence)
      Indicates whether the specified field holds a valid transport layer port.
      boolean isValid()
      Indicates whether or not the backing JSON node contains valid data.
      protected boolean isValidLength​(String field, int maxLength)
      Indicates whether the specified field is of a valid length.
      String key()
      Returns the configuration key.
      com.fasterxml.jackson.databind.JsonNode node()
      Returns the JSON node that contains the configuration data.
      protected <T> Config<S> setList​(String name, Function<? super T,​String> function, List<T> value)
      Sets the specified property as an array of items in a given collection transformed into a String with supplied function.
      protected <E extends Enum>
      Config<S>
      setOrClear​(String name, E value)
      Sets the specified property as a double or clears it if null value given.
      protected Config<S> setOrClear​(String name, Boolean value)
      Sets the specified property as a boolean or clears it if null value given.
      protected Config<S> setOrClear​(String name, Double value)
      Sets the specified property as a double or clears it if null value given.
      protected Config<S> setOrClear​(String name, Integer value)
      Sets the specified property as an integer or clears it if null value given.
      protected Config<S> setOrClear​(String name, Long value)
      Sets the specified property as a long or clears it if null value given.
      protected Config<S> setOrClear​(String name, String value)
      Sets the specified property as a string or clears it if null value given.
      protected <T> Config<S> setOrClear​(String name, Collection<T> collection)
      Sets the specified property as an array of items in a given collection or clears it if null is given.
      S subject()
      Returns the specific subject to which this configuration pertains.
      String toString()  
    • Field Detail

      • subject

        protected S subject
      • node

        protected com.fasterxml.jackson.databind.JsonNode node
      • object

        protected com.fasterxml.jackson.databind.node.ObjectNode object
      • array

        protected com.fasterxml.jackson.databind.node.ArrayNode array
      • mapper

        protected com.fasterxml.jackson.databind.ObjectMapper mapper
    • Constructor Detail

      • Config

        public Config()
    • Method Detail

      • init

        public final void init​(S subject,
                               String key,
                               com.fasterxml.jackson.databind.JsonNode node,
                               com.fasterxml.jackson.databind.ObjectMapper mapper,
                               ConfigApplyDelegate delegate)
        Initializes the configuration behaviour with necessary context.
        Parameters:
        subject - configuration subject
        key - configuration key
        node - JSON node where configuration data is stored
        mapper - JSON object mapper
        delegate - delegate context, or null for detached configs.
      • isValid

        public boolean isValid()
        Indicates whether or not the backing JSON node contains valid data.

        Default implementation returns true. Subclasses are expected to override this with their own validation. Implementations are free to throw a RuntimeException if data is invalid.

        Returns:
        true if the data is valid; false otherwise
        Throws:
        RuntimeException - if configuration is invalid or completely foobar
      • subject

        public S subject()
        Returns the specific subject to which this configuration pertains.
        Returns:
        configuration subject
      • key

        public String key()
        Returns the configuration key. This is primarily aimed for use in composite JSON trees in external representations and has no bearing on the internal behaviours.
        Returns:
        configuration key
      • node

        public com.fasterxml.jackson.databind.JsonNode node()
        Returns the JSON node that contains the configuration data.
        Returns:
        JSON node backing the configuration
      • apply

        public void apply()
        Applies any configuration changes made via this configuration.

        Not effective for detached configs.

      • get

        protected String get​(String name,
                             String defaultValue)
        Gets the specified property as a string.
        Parameters:
        name - property name
        defaultValue - default value if property not set
        Returns:
        property value or default value
      • setOrClear

        protected Config<S> setOrClear​(String name,
                                       String value)
        Sets the specified property as a string or clears it if null value given.
        Parameters:
        name - property name
        value - new value or null to clear the property
        Returns:
        self
      • get

        protected boolean get​(String name,
                              boolean defaultValue)
        Gets the specified property as a boolean.
        Parameters:
        name - property name
        defaultValue - default value if property not set
        Returns:
        property value or default value
      • clear

        protected Config<S> clear​(String name)
        Clears the specified property.
        Parameters:
        name - property name
        Returns:
        self
      • setOrClear

        protected Config<S> setOrClear​(String name,
                                       Boolean value)
        Sets the specified property as a boolean or clears it if null value given.
        Parameters:
        name - property name
        value - new value or null to clear the property
        Returns:
        self
      • get

        protected int get​(String name,
                          int defaultValue)
        Gets the specified property as an integer.
        Parameters:
        name - property name
        defaultValue - default value if property not set
        Returns:
        property value or default value
      • setOrClear

        protected Config<S> setOrClear​(String name,
                                       Integer value)
        Sets the specified property as an integer or clears it if null value given.
        Parameters:
        name - property name
        value - new value or null to clear the property
        Returns:
        self
      • get

        protected long get​(String name,
                           long defaultValue)
        Gets the specified property as a long.
        Parameters:
        name - property name
        defaultValue - default value if property not set
        Returns:
        property value or default value
      • setOrClear

        protected Config<S> setOrClear​(String name,
                                       Long value)
        Sets the specified property as a long or clears it if null value given.
        Parameters:
        name - property name
        value - new value or null to clear the property
        Returns:
        self
      • get

        protected double get​(String name,
                             double defaultValue)
        Gets the specified property as a double.
        Parameters:
        name - property name
        defaultValue - default value if property not set
        Returns:
        property value or default value
      • setOrClear

        protected Config<S> setOrClear​(String name,
                                       Double value)
        Sets the specified property as a double or clears it if null value given.
        Parameters:
        name - property name
        value - new value or null to clear the property
        Returns:
        self
      • get

        protected <E extends Enum<E>> E get​(String name,
                                            E defaultValue,
                                            Class<E> enumClass)
        Gets the specified property as an enum.
        Type Parameters:
        E - type of enum
        Parameters:
        name - property name
        defaultValue - default value if property not set
        enumClass - the enum class
        Returns:
        property value or default value
      • setOrClear

        protected <E extends EnumConfig<S> setOrClear​(String name,
                                                        E value)
        Sets the specified property as a double or clears it if null value given.
        Type Parameters:
        E - type of enum
        Parameters:
        name - property name
        value - new value or null to clear the property
        Returns:
        self
      • getList

        protected <T> List<T> getList​(String name,
                                      Function<String,​T> function)
        Gets the specified array property as a list of items.
        Type Parameters:
        T - type of item
        Parameters:
        name - property name
        function - mapper from string to item
        Returns:
        list of items
      • getList

        protected <T> List<T> getList​(String name,
                                      Function<String,​T> function,
                                      List<T> defaultValue)
        Gets the specified array property as a list of items.
        Type Parameters:
        T - type of item
        Parameters:
        name - property name
        function - mapper from string to item
        defaultValue - default value if property not set
        Returns:
        list of items
      • setList

        protected <T> Config<S> setList​(String name,
                                        Function<? super T,​String> function,
                                        List<T> value)
        Sets the specified property as an array of items in a given collection transformed into a String with supplied function.
        Type Parameters:
        T - type of items
        Parameters:
        name - propertyName
        function - to transform item to a String
        value - list of items
        Returns:
        self
      • setOrClear

        protected <T> Config<S> setOrClear​(String name,
                                           Collection<T> collection)
        Sets the specified property as an array of items in a given collection or clears it if null is given.
        Type Parameters:
        T - type of items
        Parameters:
        name - propertyName
        collection - collection of items
        Returns:
        self
      • isValidLength

        protected boolean isValidLength​(String field,
                                        int maxLength)
        Indicates whether the specified field is of a valid length.
        Parameters:
        field - the field to validate
        maxLength - the maximum allowed length of the field
        Returns:
        true if the field lenth is less than the required length
      • hasField

        protected boolean hasField​(String name)
        Returns true if this config contains a field with the given name.
        Parameters:
        name - the field name
        Returns:
        true if field is present, false otherwise
      • hasField

        protected boolean hasField​(com.fasterxml.jackson.databind.node.ObjectNode node,
                                   String name)
        Returns true if the given node contains a field with the given name.
        Parameters:
        node - the node to examine
        name - the name to look for
        Returns:
        true if the node has a field with the given name, false otherwise
      • hasOnlyFields

        protected boolean hasOnlyFields​(String... allowedFields)
        Indicates whether only the specified fields are present in the backing JSON.
        Parameters:
        allowedFields - allowed field names
        Returns:
        true if only allowedFields are present; false otherwise
      • hasOnlyFields

        protected boolean hasOnlyFields​(com.fasterxml.jackson.databind.node.ObjectNode node,
                                        String... allowedFields)
        Indicates whether only the specified fields are present in a particular JSON object.
        Parameters:
        node - node whose fields to check
        allowedFields - allowed field names
        Returns:
        true if only allowedFields are present; false otherwise
      • hasFields

        protected boolean hasFields​(String... mandatoryFields)
        Indicates whether all specified fields are present in the backing JSON.
        Parameters:
        mandatoryFields - mandatory field names
        Returns:
        true if all mandatory fields are present; false otherwise
      • hasFields

        protected boolean hasFields​(com.fasterxml.jackson.databind.node.ObjectNode node,
                                    String... mandatoryFields)
        Indicates whether all specified fields are present in a particular JSON object.
        Parameters:
        node - node whose fields to check
        mandatoryFields - mandatory field names
        Returns:
        true if all mandatory fields are present; false otherwise
      • isMacAddress

        protected boolean isMacAddress​(String field,
                                       Config.FieldPresence presence)
        Indicates whether the specified field holds a valid MAC address.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isMacAddress

        protected boolean isMacAddress​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                       String field,
                                       Config.FieldPresence presence)
        Indicates whether the specified field of a particular node holds a valid MAC address.
        Parameters:
        objectNode - JSON node
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isIpAddress

        protected boolean isIpAddress​(String field,
                                      Config.FieldPresence presence)
        Indicates whether the specified field holds a valid IP address.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isIpAddress

        protected boolean isIpAddress​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                      String field,
                                      Config.FieldPresence presence)
        Indicates whether the specified field of a particular node holds a valid IP address.
        Parameters:
        objectNode - node from whom to access the field
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isIpPrefix

        protected boolean isIpPrefix​(String field,
                                     Config.FieldPresence presence)
        Indicates whether the specified field holds a valid IP prefix.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isIpPrefix

        protected boolean isIpPrefix​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                     String field,
                                     Config.FieldPresence presence)
        Indicates whether the specified field of a particular node holds a valid IP prefix.
        Parameters:
        objectNode - node from whom to access the field
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isTpPort

        protected boolean isTpPort​(String field,
                                   Config.FieldPresence presence)
        Indicates whether the specified field holds a valid transport layer port.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isTpPort

        protected boolean isTpPort​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                   String field,
                                   Config.FieldPresence presence)
        Indicates whether the specified field of a particular node holds a valid transport layer port.
        Parameters:
        objectNode - node from whom to access the field
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isConnectPoint

        protected boolean isConnectPoint​(String field,
                                         Config.FieldPresence presence)
        Indicates whether the specified field holds a valid connect point string.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isConnectPoint

        protected boolean isConnectPoint​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                         String field,
                                         Config.FieldPresence presence)
        Indicates whether the specified field of a particular node holds a valid connect point string.
        Parameters:
        objectNode - JSON node
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isString

        protected boolean isString​(String field,
                                   Config.FieldPresence presence,
                                   String... pattern)
        Indicates whether the specified field holds a valid string value.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        pattern - optional regex pattern
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isString

        protected boolean isString​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                   String field,
                                   Config.FieldPresence presence,
                                   String... pattern)
        Indicates whether the specified field on a particular node holds a valid string value.
        Parameters:
        objectNode - JSON node
        field - JSON field name
        presence - specifies if field is optional or mandatory
        pattern - optional regex pattern
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isNumber

        protected boolean isNumber​(String field,
                                   Config.FieldPresence presence,
                                   long... minMax)
        Indicates whether the specified field holds a valid number.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        minMax - optional min/max values
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isNumber

        protected boolean isNumber​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                   String field,
                                   Config.FieldPresence presence,
                                   long... minMax)
        Indicates whether the specified field of a particular node holds a valid number.
        Parameters:
        objectNode - JSON object
        field - JSON field name
        presence - specifies if field is optional or mandatory
        minMax - optional min/max values
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isIntegralNumber

        protected boolean isIntegralNumber​(String field,
                                           Config.FieldPresence presence,
                                           long... minMax)
        Indicates whether the specified field holds a valid integer.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        minMax - optional min/max values
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isIntegralNumber

        protected boolean isIntegralNumber​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                           String field,
                                           Config.FieldPresence presence,
                                           long... minMax)
        Indicates whether the specified field of a particular node holds a valid integer.
        Parameters:
        objectNode - JSON node
        field - JSON field name
        presence - specifies if field is optional or mandatory
        minMax - optional min/max values
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isDecimal

        protected boolean isDecimal​(String field,
                                    Config.FieldPresence presence,
                                    double... minMax)
        Indicates whether the specified field holds a valid decimal number.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        minMax - optional min/max values
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isDecimal

        protected boolean isDecimal​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                    String field,
                                    Config.FieldPresence presence,
                                    double... minMax)
        Indicates whether the specified field of a particular node holds a valid decimal number.
        Parameters:
        objectNode - JSON node
        field - JSON field name
        presence - specifies if field is optional or mandatory
        minMax - optional min/max values
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isBoolean

        protected boolean isBoolean​(String field,
                                    Config.FieldPresence presence)
        Indicates whether the specified field holds a valid boolean value.
        Parameters:
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid
      • isBoolean

        protected boolean isBoolean​(com.fasterxml.jackson.databind.node.ObjectNode objectNode,
                                    String field,
                                    Config.FieldPresence presence)
        Indicates whether the specified field of a particular node holds a valid boolean value.
        Parameters:
        objectNode - JSON object node
        field - JSON field name
        presence - specifies if field is optional or mandatory
        Returns:
        true if valid; false otherwise
        Throws:
        InvalidFieldException - if the field is present but not valid