Coverage Report - net.sf.ezmorph.primitive.BooleanMorpher
 
Classes in this File Line Coverage Branch Coverage Complexity
BooleanMorpher
98%
49/50
91%
58/64
5.286
 
 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  
 
 17  
 package net.sf.ezmorph.primitive;
 18  
 
 19  
 import net.sf.ezmorph.MorphException;
 20  
 
 21  
 import org.apache.commons.lang.builder.EqualsBuilder;
 22  
 import org.apache.commons.lang.builder.HashCodeBuilder;
 23  
 
 24  
 /**
 25  
  * Morphs to a boolean.
 26  
  *
 27  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 28  
  */
 29  
 public final class BooleanMorpher extends AbstractPrimitiveMorpher
 30  
 {
 31  
    private boolean defaultValue;
 32  
 
 33  
    public BooleanMorpher()
 34  
    {
 35  228
       super();
 36  228
    }
 37  
 
 38  
    /**
 39  
     * @param defaultValue return value if the value to be morphed is null
 40  
     */
 41  
    public BooleanMorpher( boolean defaultValue )
 42  
    {
 43  661
       super( true );
 44  661
       this.defaultValue = defaultValue;
 45  661
    }
 46  
 
 47  
    public boolean equals( Object obj )
 48  
    {
 49  40
       if( this == obj ){
 50  16
          return true;
 51  
       }
 52  24
       if( obj == null ){
 53  4
          return false;
 54  
       }
 55  
 
 56  20
       if( !(obj instanceof BooleanMorpher) ){
 57  8
          return false;
 58  
       }
 59  
 
 60  12
       BooleanMorpher other = (BooleanMorpher) obj;
 61  12
       EqualsBuilder builder = new EqualsBuilder();
 62  12
       if( isUseDefault() && other.isUseDefault() ){
 63  4
          builder.append( getDefaultValue(), other.getDefaultValue() );
 64  4
          return builder.isEquals();
 65  8
       }else if( !isUseDefault() && !other.isUseDefault() ){
 66  4
          return builder.isEquals();
 67  
       }else{
 68  4
          return false;
 69  
       }
 70  
    }
 71  
 
 72  
    /**
 73  
     * Returns the default value for this Morpher.
 74  
     */
 75  
    public boolean getDefaultValue()
 76  
    {
 77  20
       return defaultValue;
 78  
    }
 79  
 
 80  
    public int hashCode()
 81  
    {
 82  24
       HashCodeBuilder builder = new HashCodeBuilder();
 83  24
       if( isUseDefault() ){
 84  12
          builder.append( getDefaultValue() );
 85  
       }
 86  24
       return builder.toHashCode();
 87  
    }
 88  
 
 89  
    /**
 90  
     * Morphs the input object into an output object of the supported type.
 91  
     *
 92  
     * @param value The input value to be morphed
 93  
     * @exception MorphException if conversion cannot be performed successfully
 94  
     */
 95  
    public boolean morph( Object value )
 96  
    {
 97  403
       if( value == null ){
 98  108
          if( isUseDefault() ){
 99  100
             return defaultValue;
 100  
          }else{
 101  8
             throw new MorphException( "value is null" );
 102  
          }
 103  
       }
 104  
 
 105  295
       if( value instanceof Boolean ){
 106  59
          return ((Boolean) value).booleanValue();
 107  236
       }else if( value instanceof Number ){
 108  88
          if( value instanceof Double
 109  24
                && (Double.isInfinite( ((Number) value).doubleValue() ) || Double.isNaN( ((Number) value).doubleValue() )) ){
 110  24
             return true;
 111  
          }
 112  64
          if( value instanceof Float
 113  6
                && (Float.isInfinite( ((Number) value).floatValue() ) || Float.isNaN( ((Number) value).floatValue() )) ){
 114  0
             return true;
 115  
          }
 116  64
          long l = ((Number) value).longValue();
 117  64
          return l != 0;
 118  
       }else{
 119  148
          String s = String.valueOf( value );
 120  
 
 121  148
          if( s.equalsIgnoreCase( "true" ) || s.equalsIgnoreCase( "yes" )
 122  81
                || s.equalsIgnoreCase( "on" ) ){
 123  60
             return true;
 124  88
          }else if( s.equalsIgnoreCase( "false" ) || s.equalsIgnoreCase( "no" )
 125  33
                || s.equalsIgnoreCase( "off" ) ){
 126  64
             return false;
 127  24
          }else if( isUseDefault() ){
 128  16
             return defaultValue;
 129  
          }
 130  
       }
 131  
 
 132  8
       throw new MorphException( "Can't morph value: " + value );
 133  
    }
 134  
 
 135  
    public Class morphsTo()
 136  
    {
 137  1038
       return Boolean.TYPE;
 138  
    }
 139  
 }