Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 298   Methods: 13
NCLOC: 105   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ListEditMap.java 100% 100% 100% 100%
coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.form;
 16   
 
 17   
 import java.util.ArrayList;
 18   
 import java.util.HashMap;
 19   
 import java.util.HashSet;
 20   
 import java.util.List;
 21   
 import java.util.Map;
 22   
 import java.util.Set;
 23   
 
 24   
 import org.apache.tapestry.Tapestry;
 25   
 
 26   
 /**
 27   
  *  A utility class often used with the {@link org.apache.tapestry.form.ListEdit} component.
 28   
  *  A ListEditMap is loaded with data objects before the ListEdit renders, and again
 29   
  *  before the ListEdit rewinds.  This streamlines the synchronization of the form
 30   
  *  against data on the server.  It is most useful when the set of objects is of a manageable
 31   
  *  size (say, no more than a few hundred objects).
 32   
  * 
 33   
  *  <p>
 34   
  *  The map stores a list of keys, and relates each key to a value.
 35   
  *  It also tracks a deleted flag for each key.
 36   
  * 
 37   
  *  <p>
 38   
  *  Usage:
 39   
  *  <br>
 40   
  *  The page or component should implement {@link org.apache.tapestry.event.PageRenderListener}
 41   
  *  and implement {@link org.apache.tapestry.event.PageRenderListener#pageBeginRender(org.apache.tapestry.event.PageEvent)}
 42   
  *  to initialize the map.
 43   
  * 
 44   
  *  <p>
 45   
  *  The external data (from which keys and values are obtained) is queried, and
 46   
  *  each key/value pair is {@link #add(Object, Object) added} to the map, in the order
 47   
  *  that items should be presented.
 48   
  * 
 49   
  *  <p>
 50   
  *  The {@link org.apache.tapestry.form.ListEdit}'s source parameter should be bound
 51   
  *  to the map's {@link #getKeys() keys} property.  The value parameter
 52   
  *  should be bound to the map's {@link #setKey(Object) key} property.
 53   
  * 
 54   
  *  <p>
 55   
  *  The {@link org.apache.tapestry.form.ListEdit}'s listener parameter should be 
 56   
  *  bound to a listener method to synchronize a property of the component from
 57   
  *  the map.
 58   
  * 
 59   
  *  <code>
 60   
  *  public void synchronize({@link org.apache.tapestry.IRequestCycle} cycle)
 61   
  *  {
 62   
  *     ListEditMap map = ...;
 63   
  *     <i>Type</i> object = (<i>Type</i>)map.getValue();
 64   
  * 
 65   
  *     if (object == null)
 66   
  *       ...
 67   
  * 
 68   
  *     set<i>Property</i>(object);
 69   
  *  }
 70   
  *  </code>
 71   
  * 
 72   
  *  <p>
 73   
  *  You may also connect a {@link org.apache.tapestry.form.Checkbox}'s
 74   
  *  selected parameter to the map's {@link #isDeleted() deleted} property.
 75   
  *  
 76   
  *  <p>
 77   
  *  You may track inclusion in other sets by subclasses and implementing
 78   
  *  new boolean properties.  The accessor method should be a call to
 79   
  *  {@link #checkSet(Set)} and the mutator method should be a call
 80   
  *  to {@link #updateSet(Set, boolean)}.
 81   
  * 
 82   
  *  @author Howard Lewis Ship
 83   
  *  @since 3.0
 84   
  *
 85   
  **/
 86   
 
 87   
 public class ListEditMap
 88   
 {
 89   
     private Map _map = new HashMap();
 90   
     private List _keys = new ArrayList();
 91   
     private Set _deletedKeys;
 92   
     private Object _currentKey;
 93   
 
 94   
     /**
 95   
      *  Records the key and value into this map.
 96   
      *  The keys may be obtained, in the order in which they are added,
 97   
      *  using {@link #getKeys()}.
 98   
      *  This also sets the current key (so that you
 99   
      *  may invoke {@link #setDeleted(boolean)}, for example).
 100   
      * 
 101   
      **/
 102   
 
 103  21
     public void add(Object key, Object value)
 104   
     {
 105  21
         _currentKey = key;
 106   
         
 107  21
         _keys.add(_currentKey);
 108  21
         _map.put(_currentKey, value);
 109   
     }
 110   
 
 111   
     /**
 112   
      *  Returns a List of keys, in the order that keys
 113   
      *  were added to the map (using {@link #add(Object, Object)}.
 114   
      *  The caller must not modify the List.
 115   
      * 
 116   
      **/
 117   
 
 118  1
     public List getKeys()
 119   
     {
 120  1
         return _keys;
 121   
     }
 122   
 
 123   
     /**
 124   
      *  Sets the key for the map.  This defines the key used
 125   
      *  with the other methods: {@link #getValue()}, 
 126   
      *  {@link #isDeleted()}, {@link #setDeleted(boolean)}.
 127   
      * 
 128   
      **/
 129   
 
 130  8
     public void setKey(Object key)
 131   
     {
 132  8
         _currentKey = key;
 133   
     }
 134   
 
 135   
     /**
 136   
      *  Returns the current key within the map.
 137   
      * 
 138   
      **/
 139   
 
 140  2
     public Object getKey()
 141   
     {
 142  2
         return _currentKey;
 143   
     }
 144   
 
 145   
     /**
 146   
      *  Returns the value for the key (set using {@link #setKey(Object)}).
 147   
      *  May return null if no such key has been added (this can occur
 148   
      *  if a data object is deleted between the time a form is rendered
 149   
      *  and the time a form is submitted).
 150   
      * 
 151   
      **/
 152   
 
 153  2
     public Object getValue()
 154   
     {
 155  2
         return _map.get(_currentKey);
 156   
     }
 157   
 
 158   
     /**
 159   
      *  Returns true if the {@link #setKey(Object) current key}
 160   
      *  is in the set of deleted keys.
 161   
      * 
 162   
      **/
 163   
 
 164  6
     public boolean isDeleted()
 165   
     {
 166  6
         return checkSet(_deletedKeys);
 167   
     }
 168   
 
 169   
     /**
 170   
      *  Returns true if the set contains the
 171   
      *  {@link #getKey() current key}.  Returns 
 172   
      *  false is the set is null, or doesn't contain
 173   
      *  the current key.
 174   
      * 
 175   
      **/
 176   
 
 177  6
     protected boolean checkSet(Set set)
 178   
     {
 179  6
         if (set == null)
 180  3
             return false;
 181   
 
 182  3
         return set.contains(_currentKey);
 183   
     }
 184   
 
 185   
     /**
 186   
      *  Adds or removes the {@link #setKey(Object) current key}
 187   
      *  from the set of deleted keys.
 188   
      * 
 189   
      **/
 190   
 
 191  7
     public void setDeleted(boolean value)
 192   
     {
 193  7
         _deletedKeys = updateSet(_deletedKeys, value);
 194   
     }
 195   
 
 196   
     /**
 197   
      *  Updates the set, adding or removing the {@link #getKey() current key}
 198   
      *  from it.  Returns the set passed in.
 199   
      *  If the value is true and the set is null, an new instance
 200   
      *  of {@link HashSet} is created and returned.
 201   
      * 
 202   
      **/
 203   
 
 204  7
     protected Set updateSet(Set set, boolean value)
 205   
     {
 206  7
         if (value)
 207   
         {
 208  5
             if (set == null)
 209  3
                 set = new HashSet();
 210   
 
 211  5
             set.add(_currentKey);
 212   
         }
 213   
         else
 214   
         {
 215  2
             if (set != null)
 216  1
                 set.remove(_currentKey);
 217   
         }
 218   
 
 219  7
         return set;
 220   
     }
 221   
 
 222   
     /**
 223   
      *  Returns the deleted keys in an unspecified order.  May return
 224   
      *  null if no keys are deleted.
 225   
      * 
 226   
      **/
 227   
 
 228  2
     public List getDeletedKeys()
 229   
     {
 230  2
         return convertSetToList(_deletedKeys);
 231   
     }
 232   
     
 233  2
     protected List convertSetToList(Set set)
 234   
     {
 235  2
         if (Tapestry.isEmpty(set))
 236  1
             return null;
 237   
 
 238  1
         return new ArrayList(set);
 239   
     }
 240   
 
 241   
     /**
 242   
      *  Returns all the values stored in the map, in the
 243   
      *  order in which values were added to the map
 244   
      *  using {@link #add(Object, Object)}.
 245   
      * 
 246   
      **/
 247   
 
 248  3
     public List getAllValues()
 249   
     {
 250  3
         int count = _keys.size();
 251  3
         List result = new ArrayList(count);
 252   
 
 253  3
         for (int i = 0; i < count; i++)
 254   
         {
 255  9
             Object key = _keys.get(i);
 256  9
             Object value = _map.get(key);
 257   
 
 258  9
             result.add(value);
 259   
         }
 260   
 
 261  3
         return result;
 262   
     }
 263   
 
 264   
     /**
 265   
       *  Returns all the values stored in the map, excluding
 266   
       *  those whose id has been marked deleted, in the
 267   
       *  order in which values were added to the map
 268   
       *  using {@link #add(Object, Object)}.
 269   
       * 
 270   
       **/
 271   
 
 272  4
     public List getValues()
 273   
     {
 274  4
         int deletedCount = Tapestry.size(_deletedKeys);
 275   
 
 276  4
         if (deletedCount == 0)
 277  1
             return getAllValues();
 278   
 
 279  3
         int count = _keys.size();
 280   
 
 281  3
         List result = new ArrayList(count - deletedCount);
 282   
 
 283  3
         for (int i = 0; i < count; i++)
 284   
         {
 285  9
             Object key = _keys.get(i);
 286   
 
 287  9
             if (_deletedKeys.contains(key))
 288  4
                 continue;
 289   
 
 290  5
             Object value = _map.get(key);
 291  5
             result.add(value);
 292   
         }
 293   
 
 294  3
         return result;
 295   
     }
 296   
 
 297   
 }
 298