Coverage Report - org.apache.tapestry.record.PersistentPropertyData
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistentPropertyData
0%
0/45
0%
0/10
1.75
 
 1  
 // Copyright 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.record;
 16  
 
 17  
 import java.util.ArrayList;
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.hivemind.util.Defense;
 24  
 
 25  
 /**
 26  
  * Stores persistent property changes concerning a single page. The data may be
 27  
  * stored as an encoded string and the PPD can turn between encoded and object
 28  
  * form.
 29  
  * 
 30  
  * @author Howard M. Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  
 public class PersistentPropertyData
 34  
 {
 35  
 
 36  
     /**
 37  
      * Keyed on {@link org.apache.tapestry.record.ChangeKey}, values are new
 38  
      * objects.
 39  
      */
 40  
 
 41  
     private Map _changes;
 42  
 
 43  
     private String _encoded;
 44  
 
 45  
     private final PersistentPropertyDataEncoder _encoder;
 46  
 
 47  
     /**
 48  
      * Creates a new data using the specified encoder. The set of page changes
 49  
      * is initially empty.
 50  
      */
 51  
 
 52  
     public PersistentPropertyData(PersistentPropertyDataEncoder encoder)
 53  0
     {
 54  0
         Defense.notNull(encoder, "encoder");
 55  
 
 56  0
         _encoder = encoder;
 57  0
         _changes = new HashMap();
 58  0
     }
 59  
 
 60  
     public String getEncoded()
 61  
     {
 62  0
         if (_encoded == null) _encoded = encode();
 63  
 
 64  0
         return _encoded;
 65  
     }
 66  
 
 67  
     public List getPageChanges()
 68  
     {
 69  0
         if (_changes == null)
 70  
         {
 71  0
             List pageChanges = _encoder.decodePageChanges(_encoded);
 72  
 
 73  0
             _changes = decode(pageChanges);
 74  
 
 75  0
             return pageChanges;
 76  
         }
 77  
 
 78  0
         return createPageChangeList();
 79  
     }
 80  
 
 81  
     public void store(String componentPath, String propertyName, Object newValue)
 82  
     {
 83  0
         Defense.notNull(propertyName, "propertyName");
 84  
 
 85  0
         if (_changes == null)
 86  0
             _changes = decode(_encoder.decodePageChanges(_encoded));
 87  
 
 88  0
         ChangeKey key = new ChangeKey(componentPath, propertyName);
 89  
 
 90  0
         _changes.put(key, newValue);
 91  
 
 92  
         // With the new (or changed) value, the encoded string is no
 93  
         // longer valid.
 94  
 
 95  0
         _encoded = null;
 96  0
     }
 97  
 
 98  
     public void storeEncoded(String encoded)
 99  
     {
 100  0
         Defense.notNull(encoded, "encoded");
 101  
 
 102  0
         _encoded = encoded;
 103  
 
 104  
         // The decoded data is no longer valid now.
 105  
 
 106  0
         _changes = null;
 107  0
     }
 108  
 
 109  
     private List createPageChangeList()
 110  
     {
 111  0
         List result = new ArrayList();
 112  
 
 113  0
         Iterator i = _changes.entrySet().iterator();
 114  
 
 115  0
         while(i.hasNext())
 116  
         {
 117  0
             Map.Entry me = (Map.Entry) i.next();
 118  
 
 119  0
             ChangeKey changeKey = (ChangeKey) me.getKey();
 120  0
             Object value = me.getValue();
 121  
 
 122  0
             PropertyChange change = new PropertyChangeImpl(changeKey.getComponentPath(), changeKey.getPropertyName(), value);
 123  
             
 124  0
             result.add(change);
 125  0
         }
 126  
 
 127  0
         return result;
 128  
     }
 129  
 
 130  
     private String encode()
 131  
     {
 132  0
         List changes = createPageChangeList();
 133  
 
 134  0
         return _encoder.encodePageChanges(changes);
 135  
     }
 136  
 
 137  
     private Map decode(List pageChanges)
 138  
     {
 139  0
         Map result = new HashMap();
 140  
 
 141  0
         Iterator i = pageChanges.iterator();
 142  0
         while(i.hasNext())
 143  
         {
 144  0
             PropertyChange pc = (PropertyChange) i.next();
 145  
 
 146  0
             String propertyName = pc.getPropertyName();
 147  0
             String componentPath = pc.getComponentPath();
 148  
 
 149  0
             ChangeKey key = new ChangeKey(componentPath, propertyName);
 150  
 
 151  0
             result.put(key, pc.getNewValue());
 152  0
         }
 153  
 
 154  0
         return result;
 155  
     }
 156  
 }