org.apache.tapestry.util
Class DefaultPrimaryKeyConverter

java.lang.Object
  extended by org.apache.tapestry.util.DefaultPrimaryKeyConverter
All Implemented Interfaces:
IPrimaryKeyConverter

public class DefaultPrimaryKeyConverter
extends Object
implements IPrimaryKeyConverter

Companion to the For component, this class is an implementation of IPrimaryKeyConverter that performs some additional handling, such as tracking value sets..

Value sets are sets of value objects maintained by the converter; the converter will provide a synthetic read/write boolean property that indicates if the last value is or is not in the set.

A single built-in value set, isDeleted() has a special purpose; it controls whether or not values are returned from getValues(). Subclasses may add additional synthetic boolean properties and additional sets.

Why not just store a boolean property in the object itself? Well, deleted is a good example of a property that is meaningful in the context of an operation, but isn't stored ... once an object is deleted (from secondary storage, such as a database) there's no place to store such a flag. The DefaultPrimaryKey converter is used in this context to store transient, operation data ... such as which values are to be deleted.

This class can be thought of as a successor to ListEditMap.

Since:
4.0
Author:
Howard M. Lewis Ship

Constructor Summary
DefaultPrimaryKeyConverter()
           
 
Method Summary
 void add(Object key, Object value)
           
protected  boolean checkValueSetForLastValue(Set valueSet)
          Checks the set to see if it contains the last value.
 void clear()
          Clears all properties of the converter, returning it to a pristine state.
protected  Set createUnmodifiableSet(Set valueSet)
          Converts a value set into a returnable value; null is converted to the empty set, and non-null is wrapped as unmodifiable.
 List getAllValues()
          Returns a unmodifiable list of values stored into the converter, in the order in which they were stored.
 Set getDeletedValues()
          Returns an unmodifiable set of all values marked as deleted.
 Object getLastValue()
          Returns the last active value; this is the value passed to getPrimaryKey(Object) or the value for the key passed to getValue(Object).
 Object getPrimaryKey(Object value)
          Gets the primary key of an object previously stored in this converter.
 Object getValue(Object primaryKey)
          Given a primary key, locates the corresponding object.
 List getValues()
          Returns a list of all values stored into the converter, with deleted values removed.
 boolean isDeleted()
          Checks to see if the last value is, or is not, in the set of deleted values.
protected  Object provideMissingValue(Object key)
          Invoked by getValue(Object) when the key is not found in the converter's map.
 void removeDeletedValues()
          Iterates over the keys and values, removing any values (and corresponding keys) that that are in the deleted set.
 void setDeleted(boolean deleted)
          Adds or removes the last value from the deleted values set.
protected  Set updateValueSetForLastValue(Set set, boolean inSet)
          Updates a value set to add or remove the last value to the set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultPrimaryKeyConverter

public DefaultPrimaryKeyConverter()
Method Detail

clear

public void clear()
Clears all properties of the converter, returning it to a pristine state. Subclasses should invoke this implementation in addition to clearing any of their own state.


add

public final void add(Object key,
                      Object value)

getAllValues

public final List getAllValues()
Returns a unmodifiable list of values stored into the converter, in the order in which they were stored.

Returns:
an unmodifiable List
See Also:
add(Object, Object)

getValues

public final List getValues()
Returns a list of all values stored into the converter, with deleted values removed.


isDeleted

public final boolean isDeleted()
Checks to see if the last value is, or is not, in the set of deleted values.


checkValueSetForLastValue

protected final boolean checkValueSetForLastValue(Set valueSet)
Checks the set to see if it contains the last value.

Parameters:
valueSet - the set to check, which may be null
Returns:
true if the last value is in the set (if non-null)

setDeleted

public final void setDeleted(boolean deleted)
Adds or removes the last value from the deleted values set.

Parameters:
deleted -

updateValueSetForLastValue

protected final Set updateValueSetForLastValue(Set set,
                                               boolean inSet)
Updates a value set to add or remove the last value to the set. The logic here will create and return a new Set instance if necessary (that is, if inSet is true and set is null). The point is to defer the creation of the set until its actually needed.

Parameters:
set - the set to update, which may be null
inSet - if true, the last value will be added to the set (creating the set as necessary); if false, the last value will be removed
Returns:
the set passed in, or a new Set instance

getLastValue

public final Object getLastValue()
Returns the last active value; this is the value passed to getPrimaryKey(Object) or the value for the key passed to getValue(Object).

Maintaining value sets involves adding or removing the active value from a set.

Returns:
the last active object

getDeletedValues

public final Set getDeletedValues()
Returns an unmodifiable set of all values marked as deleted.


createUnmodifiableSet

protected final Set createUnmodifiableSet(Set valueSet)
Converts a value set into a returnable value; null is converted to the empty set, and non-null is wrapped as unmodifiable.

Parameters:
valueSet - the set to convert and return
Returns:
a non-null, non-modifiable Set

removeDeletedValues

public final void removeDeletedValues()
Iterates over the keys and values, removing any values (and corresponding keys) that that are in the deleted set. After invoking this, getAllValues() will be the same as getValues().


getPrimaryKey

public final Object getPrimaryKey(Object value)
Gets the primary key of an object previously stored in this converter.

Specified by:
getPrimaryKey in interface IPrimaryKeyConverter
Parameters:
value - an object previously stored in the converter
Returns:
the corresponding key used to store the object
Throws:
org.apache.hivemind.ApplicationRuntimeException - if the value was not previously stored
See Also:
add(Object, Object)

getValue

public final Object getValue(Object primaryKey)
Given a primary key, locates the corresponding object. May invoke provideMissingValue(Object) if no such key has been stored into the converter.

Specified by:
getValue in interface IPrimaryKeyConverter
Returns:
the object if the key is found, or null otherwise.
See Also:
add(Object, Object)

provideMissingValue

protected Object provideMissingValue(Object key)
Invoked by getValue(Object) when the key is not found in the converter's map. Subclasses may override this method to either obtain the corresponding object from secondary storage, to throw an exception, or to provide a new object instance. This implementation returns null.

Parameters:
key - the key for which an object was requested
Returns:
the object for the key, or null if no object may can be provided


Copyright © 2006-2008 Apache Software Foundation. All Rights Reserved.