Clover coverage report - Code Coverage for hivemind release 1.1-alpha-3
Coverage timestamp: Tue Mar 22 2005 09:10:26 EST
file stats: LOC: 233   Methods: 12
NCLOC: 107   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, 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.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   
 import java.util.StringTokenizer;
 24   
 
 25   
 import org.apache.hivemind.ApplicationRuntimeException;
 26   
 
 27   
 /**
 28   
  * Provides access to an object (of a particular class) as a set of individual property that may be
 29   
  * read or updated.
 30   
  * 
 31   
  * @author Howard Lewis Ship
 32   
  */
 33   
 class ClassAdaptor
 34   
 {
 35   
     private final Map _propertyAdaptorMap = new HashMap();
 36   
 
 37  935
     ClassAdaptor(PropertyDescriptor[] properties)
 38   
     {
 39  935
         for (int i = 0; i < properties.length; i++)
 40   
         {
 41  3691
             PropertyDescriptor d = properties[i];
 42   
 
 43  3691
             String name = d.getName();
 44   
 
 45  3691
             _propertyAdaptorMap.put(name, new PropertyAdaptor(name, d.getPropertyType(), d
 46   
                     .getReadMethod(), d.getWriteMethod()));
 47   
         }
 48   
     }
 49   
 
 50   
     /**
 51   
      * Updates the property of the target object.
 52   
      * 
 53   
      * @param target
 54   
      *            the object to update
 55   
      * @param value
 56   
      *            the value to be stored into the target object property
 57   
      */
 58  6086
     public void write(Object target, String propertyName, Object value)
 59   
     {
 60  6086
         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 61   
 
 62  6086
         a.write(target, value);
 63   
     }
 64   
 
 65   
     /**
 66   
      * An improved version of {@link #write(Object, String, Object)}that can convert a string value
 67   
      * to an appropriate property type value.
 68   
      * 
 69   
      * @since 1.1
 70   
      */
 71   
 
 72  13
     public void smartWrite(Object target, String propertyName, String value)
 73   
     {
 74  13
         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 75   
 
 76  13
         a.smartWrite(target, value);
 77   
     }
 78   
 
 79   
     /**
 80   
      * Reads the property of the target object.
 81   
      * 
 82   
      * @param target
 83   
      *            the object to read
 84   
      * @param propertyName
 85   
      *            the name of the property to read
 86   
      */
 87  9
     public Object read(Object target, String propertyName)
 88   
     {
 89  9
         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 90   
 
 91  8
         return a.read(target);
 92   
     }
 93   
 
 94   
     /**
 95   
      * Returns the type of the named property.
 96   
      * 
 97   
      * @param target
 98   
      *            the object to examine
 99   
      * @param propertyName
 100   
      *            the name of the property to check
 101   
      */
 102  6213
     public Class getPropertyType(Object target, String propertyName)
 103   
     {
 104  6213
         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 105   
 
 106  6212
         return a.getPropertyType();
 107   
     }
 108   
 
 109   
     /**
 110   
      * Returns true if the named property exists and is readable.
 111   
      */
 112   
 
 113  4
     public boolean isReadable(String propertyName)
 114   
     {
 115  4
         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 116   
 
 117  4
         return result != null && result.isReadable();
 118   
     }
 119   
 
 120   
     /**
 121   
      * Returns true if the named property exists and is writable.
 122   
      */
 123   
 
 124  2731
     public boolean isWritable(String propertyName)
 125   
     {
 126  2731
         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 127   
 
 128  2731
         return result != null && result.isWritable();
 129   
     }
 130   
 
 131  12323
     PropertyAdaptor getPropertyAdaptor(Object target, String propertyName)
 132   
     {
 133  12323
         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 134   
 
 135  12323
         if (result == null)
 136  2
             throw new ApplicationRuntimeException(
 137   
                     UtilMessages.noSuchProperty(target, propertyName), target, null, null);
 138   
 
 139  12321
         return result;
 140   
     }
 141   
 
 142   
     /**
 143   
      * Returns a List of the names of readable properties (properties with a non-null getter).
 144   
      */
 145  1
     public List getReadableProperties()
 146   
     {
 147  1
         List result = new ArrayList(_propertyAdaptorMap.size());
 148   
 
 149  1
         Iterator i = _propertyAdaptorMap.values().iterator();
 150   
 
 151  1
         while (i.hasNext())
 152   
         {
 153  3
             PropertyAdaptor a = (PropertyAdaptor) i.next();
 154   
 
 155  3
             if (a.isReadable())
 156  2
                 result.add(a.getPropertyName());
 157   
         }
 158   
 
 159  1
         return result;
 160   
     }
 161   
 
 162   
     /**
 163   
      * Returns a List of the names of readable properties (properties with a non-null setter).
 164   
      */
 165  464
     public List getWriteableProperties()
 166   
     {
 167  464
         List result = new ArrayList(_propertyAdaptorMap.size());
 168   
 
 169  464
         Iterator i = _propertyAdaptorMap.values().iterator();
 170   
 
 171  464
         while (i.hasNext())
 172   
         {
 173  1331
             PropertyAdaptor a = (PropertyAdaptor) i.next();
 174   
 
 175  1331
             if (a.isWritable())
 176  859
                 result.add(a.getPropertyName());
 177   
         }
 178   
 
 179  464
         return result;
 180   
     }
 181   
 
 182   
     /**
 183   
      * Does the grunt work for
 184   
      * {@link org.apache.hivemind.util.PropertyUtils#configureProperties(Object, String)}.
 185   
      * 
 186   
      * @since 1.1
 187   
      */
 188   
 
 189  10
     public void configureProperties(Object target, String initializer)
 190   
     {
 191  10
         StringTokenizer tokenizer = new StringTokenizer(initializer, ",");
 192   
 
 193  10
         while (tokenizer.hasMoreTokens())
 194   
         {
 195  13
             configurePropertyFromToken(target, tokenizer.nextToken());
 196   
         }
 197   
     }
 198   
 
 199   
     /**
 200   
      * The token is either:
 201   
      * <ul>
 202   
      * <li>propertyName=value</li>
 203   
      * <li>propertyName</li>
 204   
      * <li>!propertyName</li>
 205   
      * </ul>
 206   
      * The later two are for boolean properties (true and false, respectively).
 207   
      * 
 208   
      * TODO: Should we strip whitespace from around properety names?  From around
 209   
      * values?
 210   
      * @since 1.1
 211   
      */
 212  13
     private void configurePropertyFromToken(Object target, String token)
 213   
     {
 214  13
         int equalsx = token.indexOf('=');
 215   
 
 216  13
         if (equalsx > 0)
 217   
         {
 218  9
             String propertyName = token.substring(0, equalsx);
 219  9
             String value = token.substring(equalsx + 1);
 220   
 
 221  9
             smartWrite(target, propertyName, value);
 222  8
             return;
 223   
         }
 224   
 
 225  4
         boolean negate = token.startsWith("!");
 226   
 
 227  4
         String propertyName = negate ? token.substring(1) : token;
 228   
 
 229  4
         Boolean value = negate ? Boolean.FALSE : Boolean.TRUE;
 230   
 
 231  4
         write(target, propertyName, value);
 232   
     }
 233   
 }