2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 231   Methods: 12
NCLOC: 107   Classes: 1
 
 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  1058 ClassAdaptor(PropertyDescriptor[] properties)
 38    {
 39  1058 for (int i = 0; i < properties.length; i++)
 40    {
 41  4134 PropertyDescriptor d = properties[i];
 42   
 43  4134 String name = d.getName();
 44   
 45  4134 _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  8375 public void write(Object target, String propertyName, Object value)
 59    {
 60  8374 PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 61   
 62  8375 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  8501 public Class getPropertyType(Object target, String propertyName)
 103    {
 104  8520 PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
 105   
 106  8518 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  4687 public boolean isWritable(String propertyName)
 125    {
 126  4687 PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 127   
 128  4687 return result != null && result.isWritable();
 129    }
 130   
 131  16917 PropertyAdaptor getPropertyAdaptor(Object target, String propertyName)
 132    {
 133  16918 PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
 134   
 135  16918 if (result == null)
 136  2 throw new ApplicationRuntimeException(
 137    UtilMessages.noSuchProperty(target, propertyName), target, null, null);
 138   
 139  16915 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  790 public List getWriteableProperties()
 166    {
 167  790 List result = new ArrayList(_propertyAdaptorMap.size());
 168   
 169  790 Iterator i = _propertyAdaptorMap.values().iterator();
 170   
 171  790 while (i.hasNext())
 172    {
 173  3030 PropertyAdaptor a = (PropertyAdaptor) i.next();
 174   
 175  3033 if (a.isWritable())
 176  2237 result.add(a.getPropertyName());
 177    }
 178   
 179  790 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    * @since 1.1
 209    */
 210  13 private void configurePropertyFromToken(Object target, String token)
 211    {
 212  13 int equalsx = token.indexOf('=');
 213   
 214  13 if (equalsx > 0)
 215    {
 216  9 String propertyName = token.substring(0, equalsx).trim();
 217  9 String value = token.substring(equalsx + 1);
 218   
 219  9 smartWrite(target, propertyName, value);
 220  8 return;
 221    }
 222   
 223  4 boolean negate = token.startsWith("!");
 224   
 225  4 String propertyName = negate ? token.substring(1) : token;
 226   
 227  4 Boolean value = negate ? Boolean.FALSE : Boolean.TRUE;
 228   
 229  4 write(target, propertyName, value);
 230    }
 231    }