Clover coverage report - Code Coverage for hivemind release 1.0-rc-1
Coverage timestamp: Wed Aug 25 2004 13:06:02 EDT
file stats: LOC: 169   Methods: 9
NCLOC: 87   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
ClassAdaptor.java 100% 100% 100% 100%
coverage
 1   
 //  Copyright 2004 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.hivemind.util;
 16   
 
 17   
 import java.beans.PropertyDescriptor;
 18   
 import java.util.ArrayList;
 19   
 import java.util.HashMap;
 20   
 import java.util.Iterator;
 21   
 import java.util.List;
 22   
 import java.util.Map;
 23   
 
 24   
 import org.apache.hivemind.ApplicationRuntimeException;
 25   
 
 26   
 /**
 27   
  * Provides access to an object (of a particular class) as a set of individual property
 28   
  * that may be read or updated.
 29   
  * 
 30   
  * @author Howard Lewis Ship
 31   
  */
 32   
 class ClassAdaptor
 33   
 {
 34   
     private final Map _propertyAdaptorMap = new HashMap();
 35   
 
 36  63
     ClassAdaptor(PropertyDescriptor[] properties)
 37   
     {
 38  63
         for (int i = 0; i < properties.length; i++)
 39   
         {
 40  209
             PropertyDescriptor d = properties[i];
 41   
 
 42  209
             String name = d.getName();
 43   
 
 44  209
             _propertyAdaptorMap.put(
 45   
                 name,
 46   
                 new PropertyAdaptor(
 47   
                     name,
 48   
                     d.getPropertyType(),
 49   
                     d.getReadMethod(),
 50   
                     d.getWriteMethod()));
 51   
         }
 52   
     }
 53   
 
 54   
     /**
 55   
      * Updates the property of the target object.
 56   
      * 
 57   
      * @param target the object to update
 58   
      * @param value the value to be stored into the target object property
 59   
      */
 60  6113
     public void write(Object target, String propertyName, Object value)
 61   
     {
 62  6113
         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 63   
 
 64  6113
         a.write(target, value);
 65   
     }
 66   
 
 67   
     /**
 68   
      * Reads the property of the target object.
 69   
      * 
 70   
      * @param target the object to read
 71   
      * @param propertyName the name of the property to read
 72   
      */
 73  9
     public Object read(Object target, String propertyName)
 74   
     {
 75  9
         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 76   
 
 77  8
         return a.read(target);
 78   
     }
 79   
 
 80   
     /**
 81   
      * Returns the type of the named property.
 82   
      * 
 83   
      * @param target the object to examine
 84   
      * @param propertyName the name of the property to check
 85   
      */
 86  6176
     public Class getPropertyType(Object target, String propertyName)
 87   
     {
 88  6176
         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 89   
 
 90  6175
         return a.getPropertyType();
 91   
     }
 92   
 
 93   
     /**
 94   
      * Returns true if the named property exists and is readable.
 95   
      */
 96   
 
 97  4
     public boolean isReadable(String propertyName)
 98   
     {
 99  4
         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 100   
 
 101  4
         return result != null && result.isReadable();
 102   
     }
 103   
 
 104   
     /**
 105   
      * Returns true if the named property exists and is writable.
 106   
      */
 107   
 
 108  2023
     public boolean isWritable(String propertyName)
 109   
     {
 110  2023
         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 111   
 
 112  2023
         return result != null && result.isWritable();
 113   
     }
 114   
 
 115  12300
     PropertyAdaptor getPropertyAdaptor(Object target, String propertyName)
 116   
     {
 117  12300
         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 118   
 
 119  12300
         if (result == null)
 120  2
             throw new ApplicationRuntimeException(
 121   
                 UtilMessages.noSuchProperty(target, propertyName),
 122   
                 target,
 123   
                 null,
 124   
                 null);
 125   
 
 126  12298
         return result;
 127   
     }
 128   
 
 129   
     /**
 130   
      * Returns a List of the names of readable properties (properties with a non-null getter).
 131   
      */
 132  1
     public List getReadableProperties()
 133   
     {
 134  1
         List result = new ArrayList(_propertyAdaptorMap.size());
 135   
 
 136  1
         Iterator i = _propertyAdaptorMap.values().iterator();
 137   
 
 138  1
         while (i.hasNext())
 139   
         {
 140  3
             PropertyAdaptor a = (PropertyAdaptor) i.next();
 141   
 
 142  3
             if (a.isReadable())
 143  2
                 result.add(a.getPropertyName());
 144   
         }
 145   
 
 146  1
         return result;
 147   
     }
 148   
     
 149   
     /**
 150   
      * Returns a List of the names of readable properties (properties with a non-null setter).
 151   
      */    
 152  412
     public List getWriteableProperties()
 153   
     {
 154  412
         List result = new ArrayList(_propertyAdaptorMap.size());
 155   
 
 156  412
         Iterator i = _propertyAdaptorMap.values().iterator();
 157   
 
 158  412
         while (i.hasNext())
 159   
         {
 160  1308
             PropertyAdaptor a = (PropertyAdaptor) i.next();
 161   
 
 162  1308
             if (a.isWritable())
 163  891
                 result.add(a.getPropertyName());
 164   
         }
 165   
 
 166  412
         return result;
 167   
     }    
 168   
 }
 169