Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2007, 2009, 2010 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.tapestry5.util;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
A default, extensible version of org.apache.tapestry5.PrimaryKeyEncoder that is based on loading known values into an internal map. When there's a reasonable number (hundreds, perhaps thousands) of items to choose from, and those items are fast and cheap to read and instantiate, this implementation is a good bet. For very large result sets, you'll need to create your own implementation of org.apache.tapestry5.PrimaryKeyEncoder.

Deprecated:
See deprecation notes for org.apache.tapestry5.PrimaryKeyEncoder.
Parameters:
<K> the key type (which must be serializable)
<V> the value type
 
 public class DefaultPrimaryKeyEncoder<K extends Serializable, V> implements PrimaryKeyEncoder<K, V>
 {
     private final Map<K, V> keyToValue = new LinkedHashMap<K, V>();
 
     private final Map<V, K> valueToKey = CollectionFactory.newMap();
 
     private Set<K> deletedKeys;
 
     private K currentKey;
 
     private final Class<K> keyType;

    
Compatibility with 5.0: new encoder, key type unknown. You will want to use the other constructor and specify the key type.
 
     public DefaultPrimaryKeyEncoder()
     {
         this(null);
     }

    

Since:
5.1.0.0
 
     public DefaultPrimaryKeyEncoder(Class<K> keyType)
     {
         this. = keyType;
     }
 
 
     public Class<K> getKeyType()
     {
         return ;
     }

    
Adds a new key/value pair to the encoder.
 
     public final void add(K key, V value)
     {
         assert key != null;
         assert value != null;
         V existing = .get(key);
         if (existing != nullthrow new IllegalArgumentException(PublicUtilMessages.duplicateKey(keyvalueexisting));
 
         .put(keyvalue);
 
         // TODO: Ensure that the value is unique?
 
         .put(valuekey);
     }

    
Returns the values previously added to the encoder, in the order in which they were added. Values that are deleted are not returned.

Returns:
ordered list of values
 
     public final List<V> getValues()
     {
         return valuesNotInKeySet();
     }

    
Returns a list of all the values except those values whose keys are in the provided set. The set may be null, in which case all values are returned.

Parameters:
keySet set of keys identifying values to exclude, or null to exclude no values
Returns:
values (not in the set) in order origionally added
    protected final List<V> valuesNotInKeySet(Set<K> keySet)
    {
        if (keySet == null || keySet.isEmpty()) return getAllValues();
        List<V> result = CollectionFactory.newList();
        for (Map.Entry<K, V> entry : .entrySet())
        {
            if (keySet.contains(entry.getKey())) continue;
            result.add(entry.getValue());
        }
        return result;
    }
    public final List<V> getAllValues()
    {
        List<V> result = CollectionFactory.newList();
        for (Map.Entry<K, V> entry : .entrySet())
        {
            result.add(entry.getValue());
        }
        return result;
    }

    
For a previously added key/value pair, returns the key corresponding to the given value.
    public final K toKey(V value)
    {
        assert value != null;
         = .get(value);
        if ( == nullthrow new IllegalArgumentException(PublicUtilMessages.missingValue(value
                .keySet()));
        return ;
    }
    public final V toValue(K key)
    {
        V result = .get(key);
        if (result == null)
        {
            result = provideMissingObject(key);
             = key;
        }
        else
        {
             = key;
        }
        return result;
    }

    
Invoked by toValue(java.io.Serializable) whenever a key can not be converted to a value using the internal cache. This is an opportunity to record the fact that an error occured (they key was not valuable, possibly because it points to a deleted entity object) and provide a temporary object. This method may return null, but in a typical application, that will likely case NullPointerExceptions further down the processing chain.

This implementation returns null, and is intended to be overriden in subclasses.

Parameters:
key key for which a value is required
Returns:
a substitute value, or null
    protected V provideMissingObject(K key)
    {
        return null;
    }
    public final boolean isDeleted()
    {
        return inKeySet();
    }
    public final void setDeleted(boolean value)
    {
         = modifyKeySet(value);
    }

    
Returns true if the current key is in the provided set.

Parameters:
keySet the set of keys to check, or null
Returns:
true if the key is in the set, false if it is missing (or if keySet is null)
    protected final boolean inKeySet(Set<K> keySet)
    {
        return keySet != null && keySet.contains();
    }

    
Modifies a keySet to add or remove the current key. If necessary, a new Set is created.

Useage: private Set<K> myFlagKeys;

public boolean void setMyFlag(boolean value) { myFlagKeys = modifySet(myFlagKeys, value); }

Parameters:
keySet the set of keys, or null
value true to add the current key, false to remove
Returns:
the provided key set, or a new one
    protected final Set<K> modifyKeySet(Set<K> keySetboolean value)
    {
        if (keySet == null)
        {
            if (!valuereturn null;
            keySet = CollectionFactory.newSet();
        }
        if (valuekeySet.add();
        else keySet.remove();
        return keySet;
    }

    
Does nothing. Subclasses may override as necessary.
    public void prepareForKeys(List<K> keys)
    {
    }
New to GrepCode? Check out our FAQ X