magoffin.matt.meta.support
Class AbstractMetadataResource

java.lang.Object
  extended by magoffin.matt.meta.support.AbstractMetadataResource
All Implemented Interfaces:
MetadataResource
Direct Known Subclasses:
AbstractEnumMetadataResource

public abstract class AbstractMetadataResource
extends java.lang.Object
implements MetadataResource

Abstract base implementation of MetadataResource to help make implementing this API easier.

This class keeps a Map of List objects. Values can be added to the List instances by calling setValue(String, Object) or setValues(String, Collection). The getValue(String,Locale) method will return the first available value for the given key. The getValues(String,Locale) will return an unmodifiable List of all values (or an empty List if no values are available for that key).

The Locale parameters in getValue(String, Locale) and getValues(String, Locale) are not used by this class. Extending implementations can choose to support this as needed.

Version:
$Revision: 3 $ $Date: 2008-12-27 08:09:28 +1300 (Sat, 27 Dec 2008) $
Author:
Matt Magoffin (spamsqr@msqr.us)

Field Summary
protected  org.apache.log4j.Logger log
          A class-level logger.
 
Constructor Summary
AbstractMetadataResource()
           
 
Method Summary
protected  void addError(java.lang.String key, java.lang.String msg)
          Set an error message for a particular key.
protected  void addValue(java.lang.String key, java.lang.Object value)
          Add a value to a key.
protected  void clearValues(java.lang.String key)
          Clear all values for a key.
 java.lang.Iterable<java.lang.String> getParsedKeys()
          Get an Iterable of all successfully parsed metadata keys.
 java.util.Map<java.lang.String,java.util.List<java.lang.String>> getParseErrors()
          Get a Map of errors that occured while parsing the metadata resource.
 java.lang.Object getValue(java.lang.String key, java.util.Locale locale)
          Get a single metadata value based on an arbitrary key.
protected  java.util.List<java.lang.Object> getValueList(java.lang.String key)
          Get the List associated with a key.
 java.lang.Iterable<?> getValues(java.lang.String key, java.util.Locale locale)
          Get all available metadata values based on an arbitrary key.
protected  void setValue(java.lang.String key, java.lang.Object value)
          Set a value for a key.
protected  void setValues(java.lang.String key, java.util.Collection<?> values)
          Set a collection of values for a key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final org.apache.log4j.Logger log
A class-level logger.

Constructor Detail

AbstractMetadataResource

public AbstractMetadataResource()
Method Detail

setValue

protected final void setValue(java.lang.String key,
                              java.lang.Object value)
Set a value for a key.

This replaces any values currently associated with the key.

Parameters:
key - the key to use
value - the value to set

setValues

protected final void setValues(java.lang.String key,
                               java.util.Collection<?> values)
Set a collection of values for a key.

This replaces any values currently associated with the key.

Parameters:
key - the key to use
values - the values to set

addValue

protected final void addValue(java.lang.String key,
                              java.lang.Object value)
Add a value to a key.

Null values will not be added. Empty String values will not be added, either.

Parameters:
key - the key to add the value to
value - the value to add

clearValues

protected final void clearValues(java.lang.String key)
Clear all values for a key.

Parameters:
key - the key to clear

getValueList

protected final java.util.List<java.lang.Object> getValueList(java.lang.String key)
Get the List associated with a key.

If a non-null List is returned, this is a "live" list of values associated with this key, and changes made to the List will affect what is stored with this key.

Parameters:
key - the key
Returns:
the List associated with this key (may be null)

addError

protected final void addError(java.lang.String key,
                              java.lang.String msg)
Set an error message for a particular key.

When an error is encountered while parsing a particular metadata value, an error message can be added for that key

Parameters:
key - the key to set the error message for
msg - the error message

getValue

public java.lang.Object getValue(java.lang.String key,
                                 java.util.Locale locale)
Description copied from interface: MetadataResource
Get a single metadata value based on an arbitrary key.

Keys are implementation specific. If more than one metadata of the specified key is available, this will return the first available one.

An optional Locale parameter may be provided. Some metadata values might have Locale-specific variations. The Locale parameter may be null in which case the platform's default Locale will be used.

Specified by:
getValue in interface MetadataResource
Parameters:
key - the key
locale - a Locale
Returns:
the value, or null if not available

getValues

public java.lang.Iterable<?> getValues(java.lang.String key,
                                       java.util.Locale locale)
Description copied from interface: MetadataResource
Get all available metadata values based on an arbitrary key.

Keys are implementation specific.

An optional Locale parameter may be provided. Some metadata values might have Locale-specific variations. The Locale parameter may be null in which case the platform's default Locale will be used.

Specified by:
getValues in interface MetadataResource
Parameters:
key - the key
locale - a Locale
Returns:
iterable for all values, never null

getParseErrors

public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getParseErrors()
Description copied from interface: MetadataResource
Get a Map of errors that occured while parsing the metadata resource.

The Map has keys based on the metadata names, and the List associated with each key holds the error message(s) that occurred while processing the metadata values for that key.

If no errors occurred while processing, this will return an empty Map (never null).

Specified by:
getParseErrors in interface MetadataResource
Returns:
error map

getParsedKeys

public java.lang.Iterable<java.lang.String> getParsedKeys()
Description copied from interface: MetadataResource
Get an Iterable of all successfully parsed metadata keys.

No sorting should be implied by the returned Iterator, but implementations are free to order the results in any way they see fit.

Specified by:
getParsedKeys in interface MetadataResource
Returns:
Iterator, never null