Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.util;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.apache.hivemind.ApplicationRuntimeException;
 import  org.apache.hivemind.util.Defense;
Companion to the For component, this class is an implementation of org.apache.tapestry.components.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 org.apache.tapestry.form.ListEditMap.

Author(s):
Howard M. Lewis Ship
Since:
4.0
 
 {
     private final Map _map = new HashMap();
 
     private final List _keys = new ArrayList();
 
     // The values added to the Map, in the order they were added.
     private final List _values = new ArrayList();
 
     // The last value accessed by getPrimaryKey() or getValue().
     // Other methods may operate upon this value.
 
     private Object _lastValue;
 
     private Set _deletedValues;

    
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.
 
     public void clear()
     {
         .clear();
         .clear();
         .clear();
          = null;
          = null;
     }
 
     public final void add(Object keyObject value)
     {
         Defense.notNull(key"key");
         Defense.notNull(value"value");
 
         if (.containsKey(key))
             throw new ApplicationRuntimeException(UtilMessages.keyAlreadyExists(key));
 
         .put(keyvalue);
 
         .add(key);
         .add(value);
 
          = value;
     }

    
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)
    public final List getAllValues()
    {
        return Collections.unmodifiableList();
    }

    
Returns a list of all values stored into the converter, with deleted values removed.
    public final List getValues()
    {
        if (isDeletedValuesEmpty())
            return getAllValues();
        List result = new ArrayList();
        result.removeAll();
        return result;
    }

    
Returns true if the deleted values set is empty (or null).
    private boolean isDeletedValuesEmpty()
    {
        return  == null || .isEmpty();
    }

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

    
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)
    protected final boolean checkValueSetForLastValue(Set valueSet)
    {
        return valueSet != null && valueSet.contains();
    }

    
Adds or removes the last value from the deleted values set.

Parameters:
deleted
    public final void setDeleted(boolean deleted)
    {
    }

    
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
    protected final Set updateValueSetForLastValue(Set setboolean inSet)
    {
        Set updatedSet = set;
        if (inSet)
        {
            if (updatedSet == null)
                updatedSet = new HashSet();
            updatedSet.add();
            return updatedSet;
        }
        if (updatedSet != null)
            updatedSet.remove();
        return updatedSet;
    }

    
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
    public final Object getLastValue()
    {
        return ;
    }

    
Returns an unmodifiable set of all values marked as deleted.
    public final Set getDeletedValues()
    {
        return createUnmodifiableSet();
    }

    
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
    protected final Set createUnmodifiableSet(Set valueSet)
    {
        return valueSet == null ? . : Collections.unmodifiableSet(valueSet);
    }

    
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().
    public final void removeDeletedValues()
    {
         = null;
        if (isDeletedValuesEmpty())
            return;
        int count = .size();
        for (int i = count - 1; i >= 0; i--)
        {
            if (.contains(.get(i)))
            {
                .remove(i);
                Object key = .remove(i);
                .remove(key);
            }
        }
    }

    
Gets the primary key of an object previously stored in this converter.

Parameters:
value an object previously stored in the converter
Returns:
the corresponding key used to store the object
Throws:
ApplicationRuntimeException if the value was not previously stored
See also:
add(Object, Object)
    public final Object getPrimaryKey(Object value)
    {
        int index = .indexOf(value);
        if (index < 0)
            throw new ApplicationRuntimeException(UtilMessages.valueNotFound(value), valuenull,
                    null);
         = value;
        return .get(index);
    }

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

Returns:
the object if the key is found, or null otherwise.
See also:
add(Object, Object)
    public final Object getValue(Object primaryKey)
    {
        Object result = .get(primaryKey);
        if (result == null)
            result = provideMissingValue(primaryKey);
         = result;
        return result;
    }

    
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
    protected Object provideMissingValue(Object key)
    {
        return null;
    }
New to GrepCode? Check out our FAQ X