Coverage Report - net.sf.ezmorph.primitive.ShortMorpher
 
Classes in this File Line Coverage Branch Coverage Complexity
ShortMorpher
100%
38/38
92%
22/24
4.143
 
 1  
 /*
 2  
  * Copyright 2006-2007 the original author or authors.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package net.sf.ezmorph.primitive;
 17  
 
 18  
 import net.sf.ezmorph.MorphException;
 19  
 
 20  
 import org.apache.commons.lang.builder.EqualsBuilder;
 21  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 22  
 
 23  
 /**
 24  
  * Morphs to a short.
 25  
  *
 26  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 27  
  */
 28  
 public final class ShortMorpher extends AbstractIntegerMorpher
 29  
 {
 30  
    private short defaultValue;
 31  
 
 32  
    public ShortMorpher()
 33  
    {
 34  204
       super();
 35  204
    }
 36  
 
 37  
    /**
 38  
     * @param defaultValue return value if the value to be morphed is null
 39  
     */
 40  
    public ShortMorpher( short defaultValue )
 41  
    {
 42  689
       super( true );
 43  689
       this.defaultValue = defaultValue;
 44  689
    }
 45  
 
 46  
    public boolean equals( Object obj )
 47  
    {
 48  28
       if( this == obj ){
 49  8
          return true;
 50  
       }
 51  20
       if( obj == null ){
 52  4
          return false;
 53  
       }
 54  
 
 55  16
       if( !(obj instanceof ShortMorpher) ){
 56  4
          return false;
 57  
       }
 58  
 
 59  12
       ShortMorpher other = (ShortMorpher) obj;
 60  12
       EqualsBuilder builder = new EqualsBuilder();
 61  12
       if( isUseDefault() && other.isUseDefault() ){
 62  4
          builder.append( getDefaultValue(), other.getDefaultValue() );
 63  4
          return builder.isEquals();
 64  8
       }else if( !isUseDefault() && !other.isUseDefault() ){
 65  4
          return builder.isEquals();
 66  
       }else{
 67  4
          return false;
 68  
       }
 69  
    }
 70  
 
 71  
    /**
 72  
     * Returns the default value for this Morpher.
 73  
     */
 74  
    public short getDefaultValue()
 75  
    {
 76  20
       return defaultValue;
 77  
    }
 78  
 
 79  
    public int hashCode()
 80  
    {
 81  24
       HashCodeBuilder builder = new HashCodeBuilder();
 82  24
       if( isUseDefault() ){
 83  12
          builder.append( getDefaultValue() );
 84  
       }
 85  24
       return builder.toHashCode();
 86  
    }
 87  
 
 88  
    /**
 89  
     * Morphs the input object into an output object of the supported type.
 90  
     *
 91  
     * @param value The input value to be morphed
 92  
     * @exception MorphException if conversion cannot be performed successfully
 93  
     */
 94  
    public short morph( Object value )
 95  
    {
 96  208
       if( value == null ){
 97  108
          if( isUseDefault() ){
 98  100
             return defaultValue;
 99  
          }else{
 100  8
             throw new MorphException( "value is null" );
 101  
          }
 102  
       }
 103  
 
 104  100
       if( value instanceof Number ){
 105  16
          return ((Number) value).shortValue();
 106  
       }else{
 107  84
          short i = 0;
 108  
          try{
 109  84
             i = Short.parseShort( getIntegerValue( value ) );
 110  64
             return i;
 111  
          }
 112  20
          catch( NumberFormatException nfe ){
 113  20
             if( isUseDefault() ){
 114  16
                return defaultValue;
 115  
             }else{
 116  4
                throw new MorphException( nfe );
 117  
             }
 118  
          }
 119  
       }
 120  
    }
 121  
 
 122  
    public Class morphsTo()
 123  
    {
 124  1022
       return Short.TYPE;
 125  
    }
 126  
 }