| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BooleanObjectMorpher |
|
| 4.428571428571429;4.429 |
| 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.object; | |
| 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 BooleanObjectMorpher extends AbstractObjectMorpher | |
| 30 | { | |
| 31 | private Boolean defaultValue; | |
| 32 | ||
| 33 | public BooleanObjectMorpher() | |
| 34 | { | |
| 35 | 144 | super(); |
| 36 | 144 | } |
| 37 | ||
| 38 | /** | |
| 39 | * @param defaultValue return value if the value to be morphed is null | |
| 40 | */ | |
| 41 | public BooleanObjectMorpher( Boolean defaultValue ) | |
| 42 | { | |
| 43 | 1166 | super( true ); |
| 44 | 1166 | this.defaultValue = defaultValue; |
| 45 | 1166 | } |
| 46 | ||
| 47 | public boolean equals( Object obj ) | |
| 48 | { | |
| 49 | 28 | if( this == obj ){ |
| 50 | 8 | return true; |
| 51 | } | |
| 52 | 20 | if( obj == null ){ |
| 53 | 4 | return false; |
| 54 | } | |
| 55 | ||
| 56 | 16 | if( !(obj instanceof BooleanObjectMorpher) ){ |
| 57 | 4 | return false; |
| 58 | } | |
| 59 | ||
| 60 | 12 | BooleanObjectMorpher other = (BooleanObjectMorpher) 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 | public Object morph( Object value ) | |
| 90 | { | |
| 91 | 170 | if( value == null ){ |
| 92 | 111 | if( isUseDefault() ){ |
| 93 | 107 | return defaultValue; |
| 94 | }else{ | |
| 95 | 4 | throw new MorphException( "value is null" ); |
| 96 | } | |
| 97 | } | |
| 98 | ||
| 99 | 59 | if( value instanceof Boolean ){ |
| 100 | 8 | return (Boolean) value; |
| 101 | }else{ | |
| 102 | 51 | String s = String.valueOf( value ); |
| 103 | ||
| 104 | 51 | if( s.equalsIgnoreCase( "true" ) || s.equalsIgnoreCase( "yes" ) |
| 105 | 30 | || s.equalsIgnoreCase( "on" ) ){ |
| 106 | 16 | return Boolean.TRUE; |
| 107 | 35 | }else if( s.equalsIgnoreCase( "false" ) || s.equalsIgnoreCase( "no" ) |
| 108 | 12 | || s.equalsIgnoreCase( "off" ) ){ |
| 109 | 23 | return Boolean.FALSE; |
| 110 | 12 | }else if( isUseDefault() ){ |
| 111 | 8 | return defaultValue; |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | 4 | throw new MorphException( "Can't morph value: " + value ); |
| 116 | } | |
| 117 | ||
| 118 | public Class morphsTo() | |
| 119 | { | |
| 120 | 2606 | return Boolean.class; |
| 121 | } | |
| 122 | } |