public class CustomAttribute extends 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 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. |
Object |
get()
Same as
get(Environment) , but uses Environment.getCurrentEnvironment() to fill the 2nd argument. |
Object |
get(Configuration cfg)
Gets the value of a
Configuration -scope attribute from the given Configuration . |
Object |
get(Environment env)
Gets the attribute from the appropriate scope that's accessible through the specified
Environment . |
Object |
get(Template template)
|
void |
set(Object value)
Same as
set(Object, Environment) , but uses Environment.getCurrentEnvironment() to fill the 2nd
argument. |
void |
set(Object value,
Configuration cfg)
Sets the value of a
Configuration -scope attribute in the given Configuration . |
void |
set(Object value,
Environment env)
Sets the attribute inside the appropriate scope that's accessible through the specified
Environment . |
void |
set(Object value,
Template template)
|
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 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 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.NullPointerException
- If env
is nullpublic final Object get()
get(Environment)
, but uses Environment.getCurrentEnvironment()
to fill the 2nd argument.IllegalStateException
- If there is no current Environment
, which is usually the case when the current thread isn't
processing a template.public final Object get(Template template)
UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_TEMPLATE
.NullPointerException
- If template
is nullpublic final Object get(Configuration cfg)
Configuration
-scope attribute from the given Configuration
.UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_CONFIGURATION
.NullPointerException
- If cfg
is nullpublic final void set(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
.NullPointerException
- If env
is nullpublic final void set(Object value)
set(Object, Environment)
, but uses Environment.getCurrentEnvironment()
to fill the 2nd
argument.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(Object value, Template template)
value
- The new value of the attribute. Can be null
.UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_TEMPLATE
.NullPointerException
- If template
is nullpublic final void set(Object value, Configuration cfg)
Configuration
-scope attribute in the given Configuration
.value
- The new value of the attribute. Can be null
.UnsupportedOperationException
- If this custom attribute has different scope than SCOPE_CONFIGURATION
.NullPointerException
- If cfg
is null