Coverage Report - org.apache.tapestry.binding.AbstractBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractBinding
0%
0/32
N/A
1.364
 
 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.binding;
 16  
 
 17  
 import org.apache.hivemind.Location;
 18  
 import org.apache.hivemind.util.Defense;
 19  
 import org.apache.tapestry.BindingException;
 20  
 import org.apache.tapestry.IBinding;
 21  
 import org.apache.tapestry.coerce.ValueConverter;
 22  
 
 23  
 /**
 24  
  * Base class for {@link IBinding}implementations.
 25  
  * 
 26  
  * @author Howard Lewis Ship
 27  
  */
 28  
 
 29  
 public abstract class AbstractBinding implements IBinding
 30  
 {
 31  
     /** @since 4.0 */
 32  
 
 33  
     protected final String _description;
 34  
 
 35  
     /** @since 4.0 */
 36  
 
 37  
     private final ValueConverter _valueConverter;
 38  
 
 39  
     /** @since 3.0 */
 40  
 
 41  
     private final Location _location;
 42  
 
 43  
     /** @since 3.0 */
 44  
 
 45  
     protected AbstractBinding(String description, ValueConverter valueConverter, Location location)
 46  0
     {
 47  0
         Defense.notNull(description, "description");
 48  0
         Defense.notNull(valueConverter, "valueConverter");
 49  
 
 50  0
         _description = description;
 51  0
         _valueConverter = valueConverter;
 52  0
         _location = location;
 53  0
     }
 54  
 
 55  
     public Location getLocation()
 56  
     {
 57  0
         return _location;
 58  
     }
 59  
 
 60  
     /**
 61  
      * Overridden in subclasses that are not invariant.
 62  
      */
 63  
 
 64  
     public void setObject(Object value)
 65  
     {
 66  0
         throw createReadOnlyBindingException(this);
 67  
     }
 68  
 
 69  
     /**
 70  
      * Default implementation: returns true.
 71  
      * 
 72  
      * @since 2.0.3
 73  
      */
 74  
 
 75  
     public boolean isInvariant()
 76  
     {
 77  0
         return true;
 78  
     }
 79  
 
 80  
     public Object getObject(Class type)
 81  
     {
 82  0
         Defense.notNull(type, "type");
 83  
 
 84  0
         Object raw = getObject();
 85  
 
 86  
         try
 87  
         {
 88  0
             return _valueConverter.coerceValue(raw, type);
 89  
         }
 90  0
         catch (Exception ex)
 91  
         {
 92  0
             String message = BindingMessages.convertObjectError(this, ex);
 93  
 
 94  0
             throw new BindingException(message, getComponent(), _location, this, ex);
 95  
         }
 96  
     }
 97  
 
 98  
     /**
 99  
      * Returns the component to which this binding is connected; this is currently only used when
 100  
      * building certain exceptions. This implementation returns null.
 101  
      *
 102  
      * @return The {@link org.apache.tapestry.IComponent} object this binding is set against.
 103  
      * @since 4.0
 104  
      */
 105  
 
 106  
     public Object getComponent()
 107  
     {
 108  0
         return null;
 109  
     }
 110  
 
 111  
     /** @since 3.0 */
 112  
 
 113  
     protected BindingException createReadOnlyBindingException(IBinding binding)
 114  
     {
 115  0
         return new BindingException(BindingMessages.readOnlyBinding(binding), binding);
 116  
     }
 117  
 
 118  
     /** @since 4.0 */
 119  
 
 120  
     public String getDescription()
 121  
     {
 122  0
         return _description;
 123  
     }
 124  
 
 125  
     /**
 126  
      * Gets the converter used to coerce binding values in to their target types.
 127  
      *
 128  
      * @return The {@link ValueConverter} being used by this binding.
 129  
      */
 130  
     public ValueConverter getValueConverter()
 131  
     {
 132  0
         return _valueConverter;
 133  
     }
 134  
 
 135  
     public String toString()
 136  
     {
 137  0
         StringBuffer buffer = new StringBuffer();
 138  0
         buffer.append(getClass().getName());
 139  0
         buffer.append("@");
 140  0
         buffer.append(Integer.toHexString(hashCode()));
 141  0
         buffer.append("[");
 142  0
         buffer.append(_description);
 143  
 
 144  0
         extendDescription(buffer);
 145  
 
 146  0
         buffer.append(", location=");
 147  0
         buffer.append(_location);
 148  0
         buffer.append("]");
 149  
 
 150  0
         return buffer.toString();
 151  
     }
 152  
 
 153  
     /**
 154  
      * Does nothing, subclasses may override to add additional information.
 155  
      */
 156  
     protected void extendDescription(StringBuffer buffer)
 157  
     {
 158  
 
 159  0
     }
 160  
 }