Coverage Report - net.sf.ezmorph.MorphUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
MorphUtils
96%
64/67
75%
24/32
1
 
 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;
 18  
 
 19  
 import java.math.BigDecimal;
 20  
 import java.math.BigInteger;
 21  
 
 22  
 import net.sf.ezmorph.array.BooleanArrayMorpher;
 23  
 import net.sf.ezmorph.array.ByteArrayMorpher;
 24  
 import net.sf.ezmorph.array.CharArrayMorpher;
 25  
 import net.sf.ezmorph.array.DoubleArrayMorpher;
 26  
 import net.sf.ezmorph.array.FloatArrayMorpher;
 27  
 import net.sf.ezmorph.array.IntArrayMorpher;
 28  
 import net.sf.ezmorph.array.LongArrayMorpher;
 29  
 import net.sf.ezmorph.array.ObjectArrayMorpher;
 30  
 import net.sf.ezmorph.array.ShortArrayMorpher;
 31  
 import net.sf.ezmorph.object.BooleanObjectMorpher;
 32  
 import net.sf.ezmorph.object.CharacterObjectMorpher;
 33  
 import net.sf.ezmorph.object.ClassMorpher;
 34  
 import net.sf.ezmorph.object.NumberMorpher;
 35  
 import net.sf.ezmorph.object.StringMorpher;
 36  
 import net.sf.ezmorph.primitive.BooleanMorpher;
 37  
 import net.sf.ezmorph.primitive.ByteMorpher;
 38  
 import net.sf.ezmorph.primitive.CharMorpher;
 39  
 import net.sf.ezmorph.primitive.DoubleMorpher;
 40  
 import net.sf.ezmorph.primitive.FloatMorpher;
 41  
 import net.sf.ezmorph.primitive.IntMorpher;
 42  
 import net.sf.ezmorph.primitive.LongMorpher;
 43  
 import net.sf.ezmorph.primitive.ShortMorpher;
 44  
 
 45  
 /**
 46  
  * Covenient class for registering standard morphers to a ConvertRegistry.<br
 47  
  *
 48  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 49  
  */
 50  3
 public class MorphUtils
 51  
 {
 52  
    /** Constant value for BigDecimal(1) */
 53  4
    public static final BigDecimal BIGDECIMAL_ONE = new BigDecimal( "1" );
 54  
    /** Constant value for BigDecimal(0) */
 55  4
    public static final BigDecimal BIGDECIMAL_ZERO = new BigDecimal( "0" );
 56  
 
 57  
    /**
 58  
     * Clears and registers all standard morpehrs.
 59  
     *
 60  
     * @param morpherRegistry
 61  
     */
 62  
    public static void registerStandardMorphers( MorpherRegistry morpherRegistry )
 63  
    {
 64  505
       morpherRegistry.clear();
 65  505
       registerStandardPrimitiveMorphers( morpherRegistry );
 66  505
       registerStandardPrimitiveArrayMorphers( morpherRegistry );
 67  505
       registerStandardObjectMorphers( morpherRegistry );
 68  505
       registerStandardObjectArrayMorphers( morpherRegistry );
 69  505
    }
 70  
 
 71  
    /**
 72  
     * Registers morphers for arrays of wrappers and String with standard default
 73  
     * values.<br>
 74  
     * <ul>
 75  
     * <li>Boolean - Boolean.FALSE</li>
 76  
     * <li>Character - new Character('\0')</li>
 77  
     * <li>Byte - new Byte( (byte)0 )</li>
 78  
     * <li>Short - new Short( (short)0 )</li>
 79  
     * <li>Integer - new Integer( 0 )</li>
 80  
     * <li>Long - new Long( 0 )</li>
 81  
     * <li>Float - new Float( 0 )</li>
 82  
     * <li>Double - new Double( 0 )</li>
 83  
     * <li>String - null</li>
 84  
     * <li>BigInteger - BigInteger.ZERO</li>
 85  
     * <li>BigDecimal - MorphUtils.BIGDECIMAL_ZERO</li>
 86  
     * </ul>
 87  
     *
 88  
     * @param morpherRegistry
 89  
     */
 90  
    public static void registerStandardObjectArrayMorphers( MorpherRegistry morpherRegistry )
 91  
    {
 92  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new BooleanObjectMorpher(
 93  393
             Boolean.FALSE ) ) );
 94  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new CharacterObjectMorpher(
 95  393
             new Character( '\0' ) ) ) );
 96  521
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( StringMorpher.getInstance() ) );
 97  922
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Byte.class,
 98  393
             new Byte( (byte) 0 ) ) ) );
 99  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Short.class,
 100  393
             new Short( (short) 0 ) ) ) );
 101  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Integer.class,
 102  393
             new Integer( 0 ) ) ) );
 103  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Long.class,
 104  393
             new Long( 0 ) ) ) );
 105  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Float.class,
 106  393
             new Float( 0 ) ) ) );
 107  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Double.class,
 108  393
             new Double( 0 ) ) ) );
 109  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( BigInteger.class,
 110  393
             BigInteger.ZERO ) ) );
 111  914
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( BigDecimal.class,
 112  393
             MorphUtils.BIGDECIMAL_ZERO ) ) );
 113  521
       morpherRegistry.registerMorpher( new ObjectArrayMorpher( ClassMorpher.getInstance() ) );
 114  521
    }
 115  
 
 116  
    /**
 117  
     * Registers morphers for wrappers and String with standard default values.<br>
 118  
     * <ul>
 119  
     * <li>Boolean - Boolean.FALSE</li>
 120  
     * <li>Character - new Character('\0')</li>
 121  
     * <li>Byte - new Byte( (byte)0 )</li>
 122  
     * <li>Short - new Short( (short)0 )</li>
 123  
     * <li>Integer - new Integer( 0 )</li>
 124  
     * <li>Long - new Long( 0 )</li>
 125  
     * <li>Float - new Float( 0 )</li>
 126  
     * <li>Double - new Double( 0 )</li>
 127  
     * <li>String - null</li>
 128  
     * <li>BigInteger - BigInteger.ZERO</li>
 129  
     * <li>BigDecimal - MorphUtils.BIGDECIMAL_ZERO</li>
 130  
     * </ul>
 131  
     *
 132  
     * @param morpherRegistry
 133  
     */
 134  
    public static void registerStandardObjectMorphers( MorpherRegistry morpherRegistry )
 135  
    {
 136  521
       morpherRegistry.registerMorpher( new BooleanObjectMorpher( Boolean.FALSE ) );
 137  521
       morpherRegistry.registerMorpher( new CharacterObjectMorpher( new Character( '\0' ) ) );
 138  521
       morpherRegistry.registerMorpher( StringMorpher.getInstance() );
 139  521
       morpherRegistry.registerMorpher( new NumberMorpher( Byte.class, new Byte( (byte) 0 ) ) );
 140  521
       morpherRegistry.registerMorpher( new NumberMorpher( Short.class, new Short( (short) 0 ) ) );
 141  521
       morpherRegistry.registerMorpher( new NumberMorpher( Integer.class, new Integer( 0 ) ) );
 142  521
       morpherRegistry.registerMorpher( new NumberMorpher( Long.class, new Long( 0 ) ) );
 143  521
       morpherRegistry.registerMorpher( new NumberMorpher( Float.class, new Float( 0 ) ) );
 144  521
       morpherRegistry.registerMorpher( new NumberMorpher( Double.class, new Double( 0 ) ) );
 145  521
       morpherRegistry.registerMorpher( new NumberMorpher( BigInteger.class, BigInteger.ZERO ) );
 146  914
       morpherRegistry.registerMorpher( new NumberMorpher( BigDecimal.class,
 147  393
             MorphUtils.BIGDECIMAL_ZERO ) );
 148  521
       morpherRegistry.registerMorpher( ClassMorpher.getInstance() );
 149  521
    }
 150  
 
 151  
    /**
 152  
     * Registers morphers for arrays of primitives with standard default values.<br>
 153  
     * <ul>
 154  
     * <li>boolean - false</li>
 155  
     * <li>char - '\0'</li>
 156  
     * <li>byte - 0</li>
 157  
     * <li>short - 0</li>
 158  
     * <li>int - 0</li>
 159  
     * <li>long - 0</li>
 160  
     * <li>float - 0</li>
 161  
     * <li>double - 0</li>
 162  
     * </ul>
 163  
     *
 164  
     * @param morpherRegistry
 165  
     */
 166  
    public static void registerStandardPrimitiveArrayMorphers( MorpherRegistry morpherRegistry )
 167  
    {
 168  509
       morpherRegistry.registerMorpher( new BooleanArrayMorpher( false ) );
 169  509
       morpherRegistry.registerMorpher( new CharArrayMorpher( '\0' ) );
 170  509
       morpherRegistry.registerMorpher( new ByteArrayMorpher( (byte) 0 ) );
 171  509
       morpherRegistry.registerMorpher( new ShortArrayMorpher( (short) 0 ) );
 172  509
       morpherRegistry.registerMorpher( new IntArrayMorpher( 0 ) );
 173  509
       morpherRegistry.registerMorpher( new LongArrayMorpher( 0 ) );
 174  509
       morpherRegistry.registerMorpher( new FloatArrayMorpher( 0 ) );
 175  509
       morpherRegistry.registerMorpher( new DoubleArrayMorpher( 0 ) );
 176  509
    }
 177  
 
 178  
    /**
 179  
     * Registers morphers for primitives with standard default values.<br>
 180  
     * <ul>
 181  
     * <li>boolean - false</li>
 182  
     * <li>char - '\0'</li>
 183  
     * <li>byte - 0</li>
 184  
     * <li>short - 0</li>
 185  
     * <li>int - 0</li>
 186  
     * <li>long - 0</li>
 187  
     * <li>float - 0</li>
 188  
     * <li>double - 0</li>
 189  
     * </ul>
 190  
     *
 191  
     * @param morpherRegistry
 192  
     */
 193  
    public static void registerStandardPrimitiveMorphers( MorpherRegistry morpherRegistry )
 194  
    {
 195  509
       morpherRegistry.registerMorpher( new BooleanMorpher( false ) );
 196  509
       morpherRegistry.registerMorpher( new CharMorpher( '\0' ) );
 197  509
       morpherRegistry.registerMorpher( new ByteMorpher( (byte) 0 ) );
 198  509
       morpherRegistry.registerMorpher( new ShortMorpher( (short) 0 ) );
 199  509
       morpherRegistry.registerMorpher( new IntMorpher( 0 ) );
 200  509
       morpherRegistry.registerMorpher( new LongMorpher( 0 ) );
 201  509
       morpherRegistry.registerMorpher( new FloatMorpher( 0 ) );
 202  509
       morpherRegistry.registerMorpher( new DoubleMorpher( 0 ) );
 203  509
    }
 204  
 
 205  0
    private MorphUtils()
 206  0
    {
 207  
 
 208  0
    }
 209  
 }