public abstract class BaseResources extends AbstractResources
An implementing subclass must override getOptionalStringImpl(String)
for local raw string retrieval.
Constructor and Description |
---|
BaseResources(Class<?> contextClass,
Optional<Resources> parentResources)
Context class and parent resources constructor.
|
Modifier and Type | Method and Description |
---|---|
protected String |
dereferenceString(String string)
Evaluates and replaces any references in the given string.
|
protected Optional<String> |
getOptionalDereferencedString(String key)
Implementation for ultimately retrieving a string resource with resources evaluated.
|
Optional<String> |
getOptionalString(String key,
Object... arguments)
Retrieves a string resource that may not be present.
|
protected abstract Optional<String> |
getOptionalStringImpl(String key)
Implementation for ultimately retrieving a raw string resource.
|
getContextClass, getParentResources
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getBoolean, getDouble, getInt, getOptionalBoolean, getOptionalDouble, getOptionalInt, getString, hasResource, requireResource
public BaseResources(@Nonnull Class<?> contextClass, @Nonnull Optional<Resources> parentResources)
contextClass
- The context with which these resources are related; usually the class of the object requesting the resource.parentResources
- The parent resources for fallback lookup.NullPointerException
- if the given context class and/or parent resources is null
.public final Optional<String> getOptionalString(String key, Object... arguments) throws ResourceConfigurationException
Resources
TODO discuss dereferencing
If arguments are provided, the string if present will be considered a template and formatted applying the given arguments. Formatting takes place after
replacement of all internal resource references. The MessageFormat
formatting rules will be used.
key
- The resource key.arguments
- The arguments for formatting, if any.ResourceConfigurationException
- if there is a resource value stored in an invalid format.Format.format(Object)
protected final Optional<String> getOptionalDereferencedString(String key) throws ResourceConfigurationException
This method must not fall back to parent resources; only local strings must be returned.
This implementation delegates to getOptionalStringImpl(String)
.
key
- The resource key.NullPointerException
- if the given key is null
.ResourceConfigurationException
- if an expression is not in the correct format, or if no resource is associated with a key in an expression.protected abstract Optional<String> getOptionalStringImpl(String key) throws ResourceConfigurationException
This method should usually be implemented but not called directly by other classes. Callers must invoke dereferenceString(String)
on the returned
string value.
This method must not fall back to parent resources; only local strings must be returned.
key
- The resource key.NullPointerException
- if the given key is null
.ResourceConfigurationException
- if an expression is not in the correct format, or if no resource is associated with a key in an expression.@Nonnull protected String dereferenceString(@Nonnull String string)
This method does not need to be called if the underlying resources implementation already supports expression replacement.
string
- The string for which expressions should be evaluated.NullPointerException
- if the given string is null
.ResourceConfigurationException
- if an expression is not in the correct format, or if no resource is associated with a key in an expression.Copyright © 2016 GlobalMentor, Inc.. All rights reserved.