| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BooleanArrayMorpher |
|
| 4.0;4 |
| 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.array; | |
| 18 | ||
| 19 | import java.lang.reflect.Array; | |
| 20 | ||
| 21 | import net.sf.ezmorph.MorphException; | |
| 22 | import net.sf.ezmorph.primitive.BooleanMorpher; | |
| 23 | ||
| 24 | import org.apache.commons.lang.builder.EqualsBuilder; | |
| 25 | import org.apache.commons.lang.builder.HashCodeBuilder; | |
| 26 | ||
| 27 | /** | |
| 28 | * Morphs an array to a boolean[]. | |
| 29 | * | |
| 30 | * @author Andres Almiray <aalmiray@users.sourceforge.net> | |
| 31 | */ | |
| 32 | 3 | public final class BooleanArrayMorpher extends AbstractArrayMorpher |
| 33 | { | |
| 34 | 5 | private static final Class BOOLEAN_ARRAY_CLASS = boolean[].class; |
| 35 | private boolean defaultValue; | |
| 36 | ||
| 37 | public BooleanArrayMorpher() | |
| 38 | { | |
| 39 | 132 | super( false ); |
| 40 | 132 | } |
| 41 | ||
| 42 | /** | |
| 43 | * @param defaultValue return value if the value to be morphed is null | |
| 44 | */ | |
| 45 | public BooleanArrayMorpher( boolean defaultValue ) | |
| 46 | { | |
| 47 | 637 | super( true ); |
| 48 | 637 | this.defaultValue = defaultValue; |
| 49 | 637 | } |
| 50 | ||
| 51 | public boolean equals( Object obj ) | |
| 52 | { | |
| 53 | 28 | if( this == obj ){ |
| 54 | 8 | return true; |
| 55 | } | |
| 56 | 20 | if( obj == null ){ |
| 57 | 4 | return false; |
| 58 | } | |
| 59 | ||
| 60 | 16 | if( !(obj instanceof BooleanArrayMorpher) ){ |
| 61 | 4 | return false; |
| 62 | } | |
| 63 | ||
| 64 | 12 | BooleanArrayMorpher other = (BooleanArrayMorpher) obj; |
| 65 | 12 | EqualsBuilder builder = new EqualsBuilder(); |
| 66 | 12 | if( isUseDefault() && other.isUseDefault() ){ |
| 67 | 4 | builder.append( getDefaultValue(), other.getDefaultValue() ); |
| 68 | 4 | return builder.isEquals(); |
| 69 | 8 | }else if( !isUseDefault() && !other.isUseDefault() ){ |
| 70 | 4 | return builder.isEquals(); |
| 71 | }else{ | |
| 72 | 4 | return false; |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | public boolean getDefaultValue() | |
| 77 | { | |
| 78 | 20 | return defaultValue; |
| 79 | } | |
| 80 | ||
| 81 | public int hashCode() | |
| 82 | { | |
| 83 | 24 | HashCodeBuilder builder = new HashCodeBuilder(); |
| 84 | 24 | if( isUseDefault() ){ |
| 85 | 12 | builder.append( getDefaultValue() ); |
| 86 | } | |
| 87 | 24 | return builder.toHashCode(); |
| 88 | } | |
| 89 | ||
| 90 | public Object morph( Object array ) | |
| 91 | { | |
| 92 | 76 | if( array == null ){ |
| 93 | 4 | return null; |
| 94 | } | |
| 95 | ||
| 96 | 72 | if( BOOLEAN_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ |
| 97 | // no conversion needed | |
| 98 | 28 | return (boolean[]) array; |
| 99 | } | |
| 100 | ||
| 101 | 44 | if( array.getClass() |
| 102 | 33 | .isArray() ){ |
| 103 | 40 | int length = Array.getLength( array ); |
| 104 | 40 | int dims = getDimensions( array.getClass() ); |
| 105 | 40 | int[] dimensions = createDimensions( dims, length ); |
| 106 | 40 | Object result = Array.newInstance( boolean.class, dimensions ); |
| 107 | 70 | BooleanMorpher morpher = isUseDefault() ? new BooleanMorpher( defaultValue ) |
| 108 | 27 | : new BooleanMorpher(); |
| 109 | 40 | if( dims == 1 ){ |
| 110 | 72 | for( int index = 0; index < length; index++ ){ |
| 111 | 95 | Array.set( result, index, morpher.morph( Array.get( array, index ) ) ? Boolean.TRUE |
| 112 | 21 | : Boolean.FALSE ); |
| 113 | } | |
| 114 | }else{ | |
| 115 | 60 | for( int index = 0; index < length; index++ ){ |
| 116 | 40 | Array.set( result, index, morph( Array.get( array, index ) ) ); |
| 117 | } | |
| 118 | } | |
| 119 | 36 | return result; |
| 120 | }else{ | |
| 121 | 4 | throw new MorphException( "argument is not an array: " + array.getClass() ); |
| 122 | } | |
| 123 | } | |
| 124 | ||
| 125 | public Class morphsTo() | |
| 126 | { | |
| 127 | 1022 | return BOOLEAN_ARRAY_CLASS; |
| 128 | } | |
| 129 | } |