public class CustomAttribute
extends java.lang.Object
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);
Modifier and Type | Field and Description |
---|---|
static int |
SCOPE_CONFIGURATION
Constant used in the constructor specifying that this attribute is
Configuration -scoped. |
static int |
SCOPE_ENVIRONMENT
Constant used in the constructor specifying that this attribute is
Environment -scoped. |
static int |
SCOPE_TEMPLATE
Constant used in the constructor specifying that this attribute is
Template -scoped. |
Constructor and Description |
---|
CustomAttribute(int scope)
Creates a new custom attribute with the specified scope
|
Modifier and Type | Method and Description |
---|---|
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. |
java.lang.Object |
get()
Same as
get(Environment) , but uses Environment.getCurrentEnvironment() to fill the 2nd argument. |
java.lang.Object |
get(Configuration cfg)
Gets the value of a
Configuration -scope attribute from the given Configuration . |
java.lang.Object |
get(Environment env)
Gets the attribute from the appropriate scope that's accessible through the specified
Environment . |
java.lang.Object |
get(Template template)
|
java.lang.Object |
get(TemplateConfiguration templateConfiguration)
Same as
get(Template) , but applies to a TemplateConfiguration . |
void |
set(java.lang.Object value)
Same as
set(Object, Environment) , but uses Environment.getCurrentEnvironment() to fill the 2nd
argument. |
void |
set(java.lang.Object value,
Configuration cfg)
Sets the value of a
Configuration -scope attribute in the given Configuration . |
void |
set(java.lang.Object value,
Environment env)
Sets the attribute inside the appropriate scope that's accessible through the specified
Environment . |
void |
set(java.lang.Object value,
Template template)
|
void |
set(java.lang.Object value,
TemplateConfiguration templateConfiguration)
Same as
set(Object, Template) , but applicable to a TemplateConfiguration . |
public static final int SCOPE_ENVIRONMENT
Environment
-scoped.public static final int SCOPE_TEMPLATE
Template
-scoped.public static final int SCOPE_CONFIGURATION
Configuration
-scoped.public CustomAttribute(int scope)
scope
- one of SCOPE_ constants.protected java.lang.Object create()
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.public final java.lang.Object get(Environment env)
Environment
. If
the attribute has SCOPE_ENVIRONMENT
scope, it will be get from the given Environment
directly.
If the attribute has SCOPE_TEMPLATE
scope, it will be get from the parent of the given
Environment
(that is, in Configurable.getParent()
) directly). If the attribute has
SCOPE_CONFIGURATION
scope, it will be get from Environment.getConfiguration()
.null
), or null
if the attribute doesn't exist.java.lang.NullPointerException
- If env
is nullpublic final java.lang.Object get()
get(Environment)
, but uses Environment.getCurrentEnvironment()
to fill the 2nd argument.java.lang.IllegalStateException
- If there is no current Environment
, which is usually the case when the current thread isn't
processing a template.public final java.lang.Object get(Template template)
java.lang.UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_TEMPLATE
.java.lang.NullPointerException
- If template
is nullpublic java.lang.Object get(TemplateConfiguration templateConfiguration)
get(Template)
, but applies to a TemplateConfiguration
.public final java.lang.Object get(Configuration cfg)
Configuration
-scope attribute from the given Configuration
.java.lang.UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_CONFIGURATION
.java.lang.NullPointerException
- If cfg
is nullpublic final void set(java.lang.Object value, Environment env)
Environment
. If
the attribute has SCOPE_ENVIRONMENT
scope, it will be set in the given Environment
directly. If
the attribute has SCOPE_TEMPLATE
scope, it will be set in the parent of the given Environment
(that is, in Configurable.getParent()
) directly). If the attribute has SCOPE_CONFIGURATION
scope,
it will be set in Environment.getConfiguration()
.value
- The new value of the attribute. Can be null
.java.lang.NullPointerException
- If env
is nullpublic final void set(java.lang.Object value)
set(Object, Environment)
, but uses Environment.getCurrentEnvironment()
to fill the 2nd
argument.java.lang.IllegalStateException
- If there is no current Environment
, which is usually the case when the current thread isn't
processing a template.public final void set(java.lang.Object value, Template template)
value
- The new value of the attribute. Can be null
.java.lang.UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_TEMPLATE
.java.lang.NullPointerException
- If template
is nullpublic final void set(java.lang.Object value, TemplateConfiguration templateConfiguration)
set(Object, Template)
, but applicable to a TemplateConfiguration
.public final void set(java.lang.Object value, Configuration cfg)
Configuration
-scope attribute in the given Configuration
.value
- The new value of the attribute. Can be null
.java.lang.UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_CONFIGURATION
.java.lang.NullPointerException
- If cfg
is null