Class CustomAttribute


  • public class CustomAttribute
    extends java.lang.Object
    A class that allows one to associate custom data with a Configuration, a Template, or Environment.

    This API has similar approach to that of ThreadLocal (which allows one to associate custom data with a thread). With an example:

     // The object identity itself will serve as the attribute identifier; there's no attribute name String:
     public static final CustomAttribute MY_ATTR = new CustomAttribute(CustomAttribute.SCOPE_CONFIGURATION);
     ...
         // Set the attribute in this particular Configuration object:
         MY_ATTR.set(myAttrValue, cfg);
         ...
         // Read the attribute from this particular Configuration object:
         myAttrValue = MY_ATTR.get(cfg);
     
    • Field Detail

      • SCOPE_ENVIRONMENT

        public static final int SCOPE_ENVIRONMENT
        Constant used in the constructor specifying that this attribute is Environment-scoped.
        See Also:
        Constant Field Values
      • SCOPE_TEMPLATE

        public static final int SCOPE_TEMPLATE
        Constant used in the constructor specifying that this attribute is Template-scoped.
        See Also:
        Constant Field Values
      • SCOPE_CONFIGURATION

        public static final int SCOPE_CONFIGURATION
        Constant used in the constructor specifying that this attribute is Configuration-scoped.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CustomAttribute

        public CustomAttribute​(int scope)
        Creates a new custom attribute with the specified scope
        Parameters:
        scope - one of SCOPE_ constants.
    • Method Detail

      • create

        protected java.lang.Object create()
        This method is invoked when get() is invoked without set(Object) being invoked before it to define the value in the current scope. Override it to create the attribute value on-demand.
        Returns:
        the initial value for the custom attribute. By default returns null.
      • get

        public final java.lang.Object get​(Template template)
        Gets the value of a Template-scope attribute from the given Template.
        Throws:
        java.lang.UnsupportedOperationException - If this custom attribute has different scope than SCOPE_TEMPLATE.
        java.lang.NullPointerException - If template is null
      • get

        public final java.lang.Object get​(Configuration cfg)
        Gets the value of a Configuration-scope attribute from the given Configuration.
        Throws:
        java.lang.UnsupportedOperationException - If this custom attribute has different scope than SCOPE_CONFIGURATION.
        java.lang.NullPointerException - If cfg is null
        Since:
        2.3.22
      • set

        public final void set​(java.lang.Object value,
                              Template template)
        Sets the value of a Template-scope attribute in the given Template.
        Parameters:
        value - The new value of the attribute. Can be null.
        Throws:
        java.lang.UnsupportedOperationException - If this custom attribute has different scope than SCOPE_TEMPLATE.
        java.lang.NullPointerException - If template is null
      • set

        public final void set​(java.lang.Object value,
                              Configuration cfg)
        Sets the value of a Configuration-scope attribute in the given Configuration.
        Parameters:
        value - The new value of the attribute. Can be null.
        Throws:
        java.lang.UnsupportedOperationException - If this custom attribute has different scope than SCOPE_CONFIGURATION.
        java.lang.NullPointerException - If cfg is null
        Since:
        2.3.22