ezmorph-1.0.6/0000755000175000017500000000000011132472643013143 5ustar twernertwernerezmorph-1.0.6/META-INF/0000755000175000017500000000000011117275500014277 5ustar twernertwernerezmorph-1.0.6/META-INF/MANIFEST.MF0000644000175000017500000000015211117275476015743 0ustar twernertwernerManifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: 11.0-b16 (Sun Microsystems Inc.) ezmorph-1.0.6/net/0000755000175000017500000000000011014006420013712 5ustar twernertwernerezmorph-1.0.6/net/sf/0000755000175000017500000000000011014006420014322 5ustar twernertwernerezmorph-1.0.6/net/sf/ezmorph/0000755000175000017500000000000011014006420016006 5ustar twernertwernerezmorph-1.0.6/net/sf/ezmorph/object/0000755000175000017500000000000011014006420017254 5ustar twernertwernerezmorph-1.0.6/net/sf/ezmorph/object/CharacterObjectMorpher.java0000644000175000017500000000573010654104342024517 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a Character. * * @author Andres Almiray */ public final class CharacterObjectMorpher extends AbstractObjectMorpher { private Character defaultValue; public CharacterObjectMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public CharacterObjectMorpher( Character defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof CharacterObjectMorpher) ){ return false; } CharacterObjectMorpher other = (CharacterObjectMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public Character getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Character ){ return (Character) value; }else{ String s = String.valueOf( value ); if( s.length() > 0 ){ return new Character( s.charAt( 0 ) ); }else{ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "Can't morph value: " + value ); } } } } public Class morphsTo() { return Character.class; } }ezmorph-1.0.6/net/sf/ezmorph/object/BigIntegerMorpher.java0000644000175000017500000001053410654104342023511 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Locale; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a BigInteger. * * @author Andres Almiray */ public final class BigIntegerMorpher extends AbstractObjectMorpher { private BigInteger defaultValue; public BigIntegerMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public BigIntegerMorpher( BigInteger defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof BigIntegerMorpher) ){ return false; } BigIntegerMorpher other = (BigIntegerMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public BigInteger getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value instanceof BigInteger ){ return value; } if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ return (BigInteger) null; } } if( value instanceof Number ){ if( value instanceof Float ){ Float f = ((Float) value); if( f.isInfinite() || f.isNaN() ){ throw new MorphException( "BigInteger can not be infinite or NaN" ); } }else if( value instanceof Double ){ Double d = ((Double) value); if( d.isInfinite() || d.isNaN() ){ throw new MorphException( "BigInteger can not be infinite or NaN" ); } }else if( value instanceof BigDecimal ){ return ((BigDecimal) value).toBigInteger(); } return BigInteger.valueOf( ((Number) value).longValue() ); }else{ try{ String str = getIntegerValue( value ); if( str.length() == 0 || str.equalsIgnoreCase( "null" ) ){ return (BigInteger) null; }else{ return new BigInteger( str ); } } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return BigInteger.class; } /** * Trims the String from the begining to the first "." */ protected String getIntegerValue( Object obj ) { // use en_US Locale Locale defaultLocale = Locale.getDefault(); String str = null; try{ Locale.setDefault( Locale.US ); str = String.valueOf( obj ) .trim(); } finally{ Locale.setDefault( defaultLocale ); } int index = str.indexOf( "." ); if( index != -1 ){ str = str.substring( 0, index ); } return str; } }ezmorph-1.0.6/net/sf/ezmorph/object/ObjectListMorpher.java0000644000175000017500000000750710714150472023544 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.Morpher; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs a List to another List using a Morpher. * * @author Andres Almiray */ public final class ObjectListMorpher extends AbstractObjectMorpher { private Object defaultValue; private Morpher morpher; private Method morphMethod; /** * Creates a new ArrayMorpher which will use another Morpher for its inner * type.
* The inner morpher can not morph to an array. Multiple dimension arrays are * already handled by this class. * * @param morpher the Morpher that will handle the array's inner type. */ public ObjectListMorpher( Morpher morpher ) { setMorpher( morpher ); } public ObjectListMorpher( Morpher morpher, Object defaultValue ) { super(true); this.defaultValue = defaultValue; setMorpher( morpher ); } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof ObjectListMorpher) ){ return false; } ObjectListMorpher other = (ObjectListMorpher) obj; return morpher.equals( other.morpher ); } public int hashCode() { return new HashCodeBuilder().append( morpher ) .toHashCode(); } public Object morph( Object value ) { if( value == null ){ return null; } if( !supports( value.getClass() ) ){ throw new MorphException( value.getClass() + " is not supported" ); } List list = new ArrayList(); for( Iterator i = ((List) value).iterator(); i.hasNext(); ){ Object object = i.next(); if( object == null ){ if( isUseDefault() ){ list.add( defaultValue ); }else{ list.add( object ); } }else{ if( !morpher.supports( object.getClass() ) ){ throw new MorphException( object.getClass() + " is not supported" ); } try{ list.add( morphMethod.invoke( morpher, new Object[] { object } ) ); } catch( MorphException me ){ throw me; } catch( Exception e ){ throw new MorphException( e ); } } } return list; } public Class morphsTo() { return List.class; } public boolean supports( Class clazz ) { return clazz != null && List.class.isAssignableFrom( clazz ); } private void setMorpher( Morpher morpher ) { if( morpher == null ){ throw new IllegalArgumentException( "morpher can not be null" ); } this.morpher = morpher; // cache the morph method try{ morphMethod = morpher.getClass() .getDeclaredMethod( "morph", new Class[] { Object.class } ); } catch( NoSuchMethodException nsme ){ throw new IllegalArgumentException( nsme.getMessage() ); } } }ezmorph-1.0.6/net/sf/ezmorph/object/package.html0000644000175000017500000000022710470137600021550 0ustar twernertwerner

Morphers for Object types.

ezmorph-1.0.6/net/sf/ezmorph/object/NumberMorpher.java0000644000175000017500000002365410654104342022731 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import java.math.BigDecimal; import java.math.BigInteger; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.ByteMorpher; import net.sf.ezmorph.primitive.DoubleMorpher; import net.sf.ezmorph.primitive.FloatMorpher; import net.sf.ezmorph.primitive.IntMorpher; import net.sf.ezmorph.primitive.LongMorpher; import net.sf.ezmorph.primitive.ShortMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a subclass of Number.
* Supported types are - Byte, Short, Integer, Long, Float, BigInteger, * BigtDecimal. * * @author Andres Almiray */ public final class NumberMorpher extends AbstractObjectMorpher { private Number defaultValue; private Class type; /** * Creates a new morpher for the target type. * * @param type must be a primitive or wrapper type. BigDecimal and BigInteger * are also supported. */ public NumberMorpher( Class type ) { super( false ); if( type == null ){ throw new MorphException( "Must specify a type" ); } if( type != Byte.TYPE && type != Short.TYPE && type != Integer.TYPE && type != Long.TYPE && type != Float.TYPE && type != Double.TYPE && !Byte.class.isAssignableFrom( type ) && !Short.class.isAssignableFrom( type ) && !Integer.class.isAssignableFrom( type ) && !Long.class.isAssignableFrom( type ) && !Float.class.isAssignableFrom( type ) && !Double.class.isAssignableFrom( type ) && !BigInteger.class.isAssignableFrom( type ) && !BigDecimal.class.isAssignableFrom( type ) ){ throw new MorphException( "Must specify a Number subclass" ); } this.type = type; } /** * Creates a new morpher for the target type with a default value.
* The defaultValue should be of the same class as the target type. * * @param type must be a primitive or wrapper type. BigDecimal and BigInteger * are also supported. * @param defaultValue return value if the value to be morphed is null */ public NumberMorpher( Class type, Number defaultValue ) { super( true ); if( type == null ){ throw new MorphException( "Must specify a type" ); } if( type != Byte.TYPE && type != Short.TYPE && type != Integer.TYPE && type != Long.TYPE && type != Float.TYPE && type != Double.TYPE && !Byte.class.isAssignableFrom( type ) && !Short.class.isAssignableFrom( type ) && !Integer.class.isAssignableFrom( type ) && !Long.class.isAssignableFrom( type ) && !Float.class.isAssignableFrom( type ) && !Double.class.isAssignableFrom( type ) && !BigInteger.class.isAssignableFrom( type ) && !BigDecimal.class.isAssignableFrom( type ) ){ throw new MorphException( "Must specify a Number subclass" ); } if( defaultValue != null && !type.isInstance( defaultValue ) ){ throw new MorphException( "Default value must be of type " + type ); } this.type = type; setDefaultValue( defaultValue ); } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof NumberMorpher) ){ return false; } NumberMorpher other = (NumberMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); builder.append( type, other.type ); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public Number getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); builder.append( type ); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value != null && type.isAssignableFrom( value.getClass() ) ){ // no conversion needed return value; } String str = String.valueOf( value ) .trim(); if( !type.isPrimitive() && (value == null || str.length() == 0 || "null".equalsIgnoreCase( str )) ){ // if empty string and class != primitive treat it like null return null; } if( isDecimalNumber( type ) ){ if( Float.class.isAssignableFrom( type ) || Float.TYPE == type ){ return morphToFloat( str ); }else if( Double.class.isAssignableFrom( type ) || Double.TYPE == type ){ return morphToDouble( str ); }else{ return morphToBigDecimal( str ); } }else{ if( Byte.class.isAssignableFrom( type ) || Byte.TYPE == type ){ return morphToByte( str ); }else if( Short.class.isAssignableFrom( type ) || Short.TYPE == type ){ return morphToShort( str ); }else if( Integer.class.isAssignableFrom( type ) || Integer.TYPE == type ){ return morphToInteger( str ); }else if( Long.class.isAssignableFrom( type ) || Long.TYPE == type ){ return morphToLong( str ); }else{ return morphToBigInteger( str ); } } } public Class morphsTo() { return type; } /** * Sets the defaultValue to use if the value to be morphed is null.
* The defaultValue should be of the same class as the type this morpher * returns with morphsTo(). * * @param defaultValue return value if the value to be morphed is null */ public void setDefaultValue( Number defaultValue ) { if( defaultValue != null && !type.isInstance( defaultValue ) ){ throw new MorphException( "Default value must be of type " + type ); } this.defaultValue = defaultValue; } private boolean isDecimalNumber( Class type ) { return (Double.class.isAssignableFrom( type ) || Float.class.isAssignableFrom( type ) || Double.TYPE == type || Float.TYPE == type || BigDecimal.class.isAssignableFrom( type )); } private Object morphToBigDecimal( String str ) { Object result = null; if( isUseDefault() ){ result = new BigDecimalMorpher( (BigDecimal) defaultValue ).morph( str ); }else{ result = new BigDecimal( str ); } return result; } private Object morphToBigInteger( String str ) { Object result = null; if( isUseDefault() ){ result = new BigIntegerMorpher( (BigInteger) defaultValue ).morph( str ); }else{ result = new BigInteger( str ); } return result; } private Object morphToByte( String str ) { Object result = null; if( isUseDefault() ){ if( defaultValue == null ){ return (Byte) null; }else{ result = new Byte( new ByteMorpher( defaultValue.byteValue() ).morph( str ) ); } }else{ result = new Byte( new ByteMorpher().morph( str ) ); } return result; } private Object morphToDouble( String str ) { Object result = null; if( isUseDefault() ){ if( defaultValue == null ){ return (Double) null; }else{ result = new Double( new DoubleMorpher( defaultValue.doubleValue() ).morph( str ) ); } }else{ result = new Double( new DoubleMorpher().morph( str ) ); } return result; } private Object morphToFloat( String str ) { Object result = null; if( isUseDefault() ){ if( defaultValue == null ){ return (Float) null; }else{ result = new Float( new FloatMorpher( defaultValue.floatValue() ).morph( str ) ); } }else{ result = new Float( new FloatMorpher().morph( str ) ); } return result; } private Object morphToInteger( String str ) { Object result = null; if( isUseDefault() ){ if( defaultValue == null ){ return (Integer) null; }else{ result = new Integer( new IntMorpher( defaultValue.intValue() ).morph( str ) ); } }else{ result = new Integer( new IntMorpher().morph( str ) ); } return result; } private Object morphToLong( String str ) { Object result = null; if( isUseDefault() ){ if( defaultValue == null ){ return (Long) null; }else{ result = new Long( new LongMorpher( defaultValue.longValue() ).morph( str ) ); } }else{ result = new Long( new LongMorpher().morph( str ) ); } return result; } private Object morphToShort( String str ) { Object result = null; if( isUseDefault() ){ if( defaultValue == null ){ return (Short) null; }else{ result = new Short( new ShortMorpher( defaultValue.shortValue() ).morph( str ) ); } }else{ result = new Short( new ShortMorpher().morph( str ) ); } return result; } }ezmorph-1.0.6/net/sf/ezmorph/object/BooleanObjectMorpher.java0000644000175000017500000000616010654104342024200 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a Boolean. * * @author Andres Almiray */ public final class BooleanObjectMorpher extends AbstractObjectMorpher { private Boolean defaultValue; public BooleanObjectMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public BooleanObjectMorpher( Boolean defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof BooleanObjectMorpher) ){ return false; } BooleanObjectMorpher other = (BooleanObjectMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public Boolean getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Boolean ){ return (Boolean) value; }else{ String s = String.valueOf( value ); if( s.equalsIgnoreCase( "true" ) || s.equalsIgnoreCase( "yes" ) || s.equalsIgnoreCase( "on" ) ){ return Boolean.TRUE; }else if( s.equalsIgnoreCase( "false" ) || s.equalsIgnoreCase( "no" ) || s.equalsIgnoreCase( "off" ) ){ return Boolean.FALSE; }else if( isUseDefault() ){ return defaultValue; } } throw new MorphException( "Can't morph value: " + value ); } public Class morphsTo() { return Boolean.class; } }ezmorph-1.0.6/net/sf/ezmorph/object/MapToDateMorpher.java0000644000175000017500000001023110714150472023304 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import java.util.Calendar; import java.util.Date; import java.util.Map; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs a Map into a Date.
* The Map should have at least one of the following keys * [yer,month,day,hour,minutes,seconds,milliseconds] and the values should be * instances of Number. Any key that is not defined will have zero (0) assigned * as its value. * * @author Andres Almiray */ public class MapToDateMorpher extends AbstractObjectMorpher { private Date defaultValue; public MapToDateMorpher() { super(); } public MapToDateMorpher( Date defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof MapToDateMorpher) ){ return false; } MapToDateMorpher other = (MapToDateMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public Date getDefaultValue() { return (Date) defaultValue.clone(); } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value == null ){ return null; } if( Date.class.isAssignableFrom( value.getClass() ) ){ return (Date) value; } if( !supports( value.getClass() ) ){ throw new MorphException( value.getClass() + " is not supported" ); } Map map = (Map) value; if( map.isEmpty() ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "Unable to parse the date " + value ); } } Calendar c = Calendar.getInstance(); c.set( Calendar.YEAR, getValue( map, "year" ) ); c.set( Calendar.MONTH, getValue( map, "month" ) ); c.set( Calendar.DATE, getValue( map, "day" ) ); c.set( Calendar.HOUR_OF_DAY, getValue( map, "hour" ) ); c.set( Calendar.MINUTE, getValue( map, "minutes" ) ); c.set( Calendar.SECOND, getValue( map, "seconds" ) ); c.set( Calendar.MILLISECOND, getValue( map, "milliseconds" ) ); return c.getTime(); } public Class morphsTo() { return Date.class; } /** * Sets the defaultValue to use if the value to be morphed is null. * * @param defaultValue return value if the value to be morphed is null */ public void setDefaultValue( Date defaultValue ) { this.defaultValue = (Date) defaultValue.clone(); } public boolean supports( Class clazz ) { return clazz != null && Map.class.isAssignableFrom( clazz ); } private int getValue( Map map, String key ) { Object value = map.get( key ); if( value == null || !(value instanceof Number) ){ return 0; } Number n = (Number) value; return n.intValue(); } }ezmorph-1.0.6/net/sf/ezmorph/object/ClassMorpher.java0000644000175000017500000000346210654104342022541 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.ObjectMorpher; /** * Morphs to a Class.
* This morpher is a singleton. * * @author Andres Almiray */ public final class ClassMorpher implements ObjectMorpher { private static final ClassMorpher INSTANCE = new ClassMorpher(); /** * Returns the singleton instance */ public static ClassMorpher getInstance() { return INSTANCE; } private ClassMorpher() { } public boolean equals( Object obj ) { return INSTANCE == obj; } public int hashCode() { return 42 + getClass().hashCode(); } public Object morph( Object value ) { if( value == null ){ return null; } if( value instanceof Class ){ return (Class) value; } if( "null".equals( value ) ){ return null; } try{ return Class.forName( value.toString() ); } catch( Exception e ){ throw new MorphException( e ); } } public Class morphsTo() { return Class.class; } public boolean supports( Class clazz ) { return true; } }ezmorph-1.0.6/net/sf/ezmorph/object/DateMorpher.java0000644000175000017500000001456010654104342022352 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs a String to a Date.
*

* This morpher will iterate through the supplied formats until one succeeds or * the default value is returned (if default value is configured). *

* * @author Andres Almiray */ public final class DateMorpher extends AbstractObjectMorpher { private Date defaultValue; private String[] formats; private boolean lenient; private Locale locale; /** * @param formats a list of formats this morpher supports. */ public DateMorpher( String[] formats ) { this( formats, Locale.getDefault(), false ); } /** * @param formats a list of formats this morpher supports. * @param lenient if the parsing should be lenient or not. */ public DateMorpher( String[] formats, boolean lenient ) { this( formats, Locale.getDefault(), lenient ); } /** * @param formats a list of formats this morpher supports. * @param defaultValue return value if the value to be morphed is null. */ public DateMorpher( String[] formats, Date defaultValue ) { this( formats, defaultValue, Locale.getDefault(), false ); } /** * @param formats a list of formats this morpher supports. * @param defaultValue return value if the value to be morphed is null. * @param locale the Locale used to parse each format. * @param lenient if the parsing should be lenient or not. */ public DateMorpher( String[] formats, Date defaultValue, Locale locale, boolean lenient ) { super( true ); if( formats == null || formats.length == 0 ){ throw new MorphException( "invalid array of formats" ); } // should use defensive copying ? this.formats = formats; if( locale == null ){ this.locale = Locale.getDefault(); }else{ this.locale = locale; } this.lenient = lenient; setDefaultValue( defaultValue ); } /** * @param formats a list of formats this morpher supports. * @param locale the Locale used to parse each format. */ public DateMorpher( String[] formats, Locale locale ) { this( formats, locale, false ); } /** * @param formats a list of formats this morpher supports. * @param locale the Locale used to parse each format. * @param lenient if the parsing should be lenient or not. */ public DateMorpher( String[] formats, Locale locale, boolean lenient ) { if( formats == null || formats.length == 0 ){ throw new MorphException( "invalid array of formats" ); } // should use defensive copying ? this.formats = formats; if( locale == null ){ this.locale = Locale.getDefault(); }else{ this.locale = locale; } this.lenient = lenient; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof DateMorpher) ){ return false; } DateMorpher other = (DateMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); builder.append( formats, other.formats ); builder.append( locale, other.locale ); builder.append( lenient, other.lenient ); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public Date getDefaultValue() { return (Date) defaultValue.clone(); } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); builder.append( formats ); builder.append( locale ); builder.append( lenient ); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value == null ){ return null; } if( Date.class.isAssignableFrom( value.getClass() ) ){ return (Date) value; } if( !supports( value.getClass() ) ){ throw new MorphException( value.getClass() + " is not supported" ); } String strValue = (String) value; SimpleDateFormat dateParser = null; for( int i = 0; i < formats.length; i++ ){ if( dateParser == null ){ dateParser = new SimpleDateFormat( formats[i], locale ); }else{ dateParser.applyPattern( formats[i] ); } dateParser.setLenient( lenient ); try{ return dateParser.parse( strValue.toLowerCase() ); } catch( ParseException pe ){ // ignore exception, try the next format } } // unable to parse the date if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "Unable to parse the date " + value ); } } public Class morphsTo() { return Date.class; } /** * Sets the defaultValue to use if the value to be morphed is null. * * @param defaultValue return value if the value to be morphed is null */ public void setDefaultValue( Date defaultValue ) { this.defaultValue = (Date) defaultValue.clone(); } public boolean supports( Class clazz ) { return String.class.isAssignableFrom( clazz ); } }ezmorph-1.0.6/net/sf/ezmorph/object/AbstractObjectMorpher.java0000644000175000017500000000344410654104342024366 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import net.sf.ezmorph.ObjectMorpher; /** * Base class for ObjectMorpher implementations. * * @author Andres Almiray */ public abstract class AbstractObjectMorpher implements ObjectMorpher { private boolean useDefault; public AbstractObjectMorpher() { } /** * @param useDefault if morph() should return a default value if the value to * be morphed is null */ public AbstractObjectMorpher( boolean useDefault ) { this.useDefault = useDefault; } /** * Returns if this morpher will use a default value. */ public boolean isUseDefault() { return useDefault; } /** * Sets if this morpher will use a default value. */ public void setUseDefault( boolean useDefault ) { this.useDefault = useDefault; } /** * Returns true if the Morpher supports conversion from this Class.
* Supports any type that is not an Array. * * @param clazz the source Class * @return true if clazz is supported by this morpher, false otherwise. */ public boolean supports( Class clazz ) { return !clazz.isArray(); } }ezmorph-1.0.6/net/sf/ezmorph/object/IdentityObjectMorpher.java0000644000175000017500000000300610654104342024406 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import net.sf.ezmorph.ObjectMorpher; /** * Morpher that performs no conversion.
* This morpher is a singleton. * * @author Andres Almiray */ public final class IdentityObjectMorpher implements ObjectMorpher { private static final IdentityObjectMorpher INSTANCE = new IdentityObjectMorpher(); /** * Returns the singleton instance */ public static IdentityObjectMorpher getInstance() { return INSTANCE; } private IdentityObjectMorpher() { } public boolean equals( Object obj ) { return INSTANCE == obj; } public int hashCode() { return 42 + getClass().hashCode(); } public Object morph( Object value ) { return value; } public Class morphsTo() { return Object.class; } public boolean supports( Class clazz ) { return true; } }ezmorph-1.0.6/net/sf/ezmorph/object/StringMorpher.java0000644000175000017500000000346710654104342022747 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.ObjectMorpher; /** * Morphs to a String.
* This morpher is a singleton. * * @author Andres Almiray */ public final class StringMorpher implements ObjectMorpher { private static final StringMorpher INSTANCE = new StringMorpher(); /** * Returns the singleton instance */ public static StringMorpher getInstance() { return INSTANCE; } private StringMorpher() { } public boolean equals( Object obj ) { return INSTANCE == obj; } public int hashCode() { return 42 + getClass().hashCode(); } public Object morph( Object value ) { if( value == null ){ return null; } if( !supports( value.getClass() ) ){ throw new MorphException( "Class not supported. " + value.getClass() ); } if( String.class.isAssignableFrom( value.getClass() ) ){ return (String) value; } return String.valueOf( value ); } public Class morphsTo() { return String.class; } public boolean supports( Class clazz ) { return !clazz.isArray(); } }ezmorph-1.0.6/net/sf/ezmorph/object/SwitchingMorpher.java0000644000175000017500000000621210715155402023430 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.MorpherRegistry; import net.sf.ezmorph.ObjectMorpher; import org.apache.commons.lang.builder.HashCodeBuilder; /** * An all-purpose Morpher that can morph to several classes.
* Because this Morpher accepts any class and morphs to Object it should not be * added to a MorpherRegistry as it may be too generic for some cases and may * result in unwanted transformations. * * @author Andres Almiray */ public class SwitchingMorpher implements ObjectMorpher { private Map classMap = new HashMap(); private MorpherRegistry morpherRegistry; public SwitchingMorpher( Map classMap, MorpherRegistry morpherRegistry ) { this.morpherRegistry = morpherRegistry; if( classMap == null || classMap.isEmpty() ){ throw new MorphException( "Must specify at least one mapping" ); } this.classMap.putAll( classMap ); } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof NumberMorpher) ){ return false; } SwitchingMorpher other = (SwitchingMorpher) obj; if( classMap.size() != other.classMap.size() ){ return false; } for( Iterator entries = classMap.entrySet() .iterator(); entries.hasNext(); ){ Map.Entry entry = (Map.Entry) entries.next(); if( !other.classMap.containsKey( entry.getKey() ) ){ return false; } if( !entry.getValue() .equals( other.classMap.get( entry.getKey() ) ) ){ return false; } } return true; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); for( Iterator entries = classMap.entrySet() .iterator(); entries.hasNext(); ){ Map.Entry entry = (Map.Entry) entries.next(); builder.append( entry.getKey() ); builder.append( entry.getValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value == null ){ return null; } Class target = (Class) classMap.get( value.getClass() ); return morpherRegistry.morph( target, value ); } public Class morphsTo() { return Object.class; } public boolean supports( Class clazz ) { return true; } }ezmorph-1.0.6/net/sf/ezmorph/object/BigDecimalMorpher.java0000644000175000017500000000746110654104342023457 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.object; import java.math.BigDecimal; import java.math.BigInteger; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a BigDecimal. * * @author Andres Almiray */ public final class BigDecimalMorpher extends AbstractObjectMorpher { private BigDecimal defaultValue; public BigDecimalMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public BigDecimalMorpher( BigDecimal defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof BigDecimalMorpher) ){ return false; } BigDecimalMorpher other = (BigDecimalMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public BigDecimal getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object value ) { if( value instanceof BigDecimal ){ return value; } if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ return (BigDecimal) null; } } if( value instanceof Number ){ if( value instanceof Float ){ Float f = ((Float) value); if( f.isInfinite() || f.isNaN() ){ throw new MorphException( "BigDecimal can not be infinite or NaN" ); } }else if( value instanceof Double ){ Double d = ((Double) value); if( d.isInfinite() || d.isNaN() ){ throw new MorphException( "BigDecimal can not be infinite or NaN" ); } }else if( value instanceof BigInteger ){ return new BigDecimal( (BigInteger) value ); } return new BigDecimal( ((Number) value).doubleValue() ); }else{ try{ String str = String.valueOf( value ) .trim(); if( str.length() == 0 || str.equalsIgnoreCase( "null" ) ){ return (BigDecimal) null; }else{ return new BigDecimal( str ); } } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return BigDecimal.class; } }ezmorph-1.0.6/net/sf/ezmorph/bean/0000755000175000017500000000000011014006420016713 5ustar twernertwernerezmorph-1.0.6/net/sf/ezmorph/bean/MorphDynaClass.java0000644000175000017500000001674710654104342022477 0ustar twernertwerner/* * Copyright 2006-2007-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.bean; import java.io.Serializable; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.MorphUtils; import net.sf.ezmorph.MorpherRegistry; import org.apache.commons.beanutils.DynaBean; import org.apache.commons.beanutils.DynaClass; import org.apache.commons.beanutils.DynaProperty; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; /** * @author Andres Almiray */ public final class MorphDynaClass implements DynaClass, Serializable { private static final Comparator dynaPropertyComparator = new Comparator(){ public int compare( Object a, Object b ) { if( a instanceof DynaProperty && b instanceof DynaProperty ){ DynaProperty p1 = (DynaProperty) a; DynaProperty p2 = (DynaProperty) b; return p1.getName() .compareTo( p2.getName() ); } return -1; } }; private static final long serialVersionUID = -613214016860871560L; private Map attributes; private Class beanClass; private DynaProperty dynaProperties[]; private String name; private Map properties = new HashMap(); private Class type; public MorphDynaClass( Map attributes ) { this( null, null, attributes ); } public MorphDynaClass( Map attributes, boolean exceptionOnEmptyAttributes ) { this( null, null, attributes, exceptionOnEmptyAttributes ); } public MorphDynaClass( String name, Class type, Map attributes ) { this( name, type, attributes, false ); } public MorphDynaClass( String name, Class type, Map attributes, boolean exceptionOnEmptyAttributes ) { if( name == null ){ name = "MorphDynaClass"; } if( type == null ){ type = MorphDynaBean.class; } if( !MorphDynaBean.class.isAssignableFrom( type ) ){ throw new MorphException( "MorphDynaBean is not assignable from " + type.getName() ); } if( attributes == null || attributes.isEmpty() ){ if( exceptionOnEmptyAttributes ){ throw new MorphException( "Attributes map is null or empty." ); }else{ attributes = new HashMap(); } } this.name = name; this.type = type; this.attributes = attributes; process(); } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof MorphDynaClass) ){ return false; } MorphDynaClass other = (MorphDynaClass) obj; EqualsBuilder builder = new EqualsBuilder().append( this.name, other.name ) .append( this.type, other.type ); if( dynaProperties.length != other.dynaProperties.length ){ return false; } for( int i = 0; i < dynaProperties.length; i++ ){ DynaProperty a = this.dynaProperties[i]; DynaProperty b = other.dynaProperties[i]; builder.append( a.getName(), b.getName() ); builder.append( a.getType(), b.getType() ); } return builder.isEquals(); } public DynaProperty[] getDynaProperties() { return dynaProperties; } public DynaProperty getDynaProperty( String propertyName ) { if( propertyName == null ){ throw new MorphException( "Unnespecified bean property name" ); } return (DynaProperty) properties.get( propertyName ); } public String getName() { return this.name; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder().append( name ) .append( type ); for( int i = 0; i < dynaProperties.length; i++ ){ builder.append( this.dynaProperties[i].getName() ); builder.append( this.dynaProperties[i].getType() ); } return builder.toHashCode(); } public DynaBean newInstance() throws IllegalAccessException, InstantiationException { return newInstance( null ); } public DynaBean newInstance( MorpherRegistry morpherRegistry ) throws IllegalAccessException, InstantiationException { if( morpherRegistry == null ){ morpherRegistry = new MorpherRegistry(); MorphUtils.registerStandardMorphers( morpherRegistry ); } MorphDynaBean dynaBean = (MorphDynaBean) getBeanClass().newInstance(); dynaBean.setDynaBeanClass( this ); dynaBean.setMorpherRegistry( morpherRegistry ); Iterator keys = attributes.keySet() .iterator(); while( keys.hasNext() ){ String key = (String) keys.next(); dynaBean.set( key, null ); } return dynaBean; } public String toString() { return new ToStringBuilder( this ).append( "name", this.name ) .append( "type", this.type ) .append( "attributes", this.attributes ) .toString(); } protected Class getBeanClass() { if( this.beanClass == null ){ process(); } return this.beanClass; } private void process() { this.beanClass = this.type; try{ Iterator entries = attributes.entrySet() .iterator(); dynaProperties = new DynaProperty[attributes.size()]; int i = 0; while( entries.hasNext() ){ Map.Entry entry = (Map.Entry) entries.next(); String pname = (String) entry.getKey(); Object pclass = entry.getValue(); DynaProperty dynaProperty = null; if( pclass instanceof String ){ Class klass = (Class) Class.forName( (String) pclass ); if( klass.isArray() && klass.getComponentType() .isArray() ){ throw new MorphException( "Multidimensional arrays are not supported" ); } dynaProperty = new DynaProperty( pname, klass ); }else if( pclass instanceof Class ){ Class klass = (Class) pclass; if( klass.isArray() && klass.getComponentType() .isArray() ){ throw new MorphException( "Multidimensional arrays are not supported" ); } dynaProperty = new DynaProperty( pname, klass ); }else{ throw new MorphException( "Type must be String or Class" ); } properties.put( dynaProperty.getName(), dynaProperty ); dynaProperties[i++] = dynaProperty; } } catch( ClassNotFoundException cnfe ){ throw new MorphException( cnfe ); } // keep properties sorted by name Arrays.sort( dynaProperties, 0, dynaProperties.length, dynaPropertyComparator ); } }ezmorph-1.0.6/net/sf/ezmorph/bean/MorphDynaBean.java0000644000175000017500000002524410654104342022267 0ustar twernertwerner/* * Copyright 2006-2007-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.bean; import java.io.Serializable; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.MorphUtils; import net.sf.ezmorph.MorpherRegistry; import org.apache.commons.beanutils.DynaBean; import org.apache.commons.beanutils.DynaClass; import org.apache.commons.beanutils.DynaProperty; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; /** * @author Andres Almiray */ public final class MorphDynaBean implements DynaBean, Serializable { private static final long serialVersionUID = -605547389232706344L; private MorphDynaClass dynaClass; private Map dynaValues = new HashMap(); private MorpherRegistry morpherRegistry; public MorphDynaBean() { this( null ); } public MorphDynaBean( MorpherRegistry morpherRegistry ) { setMorpherRegistry( morpherRegistry ); } public boolean contains( String name, String key ) { DynaProperty dynaProperty = getDynaProperty( name ); Class type = dynaProperty.getType(); if( !Map.class.isAssignableFrom( type ) ){ throw new MorphException( "Non-Mapped property name: " + name + " key: " + key ); } Object value = dynaValues.get( name ); if( value == null ){ value = new HashMap(); dynaValues.put( name, value ); } return ((Map) value).containsKey( key ); } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof MorphDynaBean) ){ return false; } MorphDynaBean other = (MorphDynaBean) obj; EqualsBuilder builder = new EqualsBuilder().append( this.dynaClass, other.dynaClass ); DynaProperty[] props = dynaClass.getDynaProperties(); for( int i = 0; i < props.length; i++ ){ DynaProperty prop = props[i]; builder.append( dynaValues.get( prop.getName() ), dynaValues.get( prop.getName() ) ); } return builder.isEquals(); } public Object get( String name ) { Object value = dynaValues.get( name ); if( value != null ){ return value; } Class type = getDynaProperty( name ).getType(); if( !type.isPrimitive() ){ return value; }else{ return morpherRegistry.morph( type, value ); } } public Object get( String name, int index ) { DynaProperty dynaProperty = getDynaProperty( name ); Class type = dynaProperty.getType(); if( !type.isArray() && !List.class.isAssignableFrom( type ) ){ throw new MorphException( "Non-Indexed property name: " + name + " index: " + index ); } Object value = dynaValues.get( name ); if( value.getClass() .isArray() ){ value = Array.get( value, index ); }else if( value instanceof List ){ value = ((List) value).get( index ); } return value; } public Object get( String name, String key ) { DynaProperty dynaProperty = getDynaProperty( name ); Class type = dynaProperty.getType(); if( !Map.class.isAssignableFrom( type ) ){ throw new MorphException( "Non-Mapped property name: " + name + " key: " + key ); } Object value = dynaValues.get( name ); if( value == null ){ value = new HashMap(); dynaValues.put( name, value ); } return ((Map) value).get( key ); } public DynaClass getDynaClass() { return this.dynaClass; } public MorpherRegistry getMorpherRegistry() { return morpherRegistry; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder().append( dynaClass ); DynaProperty[] props = dynaClass.getDynaProperties(); for( int i = 0; i < props.length; i++ ){ DynaProperty prop = props[i]; builder.append( dynaValues.get( prop.getName() ) ); } return builder.toHashCode(); } public void remove( String name, String key ) { DynaProperty dynaProperty = getDynaProperty( name ); Class type = dynaProperty.getType(); if( !Map.class.isAssignableFrom( type ) ){ throw new MorphException( "Non-Mapped property name: " + name + " key: " + key ); } Object value = dynaValues.get( name ); if( value == null ){ value = new HashMap(); dynaValues.put( name, value ); } ((Map) value).remove( key ); } public void set( String name, int index, Object value ) { DynaProperty dynaProperty = getDynaProperty( name ); Class type = dynaProperty.getType(); if( !type.isArray() && !List.class.isAssignableFrom( type ) ){ throw new MorphException( "Non-Indexed property name: " + name + " index: " + index ); } Object prop = dynaValues.get( name ); if( prop == null ){ if( List.class.isAssignableFrom( type ) ){ prop = new ArrayList(); }else{ prop = Array.newInstance( type.getComponentType(), index + 1 ); } dynaValues.put( name, prop ); } if( prop.getClass() .isArray() ){ if( index >= Array.getLength( prop ) ){ Object tmp = Array.newInstance( type.getComponentType(), index + 1 ); System.arraycopy( prop, 0, tmp, 0, Array.getLength( prop ) ); prop = tmp; dynaValues.put( name, tmp ); } Array.set( prop, index, value ); }else if( prop instanceof List ){ List l = (List) prop; if( index >= l.size() ){ for( int i = l.size(); i <= index + 1; i++ ){ l.add( null ); } } ((List) prop).set( index, value ); } } public void set( String name, Object value ) { DynaProperty property = getDynaProperty( name ); if( value == null || !isDynaAssignable( property.getType(), value.getClass() ) ){ value = morpherRegistry.morph( property.getType(), value ); } dynaValues.put( name, value ); } public void set( String name, String key, Object value ) { DynaProperty dynaProperty = getDynaProperty( name ); Class type = dynaProperty.getType(); if( !Map.class.isAssignableFrom( type ) ){ throw new MorphException( "Non-Mapped property name: " + name + " key: " + key ); } Object prop = dynaValues.get( name ); if( prop == null ){ prop = new HashMap(); dynaValues.put( name, prop ); } ((Map) prop).put( key, value ); } public synchronized void setDynaBeanClass( MorphDynaClass dynaClass ) { if( this.dynaClass == null ){ this.dynaClass = dynaClass; } } public void setMorpherRegistry( MorpherRegistry morpherRegistry ) { if( morpherRegistry == null ){ this.morpherRegistry = new MorpherRegistry(); MorphUtils.registerStandardMorphers( this.morpherRegistry ); }else{ this.morpherRegistry = morpherRegistry; } } public String toString() { return new ToStringBuilder( this, ToStringStyle.MULTI_LINE_STYLE ).append( dynaValues ) .toString(); } protected DynaProperty getDynaProperty( String name ) { DynaProperty property = getDynaClass().getDynaProperty( name ); if( property == null ){ throw new MorphException( "Unspecified property for " + name ); } return property; } protected boolean isDynaAssignable( Class dest, Class src ) { boolean assignable = dest.isAssignableFrom( src ); if( assignable ){ return true; } assignable = (dest == Boolean.TYPE && src == Boolean.class) ? true : assignable; assignable = (dest == Byte.TYPE && src == Byte.class) ? true : assignable; assignable = (dest == Character.TYPE && src == Character.class) ? true : assignable; assignable = (dest == Short.TYPE && src == Short.class) ? true : assignable; assignable = (dest == Integer.TYPE && src == Integer.class) ? true : assignable; assignable = (dest == Long.TYPE && src == Long.class) ? true : assignable; assignable = (dest == Float.TYPE && src == Float.class) ? true : assignable; assignable = (dest == Double.TYPE && src == Double.class) ? true : assignable; if( src == Double.TYPE || Double.class.isAssignableFrom( src ) ){ assignable = (isByte( dest ) || isShort( dest ) || isInteger( dest ) || isLong( dest ) || isFloat( dest )) ? true : assignable; } if( src == Float.TYPE || Float.class.isAssignableFrom( src ) ){ assignable = (isByte( dest ) || isShort( dest ) || isInteger( dest ) || isLong( dest )) ? true : assignable; } if( src == Long.TYPE || Long.class.isAssignableFrom( src ) ){ assignable = (isByte( dest ) || isShort( dest ) || isInteger( dest )) ? true : assignable; } if( src == Integer.TYPE || Integer.class.isAssignableFrom( src ) ){ assignable = (isByte( dest ) || isShort( dest )) ? true : assignable; } if( src == Short.TYPE || Short.class.isAssignableFrom( src ) ){ assignable = (isByte( dest )) ? true : assignable; } return assignable; } private boolean isByte( Class clazz ) { return Byte.class.isAssignableFrom( clazz ) || clazz == Byte.TYPE; } private boolean isFloat( Class clazz ) { return Float.class.isAssignableFrom( clazz ) || clazz == Float.TYPE; } private boolean isInteger( Class clazz ) { return Integer.class.isAssignableFrom( clazz ) || clazz == Integer.TYPE; } private boolean isLong( Class clazz ) { return Long.class.isAssignableFrom( clazz ) || clazz == Long.TYPE; } private boolean isShort( Class clazz ) { return Short.class.isAssignableFrom( clazz ) || clazz == Short.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/bean/package.html0000644000175000017500000000024110551653102021203 0ustar twernertwerner

Morphers for JavaBeans and DynaBeans.

ezmorph-1.0.6/net/sf/ezmorph/bean/BeanMorpher.java0000644000175000017500000002000111047006636021770 0ustar twernertwerner/* * Copyright 2006-2007-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.bean; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.Map; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.MorpherRegistry; import net.sf.ezmorph.ObjectMorpher; import net.sf.ezmorph.object.IdentityObjectMorpher; import org.apache.commons.beanutils.DynaBean; import org.apache.commons.beanutils.DynaProperty; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Converts a JavaBean into another JavaBean or DynaBean.
* This Morpher will try to match every property from the target JavaBean's * class to the properties of the source JavaBean. If any target property * differs in type from the source property, it will try to morph it. If a * Morpher is not found for that type, the conversion will be aborted with a * MorphException; this may be changed by setting the Morpher to be lenient, in * that way it will ignore the property (the resulting value will be null). * * @author Andres Almiray */ public final class BeanMorpher implements ObjectMorpher { private static final Log log = LogFactory.getLog( BeanMorpher.class ); private final Class beanClass; private boolean lenient; private final MorpherRegistry morpherRegistry; /** * @param beanClass the target class to morph to * @param morpherRegistry a registry of morphers */ public BeanMorpher( Class beanClass, MorpherRegistry morpherRegistry ) { this( beanClass, morpherRegistry, false ); } /** * @param beanClass the target class to morph to * @param morpherRegistry a registry of morphers * @param lenient if an exception should be raised if no morpher is found for * a target property */ public BeanMorpher( Class beanClass, MorpherRegistry morpherRegistry, boolean lenient ) { validateClass( beanClass ); if( morpherRegistry == null ){ throw new MorphException( "morpherRegistry is null" ); } this.beanClass = beanClass; this.morpherRegistry = morpherRegistry; this.lenient = lenient; } public Object morph( Object sourceBean ) { if( sourceBean == null ){ return null; } if( !supports( sourceBean.getClass() ) ){ throw new MorphException( "unsupported class: " + sourceBean.getClass() .getName() ); } Object targetBean = null; try{ targetBean = beanClass.newInstance(); PropertyDescriptor[] targetPds = PropertyUtils.getPropertyDescriptors( beanClass ); for( int i = 0; i < targetPds.length; i++ ){ PropertyDescriptor targetPd = targetPds[i]; String name = targetPd.getName(); if( targetPd.getWriteMethod() == null ){ log.info( "Property '" + beanClass.getName() + "." + name + "' has no write method. SKIPPED." ); continue; } Class sourceType = null; if( sourceBean instanceof DynaBean ){ DynaBean dynaBean = (DynaBean) sourceBean; DynaProperty dynaProperty = dynaBean.getDynaClass() .getDynaProperty( name ); if( dynaProperty == null ){ log.warn( "DynaProperty '" + name + "' does not exist. SKIPPED." ); continue; } sourceType = dynaProperty.getType(); }else{ PropertyDescriptor sourcePd = PropertyUtils.getPropertyDescriptor( sourceBean, name ); if( sourcePd == null ){ log.warn( "Property '" + sourceBean.getClass() .getName() + "." + name + "' does not exist. SKIPPED." ); continue; }else if( sourcePd.getReadMethod() == null ){ log.warn( "Property '" + sourceBean.getClass() .getName() + "." + name + "' has no read method. SKIPPED." ); continue; } sourceType = sourcePd.getPropertyType(); } Class targetType = targetPd.getPropertyType(); Object value = PropertyUtils.getProperty( sourceBean, name ); setProperty( targetBean, name, sourceType, targetType, value ); } } catch( MorphException me ){ throw me; } catch( Exception e ){ throw new MorphException( e ); } return targetBean; } public Class morphsTo() { return beanClass; } public boolean supports( Class clazz ) { return !clazz.isArray(); } private void setProperty( Object targetBean, String name, Class sourceType, Class targetType, Object value ) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if( targetType.isAssignableFrom( sourceType ) ){ if( value == null && targetType.isPrimitive() ){ value = morpherRegistry.morph( targetType, value ); } PropertyUtils.setProperty( targetBean, name, value ); }else{ if( targetType.equals( Object.class ) ){ // no conversion PropertyUtils.setProperty( targetBean, name, value ); }else{ if( value == null ){ if( targetType.isPrimitive() ){ PropertyUtils.setProperty( targetBean, name, morpherRegistry.morph( targetType, value ) ); } }else{ if( IdentityObjectMorpher.getInstance() == morpherRegistry.getMorpherFor( targetType ) ){ if( !lenient ){ throw new MorphException( "Can't find a morpher for target class " + targetType.getName() + " (" + name + ")" ); }else{ log.info( "Can't find a morpher for target class " + targetType.getName() + " (" + name + ") SKIPPED" ); } }else{ PropertyUtils.setProperty( targetBean, name, morpherRegistry.morph( targetType, value ) ); } } } } } private void validateClass( Class clazz ) { if( clazz == null ){ throw new MorphException( "target class is null" ); }else if( clazz.isPrimitive() ){ throw new MorphException( "target class is a primitive" ); }else if( clazz.isArray() ){ throw new MorphException( "target class is an array" ); }else if( clazz.isInterface() ){ throw new MorphException( "target class is an interface" ); }else if( DynaBean.class.isAssignableFrom( clazz ) ){ throw new MorphException( "target class is a DynaBean" ); }else if( Number.class.isAssignableFrom( clazz ) || Boolean.class.isAssignableFrom( clazz ) || Character.class.isAssignableFrom( clazz ) ){ throw new MorphException( "target class is a wrapper" ); }else if( String.class.isAssignableFrom( clazz ) ){ throw new MorphException( "target class is a String" ); }else if( Collection.class.isAssignableFrom( clazz ) ){ throw new MorphException( "target class is a Collection" ); }else if( Map.class.isAssignableFrom( clazz ) ){ throw new MorphException( "target class is a Map" ); } } }ezmorph-1.0.6/net/sf/ezmorph/test/0000755000175000017500000000000011014006420016765 5ustar twernertwernerezmorph-1.0.6/net/sf/ezmorph/test/package.html0000644000175000017500000000024410470137600021260 0ustar twernertwerner

Assertions for testing array equiality.

ezmorph-1.0.6/net/sf/ezmorph/test/ArrayAssertions.java0000644000175000017500000013425510714150472023010 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.test; import java.util.List; import junit.framework.Assert; /** * Provides assertions on arrays (primitive and objects).
* All methods support multiple dimensional arrays. * * @author Andres Almiray */ public class ArrayAssertions extends Assert { /** * Asserts that two boolean[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( boolean[] expecteds, boolean[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a boolean[] is equal to an Object[] (presumably an Boolean[]) * * @param expecteds * @param actuals */ public static void assertEquals( boolean[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two byte[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( byte[] expecteds, byte[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a byte[] is equal to an Object[] (presumably an Byte[]) * * @param expecteds * @param actuals */ public static void assertEquals( byte[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two char[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( char[] expecteds, char[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a char[] is equal to an Object[] (presumably an Character[]) * * @param expecteds * @param actuals */ public static void assertEquals( char[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two double[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( double[] expecteds, double[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a double[] is equal to an Object[] (presumably an Double[]) * * @param expecteds * @param actuals */ public static void assertEquals( double[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two float[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( float[] expecteds, float[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a float[] is equal to an Object[] (presumably an Float[]) * * @param expecteds * @param actuals */ public static void assertEquals( float[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two int[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( int[] expecteds, int[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a int[] is equal to an Object[] (presumably an Integer[]) * * @param expecteds * @param actuals */ public static void assertEquals( int[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two Lists are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( List expecteds, List actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two long[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( long[] expecteds, long[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a long[] is equal to an Object[] (presumably an Long[]) * * @param expecteds * @param actuals */ public static void assertEquals( long[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two objects are equal. If they are not an * AssertionFailedError is thrown. * * @param expecteds * @param actuals */ public static void assertEquals( Object expected, Object actual ) { assertEquals( null, expected, actual ); } /** * Asserts that Object[] (presumably an Boolean[]) is equal to an boolean[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, boolean[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that Object[] (presumably an Byte[]) is equal to an byte[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, byte[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that Object[] (presumably an Character[]) is equal to an char[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, char[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that Object[] (presumably an Double[]) is equal to an double[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, double[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that Object[] (presumably an Float[]) is equal to an float[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, float[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that Object[] (presumably an Integer[]) is equal to an int[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, int[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that Object[] (presumably an Long[]) is equal to an long[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, long[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two Object[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that Object[] (presumably an Short[]) is equal to an short[]. * * @param expecteds * @param actuals */ public static void assertEquals( Object[] expecteds, short[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that a short[] is equal to an Object[] (presumably an Short[]) * * @param expecteds * @param actuals */ public static void assertEquals( short[] expecteds, Object[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two short[] are equal.
* * @param expecteds * @param actuals */ public static void assertEquals( short[] expecteds, short[] actuals ) { assertEquals( null, expecteds, actuals ); } /** * Asserts that two boolean[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, boolean[] expecteds, boolean[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i] ); } } /** * Asserts that a boolean[] is equal to an Object[] (presumably an Boolean[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, boolean[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Boolean( expecteds[i] ), actuals[i] ); } } /** * Asserts that two byte[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, byte[] expecteds, byte[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i] ); } } /** * Asserts that a byte[] is equal to an Object[] (presumably an Byte[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, byte[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Byte( expecteds[i] ), actuals[i] ); } } /** * Asserts that two char[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, char[] expecteds, char[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i] ); } } /** * Asserts that a char[] is equal to an Object[] (presumably an Character[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, char[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Character( expecteds[i] ), actuals[i] ); } } /** * Asserts that two double[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, double[] expecteds, double[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i], 0d ); } } /** * Asserts that a double[] is equal to an Object[] (presumably an Double[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, double[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Double( expecteds[i] ), actuals[i] ); } } /** * Asserts that two float[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, float[] expecteds, float[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i], 0f ); } } /** * Asserts that a float[] is equal to an Object[] (presumably an Float[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, float[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Float( expecteds[i] ), actuals[i] ); } } /** * Asserts that two int[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, int[] expecteds, int[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i] ); } } /** * Asserts that a int[] is equal to an Object[] (presumably an Integer[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, int[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Integer( expecteds[i] ), actuals[i] ); } } /** * Asserts that two Lists are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, List expecteds, List actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected list was null" ); } if( actuals == null ){ fail( header + "actual list was null" ); } if( expecteds == actuals || expecteds.equals( actuals ) ){ return; } if( actuals.size() != expecteds.size() ){ fail( header + "list sizes differed, expected.size()=" + expecteds.size() + " actual.size()=" + actuals.size() ); } int max = expecteds.size(); for( int i = 0; i < max; i++ ){ Object o1 = expecteds.get( i ); Object o2 = actuals.get( i ); // handle nulls if( o1 == null ){ if( o2 == null ){ return; }else{ fail( header + "lists first differed at element [" + i + "];" ); } }else{ if( o2 == null ){ fail( header + "lists first differed at element [" + i + "];" ); } } if( o1.getClass() .isArray() && o2.getClass() .isArray() ){ Object[] expected = (Object[]) o1; Object[] actual = (Object[]) o2; assertEquals( header + "lists first differed at element " + i + ";", expected, actual ); }else if( List.class.isAssignableFrom( o1.getClass() ) && List.class.isAssignableFrom( o2.getClass() ) ){ assertEquals( header + "lists first differed at element [" + i + "];", (List) o1, (List) o2 ); }else{ assertEquals( header + "lists first differed at element [" + i + "];", o1, o2 ); } } } /** * Asserts that two long[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, long[] expecteds, long[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i] ); } } /** * Asserts that a long[] is equal to an Object[] (presumably an Long[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, long[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Long( expecteds[i] ), actuals[i] ); } } /** * Asserts that two objects are equal. If they are not an * AssertionFailedError is thrown with the given message. * * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object expected, Object actual ) { if( expected == null && actual == null ) return; if( expected != null && expected.equals( actual ) ) return; Class expectedClass = expected.getClass(); Class actualClass = actual.getClass(); if( expectedClass.isArray() && actualClass.isArray() ){ Class expectedInnerType = expectedClass.getComponentType(); Class actualInnerType = actualClass.getComponentType(); if( expectedInnerType.isPrimitive() ){ assertExpectedPrimitiveArrays( message, expected, actual, expectedInnerType, actualInnerType ); }else if( actualInnerType.isPrimitive() ){ assertActualPrimitiveArrays( message, expected, actual, expectedInnerType, actualInnerType ); }else{ assertEquals( message, (Object[]) expected, (Object[]) actual ); } }else{ failNotEquals( message, expected, actual ); } } /** * Asserts that Object[] (presumably an Boolean[]) is equal to an boolean[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, boolean[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Boolean( actuals[i] ) ); } } /** * Asserts that Object[] (presumably an Byte[]) is equal to an byte[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, byte[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Byte( actuals[i] ) ); } } /** * Asserts that Object[] (presumably an Character[]) is equal to an char[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, char[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Character( actuals[i] ) ); } } /** * Asserts that Object[] (presumably an Double[]) is equal to an double[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, double[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Double( actuals[i] ) ); } } /** * Asserts that Object[] (presumably an Float[]) is equal to an float[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, float[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Float( actuals[i] ) ); } } /** * Asserts that Object[] (presumably an Integer[]) is equal to an int[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, int[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Integer( actuals[i] ) ); } } /** * Asserts that Object[] (presumably an Long[]) is equal to an long[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, long[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Long( actuals[i] ) ); } } /** * Asserts that two Object[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, Object[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ Object o1 = expecteds[i]; Object o2 = actuals[i]; if( o1 == null ){ if( o2 == null ){ return; }else{ fail( header + "arrays first differed at element [" + i + "];" ); } }else{ if( o2 == null ){ fail( header + "arrays first differed at element [" + i + "];" ); } } if( o1.getClass() .isArray() && o2.getClass() .isArray() ){ Class type1 = o1.getClass() .getComponentType(); Class type2 = o2.getClass() .getComponentType(); if( type1.isPrimitive() ){ if( type1 == Boolean.TYPE ){ if( type2 == Boolean.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (boolean[]) o1, (boolean[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (boolean[]) o1, (Object[]) o2 ); } }else if( type1 == Byte.TYPE ){ if( type2 == Byte.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (byte[]) o1, (byte[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (byte[]) o1, (Object[]) o2 ); } }else if( type1 == Short.TYPE ){ if( type2 == Short.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (short[]) o1, (short[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (short[]) o1, (Object[]) o2 ); } }else if( type1 == Integer.TYPE ){ if( type2 == Integer.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (int[]) o1, (int[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (int[]) o1, (Object[]) o2 ); } }else if( type1 == Long.TYPE ){ if( type2 == Long.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (long[]) o1, (long[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (long[]) o1, (Object[]) o2 ); } }else if( type1 == Float.TYPE ){ if( type2 == Float.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (float[]) o1, (float[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (float[]) o1, (Object[]) o2 ); } }else if( type1 == Double.TYPE ){ if( type2 == Double.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (double[]) o1, (double[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (double[]) o1, (Object[]) o2 ); } }else if( type1 == Character.TYPE ){ if( type2 == Character.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (char[]) o1, (char[]) o2 ); }else{ assertEquals( header + "arrays first differed at element " + i + ";", (char[]) o1, (Object[]) o2 ); } } }else if( type2.isPrimitive() ){ if( type2 == Boolean.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (boolean[]) o2 ); }else if( type2 == Byte.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (byte[]) o2 ); }else if( type2 == Short.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (short[]) o2 ); }else if( type2 == Integer.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (int[]) o2 ); }else if( type2 == Long.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (long[]) o2 ); }else if( type2 == Float.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (float[]) o2 ); }else if( type2 == Double.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (double[]) o2 ); }else if( type2 == Character.TYPE ){ assertEquals( header + "arrays first differed at element " + i + ";", (Object[]) o1, (char[]) o2 ); } }else{ Object[] expected = (Object[]) o1; Object[] actual = (Object[]) o2; assertEquals( header + "arrays first differed at element " + i + ";", expected, actual ); } }else{ assertEquals( header + "arrays first differed at element [" + i + "];", o1, o2 ); } } } /** * Asserts that Object[] (presumably an Short[]) is equal to a short[]. * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, Object[] expecteds, short[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], new Short( actuals[i] ) ); } } /** * Asserts that a short[] is equal to an Object[] (presumably an Short[]) * * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, short[] expecteds, Object[] actuals ) { String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", new Short( expecteds[i] ), actuals[i] ); } } /** * Asserts that two short[] are equal.
* * @param message * @param expecteds * @param actuals */ public static void assertEquals( String message, short[] expecteds, short[] actuals ) { if( expecteds == actuals ){ return; } String header = message == null ? "" : message + ": "; if( expecteds == null ){ fail( header + "expected array was null" ); } if( actuals == null ){ fail( header + "actual array was null" ); } if( actuals.length != expecteds.length ){ fail( header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length ); } for( int i = 0; i < expecteds.length; i++ ){ assertEquals( header + "arrays first differed at element [" + i + "];", expecteds[i], actuals[i] ); } } private static void assertActualPrimitiveArrays( String message, Object expected, Object actual, Class expectedInnerType, Class actualInnerType ) { if( Boolean.TYPE.isAssignableFrom( actualInnerType ) ){ if( Boolean.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Boolean[]) expected, (boolean[]) actual ); }else{ assertEquals( message, (Object[]) expected, (boolean[]) actual ); } }else if( Byte.TYPE.isAssignableFrom( actualInnerType ) ){ if( Byte.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Byte[]) expected, (byte[]) actual ); }else{ assertEquals( message, (Object[]) expected, (byte[]) actual ); } }else if( Short.TYPE.isAssignableFrom( actualInnerType ) ){ if( Short.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Short[]) expected, (short[]) actual ); }else{ assertEquals( message, (Object[]) expected, (short[]) actual ); } }else if( Integer.TYPE.isAssignableFrom( actualInnerType ) ){ if( Integer.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Integer[]) expected, (int[]) actual ); }else{ assertEquals( message, (Object[]) expected, (int[]) actual ); } }else if( Long.TYPE.isAssignableFrom( actualInnerType ) ){ if( Long.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Long[]) expected, (long[]) actual ); }else{ assertEquals( message, (Object[]) expected, (long[]) actual ); } }else if( Float.TYPE.isAssignableFrom( actualInnerType ) ){ if( Float.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Float[]) expected, (float[]) actual ); }else{ assertEquals( message, (Object[]) expected, (float[]) actual ); } }else if( Double.TYPE.isAssignableFrom( actualInnerType ) ){ if( Double.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Double[]) expected, (double[]) actual ); }else{ assertEquals( message, (Object[]) expected, (double[]) actual ); } }else if( Character.TYPE.isAssignableFrom( actualInnerType ) ){ if( Character.class.isAssignableFrom( expectedInnerType ) ){ assertEquals( message, (Character[]) expected, (char[]) actual ); }else{ assertEquals( message, (Object[]) expected, (char[]) actual ); } } } private static void assertExpectedPrimitiveArrays( String message, Object expected, Object actual, Class expectedInnerType, Class actualInnerType ) { if( Boolean.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Boolean.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (boolean[]) expected, (boolean[]) actual ); }else if( Boolean.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (boolean[]) expected, (Boolean[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (boolean[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } }else if( Byte.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Byte.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (byte[]) expected, (byte[]) actual ); }else if( Byte.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (byte[]) expected, (Byte[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (byte[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } }else if( Short.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Short.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (short[]) expected, (short[]) actual ); }else if( Short.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (short[]) expected, (Short[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (short[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } }else if( Integer.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Integer.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (int[]) expected, (int[]) actual ); }else if( Integer.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (int[]) expected, (Integer[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (int[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } }else if( Long.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Long.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (long[]) expected, (long[]) actual ); }else if( Long.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (long[]) expected, (Long[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (long[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } }else if( Float.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Float.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (float[]) expected, (float[]) actual ); }else if( Float.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (float[]) expected, (Float[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (float[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } }else if( Double.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Double.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (double[]) expected, (double[]) actual ); }else if( Double.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (double[]) expected, (Double[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (double[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } }else if( Character.TYPE.isAssignableFrom( expectedInnerType ) ){ if( Character.TYPE.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (char[]) expected, (char[]) actual ); }else if( Character.class.isAssignableFrom( actualInnerType ) ){ assertEquals( message, (char[]) expected, (Character[]) actual ); }else if( !actualInnerType.isPrimitive() ){ assertEquals( message, (char[]) expected, (Object[]) actual ); }else{ failNotEquals( message, expected, actual ); } } } private ArrayAssertions() { } }ezmorph-1.0.6/net/sf/ezmorph/array/0000755000175000017500000000000011014006420017124 5ustar twernertwernerezmorph-1.0.6/net/sf/ezmorph/array/CharArrayMorpher.java0000644000175000017500000000705010654104342023215 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.CharMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a char[]. * * @author Andres Almiray */ public final class CharArrayMorpher extends AbstractArrayMorpher { private static final Class CHAR_ARRAY_CLASS = char[].class; private char defaultValue; public CharArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public CharArrayMorpher( char defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof CharArrayMorpher) ){ return false; } CharArrayMorpher other = (CharArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public char getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( CHAR_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (char[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( char.class, dimensions ); CharMorpher morpher = isUseDefault() ? new CharMorpher( defaultValue ) : new CharMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, new Character( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return CHAR_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/ByteArrayMorpher.java0000644000175000017500000000674310654104342023253 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.ByteMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a byte[]. * * @author Andres Almiray */ public final class ByteArrayMorpher extends AbstractArrayMorpher { private static final Class BYTE_ARRAY_CLASS = byte[].class; private byte defaultValue; public ByteArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public ByteArrayMorpher( byte defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof ByteArrayMorpher) ){ return false; } ByteArrayMorpher other = (ByteArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } public byte getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( BYTE_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (byte[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( byte.class, dimensions ); ByteMorpher morpher = isUseDefault() ? new ByteMorpher( defaultValue ) : new ByteMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, new Byte( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return BYTE_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/BooleanArrayMorpher.java0000644000175000017500000000712610654104342023723 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.BooleanMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a boolean[]. * * @author Andres Almiray */ public final class BooleanArrayMorpher extends AbstractArrayMorpher { private static final Class BOOLEAN_ARRAY_CLASS = boolean[].class; private boolean defaultValue; public BooleanArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public BooleanArrayMorpher( boolean defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof BooleanArrayMorpher) ){ return false; } BooleanArrayMorpher other = (BooleanArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } public boolean getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( BOOLEAN_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (boolean[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( boolean.class, dimensions ); BooleanMorpher morpher = isUseDefault() ? new BooleanMorpher( defaultValue ) : new BooleanMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, morpher.morph( Array.get( array, index ) ) ? Boolean.TRUE : Boolean.FALSE ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return BOOLEAN_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/ObjectArrayMorpher.java0000644000175000017500000001073110713674520023554 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import java.lang.reflect.Method; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.Morpher; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to another array using a Morpher. * * @author Andres Almiray */ public final class ObjectArrayMorpher extends AbstractArrayMorpher { private Morpher morpher; private Method morphMethod; private Class target; private Class targetArrayClass; /** * Creates a new ArrayMorpher which will use another Morpher for its inner * type.
* The inner morpher can not morph to an array. Multiple dimension arrays are * already handled by this class. * * @param morpher the Morpher that will handle the array's inner type. */ public ObjectArrayMorpher( Morpher morpher ) { super( false ); setMorpher( morpher ); } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof ObjectArrayMorpher) ){ return false; } ObjectArrayMorpher other = (ObjectArrayMorpher) obj; return morpher.equals( other.morpher ); } public int hashCode() { return new HashCodeBuilder().append( morpher ) .toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( this.target, dimensions ); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ try{ Object value = Array.get( array, index ); if( value != null && !morpher.supports( value.getClass() ) ){ throw new MorphException( value.getClass() + " is not supported" ); } Object morphed = morphMethod.invoke( morpher, new Object[] { value } ); Array.set( result, index, morphed ); } catch( MorphException me ){ throw me; } catch( Exception e ){ throw new MorphException( e ); } } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return targetArrayClass; } public boolean supports( Class clazz ) { if( clazz != null && !clazz.isArray() ){ return false; } while( clazz.isArray() ){ clazz = clazz.getComponentType(); } return morpher.supports( clazz ); } private void setMorpher( Morpher morpher ) { if( morpher == null ){ throw new IllegalArgumentException( "morpher can not be null" ); } if( morpher.morphsTo() .isArray() ){ throw new IllegalArgumentException( "morpher target class can not be an array" ); } this.morpher = morpher; this.targetArrayClass = Array.newInstance( morpher.morphsTo(), 1 ) .getClass(); this.target = morpher.morphsTo(); // cache the morph method try{ morphMethod = morpher.getClass() .getDeclaredMethod( "morph", new Class[] { Object.class } ); } catch( NoSuchMethodException nsme ){ throw new IllegalArgumentException( nsme.getMessage() ); } } }ezmorph-1.0.6/net/sf/ezmorph/array/package.html0000644000175000017500000000022110470137600021412 0ustar twernertwerner

Morphers for arrays.

ezmorph-1.0.6/net/sf/ezmorph/array/FloatArrayMorpher.java0000644000175000017500000000710710654104342023410 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.FloatMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a float[]. * * @author Andres Almiray */ public final class FloatArrayMorpher extends AbstractArrayMorpher { private static final Class FLOAT_ARRAY_CLASS = float[].class; private float defaultValue; public FloatArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public FloatArrayMorpher( float defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof FloatArrayMorpher) ){ return false; } FloatArrayMorpher other = (FloatArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public float getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( FLOAT_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (float[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( float.class, dimensions ); FloatMorpher morpher = isUseDefault() ? new FloatMorpher( defaultValue ) : new FloatMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, new Float( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return FLOAT_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/AbstractArrayMorpher.java0000644000175000017500000000417610654104342024111 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.ObjectMorpher; /** * Base class for array Morphers. * * @author Andres Almiray */ public abstract class AbstractArrayMorpher implements ObjectMorpher { private boolean useDefault = false; public AbstractArrayMorpher() { } /** * @param useDefault if morph() should return a default value if the value to * be morphed is null */ public AbstractArrayMorpher( boolean useDefault ) { this.useDefault = useDefault; } /** * Returns if this morpher will use a default value. */ public boolean isUseDefault() { return useDefault; } /** * Sets if this morpher will use a default value. */ public void setUseDefault( boolean useDefault ) { this.useDefault = useDefault; } public boolean supports( Class clazz ) { return clazz.isArray(); } /** * Creates an array representing the dimensions for comversion. */ protected int[] createDimensions( int length, int initial ) { Object dims = Array.newInstance( int.class, length ); Array.set( dims, 0, new Integer( initial ) ); return (int[]) dims; } /** * Returns the number of dimensions in an array class. */ protected int getDimensions( Class arrayClass ) { if( arrayClass == null || !arrayClass.isArray() ){ return 0; } return 1 + getDimensions( arrayClass.getComponentType() ); } }ezmorph-1.0.6/net/sf/ezmorph/array/ShortArrayMorpher.java0000644000175000017500000000710710654104342023442 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.ShortMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a short[]. * * @author Andres Almiray */ public final class ShortArrayMorpher extends AbstractArrayMorpher { private static final Class SHORT_ARRAY_CLASS = short[].class; private short defaultValue; public ShortArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public ShortArrayMorpher( short defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof ShortArrayMorpher) ){ return false; } ShortArrayMorpher other = (ShortArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public short getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( SHORT_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (short[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( short.class, dimensions ); ShortMorpher morpher = isUseDefault() ? new ShortMorpher( defaultValue ) : new ShortMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, new Short( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return SHORT_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/DoubleArrayMorpher.java0000644000175000017500000000713410654104342023555 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.DoubleMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a double[]. * * @author Andres Almiray */ public final class DoubleArrayMorpher extends AbstractArrayMorpher { private static final Class DOUBLE_ARRAY_CLASS = double[].class; private double defaultValue; public DoubleArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public DoubleArrayMorpher( double defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof DoubleArrayMorpher) ){ return false; } DoubleArrayMorpher other = (DoubleArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public double getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( DOUBLE_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (double[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( double.class, dimensions ); DoubleMorpher morpher = isUseDefault() ? new DoubleMorpher( defaultValue ) : new DoubleMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, new Double( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return DOUBLE_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/IntArrayMorpher.java0000644000175000017500000000702210654104342023071 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.IntMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a int[]. * * @author Andres Almiray */ public final class IntArrayMorpher extends AbstractArrayMorpher { private static final Class INT_ARRAY_CLASS = int[].class; private int defaultValue; public IntArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public IntArrayMorpher( int defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof IntArrayMorpher) ){ return false; } IntArrayMorpher other = (IntArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public int getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( INT_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (int[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( int.class, dimensions ); IntMorpher morpher = isUseDefault() ? new IntMorpher( defaultValue ) : new IntMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, new Integer( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return INT_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/LongArrayMorpher.java0000644000175000017500000000704310654104342023241 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.LongMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a long[]. * * @author Andres Almiray */ public final class LongArrayMorpher extends AbstractArrayMorpher { private static final Class LONG_ARRAY_CLASS = long[].class; private long defaultValue; public LongArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public LongArrayMorpher( long defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof LongArrayMorpher) ){ return false; } LongArrayMorpher other = (LongArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public long getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( LONG_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (long[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( long.class, dimensions ); LongMorpher morpher = isUseDefault() ? new LongMorpher( defaultValue ) : new LongMorpher(); if( dims == 1 ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, new Long( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return LONG_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/BooleanObjectArrayMorpher.java0000644000175000017500000000777510654104342025064 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.BooleanMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a Boolean[]. * * @author Andres Almiray */ public final class BooleanObjectArrayMorpher extends AbstractArrayMorpher { private static final Class BOOLEAN_OBJECT_ARRAY_CLASS = Boolean[].class; private Boolean defaultValue; public BooleanObjectArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public BooleanObjectArrayMorpher( Boolean defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof BooleanObjectArrayMorpher) ){ return false; } BooleanObjectArrayMorpher other = (BooleanObjectArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } public Boolean getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( BOOLEAN_OBJECT_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (Boolean[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( Boolean.class, dimensions ); if( dims == 1 ){ BooleanMorpher morpher = null; if( isUseDefault() ){ if( defaultValue == null ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, null ); } return result; }else{ morpher = new BooleanMorpher( defaultValue.booleanValue() ); } }else{ morpher = new BooleanMorpher(); } for( int index = 0; index < length; index++ ){ Array.set( result, index, morpher.morph( Array.get( array, index ) ) ? Boolean.TRUE : Boolean.FALSE ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return BOOLEAN_OBJECT_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/array/CharacterObjectArrayMorpher.java0000644000175000017500000000775310654104342025375 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.array; import java.lang.reflect.Array; import net.sf.ezmorph.MorphException; import net.sf.ezmorph.primitive.CharMorpher; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs an array to a Character[]. * * @author Andres Almiray */ public final class CharacterObjectArrayMorpher extends AbstractArrayMorpher { private static final Class CHARACTER_OBJECT_ARRAY_CLASS = Character[].class; private Character defaultValue; public CharacterObjectArrayMorpher() { super( false ); } /** * @param defaultValue return value if the value to be morphed is null */ public CharacterObjectArrayMorpher( Character defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof CharacterObjectArrayMorpher) ){ return false; } CharacterObjectArrayMorpher other = (CharacterObjectArrayMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } public Character getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } public Object morph( Object array ) { if( array == null ){ return null; } if( CHARACTER_OBJECT_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ // no conversion needed return (Character[]) array; } if( array.getClass() .isArray() ){ int length = Array.getLength( array ); int dims = getDimensions( array.getClass() ); int[] dimensions = createDimensions( dims, length ); Object result = Array.newInstance( Character.class, dimensions ); if( dims == 1 ){ CharMorpher morpher = null; if( isUseDefault() ){ if( defaultValue == null ){ for( int index = 0; index < length; index++ ){ Array.set( result, index, null ); } return result; }else{ morpher = new CharMorpher( defaultValue.charValue() ); } }else{ morpher = new CharMorpher(); } for( int index = 0; index < length; index++ ){ Array.set( result, index, new Character( morpher.morph( Array.get( array, index ) ) ) ); } }else{ for( int index = 0; index < length; index++ ){ Array.set( result, index, morph( Array.get( array, index ) ) ); } } return result; }else{ throw new MorphException( "argument is not an array: " + array.getClass() ); } } public Class morphsTo() { return CHARACTER_OBJECT_ARRAY_CLASS; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/0000755000175000017500000000000011014006420020016 5ustar twernertwernerezmorph-1.0.6/net/sf/ezmorph/primitive/LongMorpher.java0000644000175000017500000000615110654104342023133 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a long. * * @author Andres Almiray */ public final class LongMorpher extends AbstractIntegerMorpher { private long defaultValue; public LongMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public LongMorpher( long defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof LongMorpher) ){ return false; } LongMorpher other = (LongMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public long getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public long morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Number ){ return ((Number) value).longValue(); }else{ long i = 0; try{ i = Long.parseLong( getIntegerValue( value ) ); return i; } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return Long.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/AbstractPrimitiveMorpher.java0000644000175000017500000000326010654104342025666 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.Morpher; /** * Base class for primitive value conversion.
* * @author Andres Almiray */ public abstract class AbstractPrimitiveMorpher implements Morpher { private boolean useDefault = false; public AbstractPrimitiveMorpher() { } /** * @param useDefault if morph() should return a default value if the value to * be morphed is null */ public AbstractPrimitiveMorpher( boolean useDefault ) { this.useDefault = useDefault; } /** * Returns if this morpher will use a default value if the value to be * morphed is null */ public boolean isUseDefault() { return useDefault; } /** * Returns true if the Morpher supports conversion from this Class.
* Supports any type that is not an Array. * * @param clazz the source Class * @return true if clazz is supported by this morpher, false otherwise. */ public boolean supports( Class clazz ) { return !clazz.isArray(); } }ezmorph-1.0.6/net/sf/ezmorph/primitive/BooleanMorpher.java0000644000175000017500000000745410654104342023622 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a boolean. * * @author Andres Almiray */ public final class BooleanMorpher extends AbstractPrimitiveMorpher { private boolean defaultValue; public BooleanMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public BooleanMorpher( boolean defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof BooleanMorpher) ){ return false; } BooleanMorpher other = (BooleanMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public boolean getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public boolean morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Boolean ){ return ((Boolean) value).booleanValue(); }else if( value instanceof Number ){ if( value instanceof Double && (Double.isInfinite( ((Number) value).doubleValue() ) || Double.isNaN( ((Number) value).doubleValue() )) ){ return true; } if( value instanceof Float && (Float.isInfinite( ((Number) value).floatValue() ) || Float.isNaN( ((Number) value).floatValue() )) ){ return true; } long l = ((Number) value).longValue(); return l != 0; }else{ String s = String.valueOf( value ); if( s.equalsIgnoreCase( "true" ) || s.equalsIgnoreCase( "yes" ) || s.equalsIgnoreCase( "on" ) ){ return true; }else if( s.equalsIgnoreCase( "false" ) || s.equalsIgnoreCase( "no" ) || s.equalsIgnoreCase( "off" ) ){ return false; }else if( isUseDefault() ){ return defaultValue; } } throw new MorphException( "Can't morph value: " + value ); } public Class morphsTo() { return Boolean.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/ByteMorpher.java0000644000175000017500000000615110654104342023137 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a byte. * * @author Andres Almiray */ public final class ByteMorpher extends AbstractIntegerMorpher { private byte defaultValue; public ByteMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public ByteMorpher( byte defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof ByteMorpher) ){ return false; } ByteMorpher other = (ByteMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public byte getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public byte morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Number ){ return ((Number) value).byteValue(); }else{ byte i = 0; try{ i = Byte.parseByte( getIntegerValue( value ) ); return i; } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return Byte.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/FloatMorpher.java0000644000175000017500000000617010654104342023302 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Moprhs to a float. * * @author Andres Almiray */ public final class FloatMorpher extends AbstractDecimalMorpher { private float defaultValue; public FloatMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public FloatMorpher( float defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof FloatMorpher) ){ return false; } FloatMorpher other = (FloatMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public float getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public float morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Number ){ return ((Number) value).floatValue(); }else{ float i = 0; try{ i = Float.parseFloat( String.valueOf( value ) ); return i; } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return Float.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/AbstractIntegerMorpher.java0000644000175000017500000000323610654104342025316 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import java.util.Locale; /** * Base class por primitive integer conversion. * * @author Andres Almiray */ public abstract class AbstractIntegerMorpher extends AbstractPrimitiveMorpher { public AbstractIntegerMorpher() { super(); } /** * @param useDefault if morph() should return a default value if the value to * be morphed is null */ public AbstractIntegerMorpher( boolean useDefault ) { super( useDefault ); } /** * Trims the String from the begining to the first "." */ protected String getIntegerValue( Object obj ) { // use en_US Locale Locale defaultLocale = Locale.getDefault(); String str = null; try{ Locale.setDefault( Locale.US ); str = String.valueOf( obj ); } finally{ Locale.setDefault( defaultLocale ); } int index = str.indexOf( "." ); if( index != -1 ){ str = str.substring( 0, index ); } return str; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/IntMorpher.java0000644000175000017500000000614210654104342022766 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to an int. * * @author Andres Almiray */ public final class IntMorpher extends AbstractIntegerMorpher { private int defaultValue; public IntMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public IntMorpher( int defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof IntMorpher) ){ return false; } IntMorpher other = (IntMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public int getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public int morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Number ){ return ((Number) value).intValue(); }else{ int i = 0; try{ i = Integer.parseInt( getIntegerValue( value ) ); return i; } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return Integer.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/package.html0000644000175000017500000000023210470137600022306 0ustar twernertwerner

Morphers for primitive types.

ezmorph-1.0.6/net/sf/ezmorph/primitive/CharMorpher.java0000644000175000017500000000614110654104342023110 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a char. * * @author Andres Almiray */ public final class CharMorpher extends AbstractPrimitiveMorpher { private char defaultValue; public CharMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public CharMorpher( char defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof CharMorpher) ){ return false; } CharMorpher other = (CharMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public char getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public char morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Character ){ return ((Character) value).charValue(); }else{ String s = String.valueOf( value ); if( s.length() > 0 ){ return s.charAt( 0 ); }else{ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "Can't morph value: " + value ); } } } } public Class morphsTo() { return Character.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/AbstractDecimalMorpher.java0000644000175000017500000000216010654104342025252 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; /** * Base class for primitive decimal conversion. * * @author Andres Almiray */ public abstract class AbstractDecimalMorpher extends AbstractPrimitiveMorpher { public AbstractDecimalMorpher() { super(); } /** * @param useDefault if morph() should return a default value if the value to * be morphed is null */ public AbstractDecimalMorpher( boolean useDefault ) { super( useDefault ); } }ezmorph-1.0.6/net/sf/ezmorph/primitive/DoubleMorpher.java0000644000175000017500000000621010654104342023442 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a double. * * @author Andres Almiray */ public final class DoubleMorpher extends AbstractDecimalMorpher { private double defaultValue; public DoubleMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public DoubleMorpher( double defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof DoubleMorpher) ){ return false; } DoubleMorpher other = (DoubleMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public double getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public double morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Number ){ return ((Number) value).doubleValue(); }else{ double i = 0; try{ i = Double.parseDouble( String.valueOf( value ) ); return i; } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return Double.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/primitive/ShortMorpher.java0000644000175000017500000000617010654104342023334 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph.primitive; import net.sf.ezmorph.MorphException; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * Morphs to a short. * * @author Andres Almiray */ public final class ShortMorpher extends AbstractIntegerMorpher { private short defaultValue; public ShortMorpher() { super(); } /** * @param defaultValue return value if the value to be morphed is null */ public ShortMorpher( short defaultValue ) { super( true ); this.defaultValue = defaultValue; } public boolean equals( Object obj ) { if( this == obj ){ return true; } if( obj == null ){ return false; } if( !(obj instanceof ShortMorpher) ){ return false; } ShortMorpher other = (ShortMorpher) obj; EqualsBuilder builder = new EqualsBuilder(); if( isUseDefault() && other.isUseDefault() ){ builder.append( getDefaultValue(), other.getDefaultValue() ); return builder.isEquals(); }else if( !isUseDefault() && !other.isUseDefault() ){ return builder.isEquals(); }else{ return false; } } /** * Returns the default value for this Morpher. */ public short getDefaultValue() { return defaultValue; } public int hashCode() { HashCodeBuilder builder = new HashCodeBuilder(); if( isUseDefault() ){ builder.append( getDefaultValue() ); } return builder.toHashCode(); } /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ public short morph( Object value ) { if( value == null ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( "value is null" ); } } if( value instanceof Number ){ return ((Number) value).shortValue(); }else{ short i = 0; try{ i = Short.parseShort( getIntegerValue( value ) ); return i; } catch( NumberFormatException nfe ){ if( isUseDefault() ){ return defaultValue; }else{ throw new MorphException( nfe ); } } } } public Class morphsTo() { return Short.TYPE; } }ezmorph-1.0.6/net/sf/ezmorph/package.html0000644000175000017500000000017510470137600020304 0ustar twernertwerner

ezmorph-1.0.6/net/sf/ezmorph/MorphUtils.java0000644000175000017500000002102510654104342020772 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph; import java.math.BigDecimal; import java.math.BigInteger; import net.sf.ezmorph.array.BooleanArrayMorpher; import net.sf.ezmorph.array.ByteArrayMorpher; import net.sf.ezmorph.array.CharArrayMorpher; import net.sf.ezmorph.array.DoubleArrayMorpher; import net.sf.ezmorph.array.FloatArrayMorpher; import net.sf.ezmorph.array.IntArrayMorpher; import net.sf.ezmorph.array.LongArrayMorpher; import net.sf.ezmorph.array.ObjectArrayMorpher; import net.sf.ezmorph.array.ShortArrayMorpher; import net.sf.ezmorph.object.BooleanObjectMorpher; import net.sf.ezmorph.object.CharacterObjectMorpher; import net.sf.ezmorph.object.ClassMorpher; import net.sf.ezmorph.object.NumberMorpher; import net.sf.ezmorph.object.StringMorpher; import net.sf.ezmorph.primitive.BooleanMorpher; import net.sf.ezmorph.primitive.ByteMorpher; import net.sf.ezmorph.primitive.CharMorpher; import net.sf.ezmorph.primitive.DoubleMorpher; import net.sf.ezmorph.primitive.FloatMorpher; import net.sf.ezmorph.primitive.IntMorpher; import net.sf.ezmorph.primitive.LongMorpher; import net.sf.ezmorph.primitive.ShortMorpher; /** * Covenient class for registering standard morphers to a ConvertRegistry.
*/ public class MorphUtils { /** Constant value for BigDecimal(1) */ public static final BigDecimal BIGDECIMAL_ONE = new BigDecimal( "1" ); /** Constant value for BigDecimal(0) */ public static final BigDecimal BIGDECIMAL_ZERO = new BigDecimal( "0" ); /** * Clears and registers all standard morpehrs. * * @param morpherRegistry */ public static void registerStandardMorphers( MorpherRegistry morpherRegistry ) { morpherRegistry.clear(); registerStandardPrimitiveMorphers( morpherRegistry ); registerStandardPrimitiveArrayMorphers( morpherRegistry ); registerStandardObjectMorphers( morpherRegistry ); registerStandardObjectArrayMorphers( morpherRegistry ); } /** * Registers morphers for arrays of wrappers and String with standard default * values.
*
    *
  • Boolean - Boolean.FALSE
  • *
  • Character - new Character('\0')
  • *
  • Byte - new Byte( (byte)0 )
  • *
  • Short - new Short( (short)0 )
  • *
  • Integer - new Integer( 0 )
  • *
  • Long - new Long( 0 )
  • *
  • Float - new Float( 0 )
  • *
  • Double - new Double( 0 )
  • *
  • String - null
  • *
  • BigInteger - BigInteger.ZERO
  • *
  • BigDecimal - MorphUtils.BIGDECIMAL_ZERO
  • *
* * @param morpherRegistry */ public static void registerStandardObjectArrayMorphers( MorpherRegistry morpherRegistry ) { morpherRegistry.registerMorpher( new ObjectArrayMorpher( new BooleanObjectMorpher( Boolean.FALSE ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new CharacterObjectMorpher( new Character( '\0' ) ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( StringMorpher.getInstance() ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Byte.class, new Byte( (byte) 0 ) ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Short.class, new Short( (short) 0 ) ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Integer.class, new Integer( 0 ) ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Long.class, new Long( 0 ) ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Float.class, new Float( 0 ) ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( Double.class, new Double( 0 ) ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( BigInteger.class, BigInteger.ZERO ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( new NumberMorpher( BigDecimal.class, MorphUtils.BIGDECIMAL_ZERO ) ) ); morpherRegistry.registerMorpher( new ObjectArrayMorpher( ClassMorpher.getInstance() ) ); } /** * Registers morphers for wrappers and String with standard default values.
*
    *
  • Boolean - Boolean.FALSE
  • *
  • Character - new Character('\0')
  • *
  • Byte - new Byte( (byte)0 )
  • *
  • Short - new Short( (short)0 )
  • *
  • Integer - new Integer( 0 )
  • *
  • Long - new Long( 0 )
  • *
  • Float - new Float( 0 )
  • *
  • Double - new Double( 0 )
  • *
  • String - null
  • *
  • BigInteger - BigInteger.ZERO
  • *
  • BigDecimal - MorphUtils.BIGDECIMAL_ZERO
  • *
* * @param morpherRegistry */ public static void registerStandardObjectMorphers( MorpherRegistry morpherRegistry ) { morpherRegistry.registerMorpher( new BooleanObjectMorpher( Boolean.FALSE ) ); morpherRegistry.registerMorpher( new CharacterObjectMorpher( new Character( '\0' ) ) ); morpherRegistry.registerMorpher( StringMorpher.getInstance() ); morpherRegistry.registerMorpher( new NumberMorpher( Byte.class, new Byte( (byte) 0 ) ) ); morpherRegistry.registerMorpher( new NumberMorpher( Short.class, new Short( (short) 0 ) ) ); morpherRegistry.registerMorpher( new NumberMorpher( Integer.class, new Integer( 0 ) ) ); morpherRegistry.registerMorpher( new NumberMorpher( Long.class, new Long( 0 ) ) ); morpherRegistry.registerMorpher( new NumberMorpher( Float.class, new Float( 0 ) ) ); morpherRegistry.registerMorpher( new NumberMorpher( Double.class, new Double( 0 ) ) ); morpherRegistry.registerMorpher( new NumberMorpher( BigInteger.class, BigInteger.ZERO ) ); morpherRegistry.registerMorpher( new NumberMorpher( BigDecimal.class, MorphUtils.BIGDECIMAL_ZERO ) ); morpherRegistry.registerMorpher( ClassMorpher.getInstance() ); } /** * Registers morphers for arrays of primitives with standard default values.
*
    *
  • boolean - false
  • *
  • char - '\0'
  • *
  • byte - 0
  • *
  • short - 0
  • *
  • int - 0
  • *
  • long - 0
  • *
  • float - 0
  • *
  • double - 0
  • *
* * @param morpherRegistry */ public static void registerStandardPrimitiveArrayMorphers( MorpherRegistry morpherRegistry ) { morpherRegistry.registerMorpher( new BooleanArrayMorpher( false ) ); morpherRegistry.registerMorpher( new CharArrayMorpher( '\0' ) ); morpherRegistry.registerMorpher( new ByteArrayMorpher( (byte) 0 ) ); morpherRegistry.registerMorpher( new ShortArrayMorpher( (short) 0 ) ); morpherRegistry.registerMorpher( new IntArrayMorpher( 0 ) ); morpherRegistry.registerMorpher( new LongArrayMorpher( 0 ) ); morpherRegistry.registerMorpher( new FloatArrayMorpher( 0 ) ); morpherRegistry.registerMorpher( new DoubleArrayMorpher( 0 ) ); } /** * Registers morphers for primitives with standard default values.
*
    *
  • boolean - false
  • *
  • char - '\0'
  • *
  • byte - 0
  • *
  • short - 0
  • *
  • int - 0
  • *
  • long - 0
  • *
  • float - 0
  • *
  • double - 0
  • *
* * @param morpherRegistry */ public static void registerStandardPrimitiveMorphers( MorpherRegistry morpherRegistry ) { morpherRegistry.registerMorpher( new BooleanMorpher( false ) ); morpherRegistry.registerMorpher( new CharMorpher( '\0' ) ); morpherRegistry.registerMorpher( new ByteMorpher( (byte) 0 ) ); morpherRegistry.registerMorpher( new ShortMorpher( (short) 0 ) ); morpherRegistry.registerMorpher( new IntMorpher( 0 ) ); morpherRegistry.registerMorpher( new LongMorpher( 0 ) ); morpherRegistry.registerMorpher( new FloatMorpher( 0 ) ); morpherRegistry.registerMorpher( new DoubleMorpher( 0 ) ); } private MorphUtils() { } }ezmorph-1.0.6/net/sf/ezmorph/ObjectMorpher.java0000644000175000017500000000207510654104342021433 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph; /** * Marker interface for morphers that return an Object.
* * @author Andres Almiray */ public interface ObjectMorpher extends Morpher { /** * Morphs the input object into an output object of the supported type. * * @param value The input value to be morphed * @exception MorphException if conversion cannot be performed successfully */ Object morph( Object value ); }ezmorph-1.0.6/net/sf/ezmorph/Morpher.java0000644000175000017500000000240010654104342020274 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph; /** * Marker interface for morphers.
* All implementations must have a morph( Object value ) method * that returns the appropiate morphed value. * * @author Andres Almiray */ public interface Morpher { /** * Returns the target Class for conversion. * * @return the target Class for conversion. */ Class morphsTo(); /** * Returns true if the Morpher supports conversion from this Class. * * @param clazz the source Class * @return true if clazz is supported by this morpher, false otherwise. */ boolean supports( Class clazz ); }ezmorph-1.0.6/net/sf/ezmorph/MorpherRegistry.java0000644000175000017500000001646611117274710022050 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph; import java.io.Serializable; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.ezmorph.object.IdentityObjectMorpher; /** * Convenient class that manages Morphers.
* A MorpherRehistry manages a group of Morphers. A Morpher will always be * associated with a target class, it is possible to have several Morphers * registered for a target class, if this is the case, the first Morpher will be * used when performing a conversion and no specific Morpher is selected in * advance.
* {@link MorphUtils} may be used to register standard Morphers for primitive * types and primitive wrappers, as well as arrays of those types. * * @author Andres Almiray */ public class MorpherRegistry implements Serializable { private static final long serialVersionUID = -3894767123320768419L; private Map morphers = new HashMap(); public MorpherRegistry() { } /** * Deregisters all morphers. */ public synchronized void clear() { morphers.clear(); } /** * Deregister all Morphers of a type.
* * @param class the target type the Morphers morph to */ public synchronized void clear( Class type ) { List registered = (List) morphers.get( type ); if( registered != null ){ morphers.remove( type ); } } /** * Deregister the specified Morpher.
* The registry will remove the target Class from the morphers * Map if it has no other registered morphers. * * @param morpher the target Morpher to remove */ public synchronized void deregisterMorpher( Morpher morpher ) { List registered = (List) morphers.get( morpher.morphsTo() ); if( registered != null && !registered.isEmpty() ){ registered.remove( morpher ); if( registered.isEmpty() ){ morphers.remove( morpher.morphsTo() ); } } } /** * Returns a morpher for clazz.
* If several morphers are found for that class, it returns the first. If no * Morpher is found it will return the IdentityObjectMorpher. * * @param clazz the target class for which a Morpher may be associated */ public synchronized Morpher getMorpherFor( Class clazz ) { List registered = (List) morphers.get( clazz ); if( registered == null || registered.isEmpty() ){ // no morpher registered for clazz return IdentityObjectMorpher.getInstance(); }else{ return (Morpher) registered.get( 0 ); } } /** * Returns all morphers for clazz.
* If no Morphers are found it will return an array containing the * IdentityObjectMorpher. * * @param clazz the target class for which a Morpher or Morphers may be * associated */ public synchronized Morpher[] getMorphersFor( Class clazz ) { List registered = (List) morphers.get( clazz ); if( registered == null || registered.isEmpty() ){ // no morphers registered for clazz return new Morpher[] { IdentityObjectMorpher.getInstance() }; }else{ Morpher[] morphs = new Morpher[registered.size()]; int k = 0; for( Iterator i = registered.iterator(); i.hasNext(); ){ morphs[k++] = (Morpher) i.next(); } return morphs; } } /** * Morphs and object to the specified target class.
* This method uses reflection to invoke primitive Morphers and Morphers that * do not implement ObjectMorpher. * * @param target the target class to morph to * @param value the value to morph * @return an instance of the target class if a suitable Morpher was found * @throws MorphException if an error occurs during the conversion */ public Object morph( Class target, Object value ) { if( value == null ){ // give the first morpher in the list a shot to convert // the value as we can't access type information on it Morpher morpher = getMorpherFor( target ); if( morpher instanceof ObjectMorpher ){ return ((ObjectMorpher) morpher).morph( value ); }else{ try{ Method morphMethod = morpher.getClass() .getDeclaredMethod( "morph", new Class[] { Object.class } ); return morphMethod.invoke( morpher, new Object[] { value } ); } catch( Exception e ){ throw new MorphException( e ); } } } Morpher[] morphers = getMorphersFor( target ); for( int i = 0; i < morphers.length; i++ ){ Morpher morpher = morphers[i]; if( morpher.supports( value.getClass() ) ){ if( morpher instanceof ObjectMorpher ){ return ((ObjectMorpher) morpher).morph( value ); }else{ try{ Method morphMethod = morpher.getClass() .getDeclaredMethod( "morph", new Class[] { Object.class } ); return morphMethod.invoke( morpher, new Object[] { value } ); } catch( Exception e ){ throw new MorphException( e ); } } } } return value; } /** * Register a Morpher for a target Class.
* The target class is the class this Morpher morphs to. If there are another * morphers registered to that class, it will be appended to a List. * * @param morpher a Morpher to register. The method morphsTo() * is used to associate the Morpher to a target Class */ public void registerMorpher( Morpher morpher ) { registerMorpher( morpher, false ); } /** * Register a Morpher for a target Class.
* The target class is the class this Morpher morphs to. If there are another * morphers registered to that class, it will be appended to a List. * * @param morpher a Morpher to register. The method morphsTo() * is used to associate the Morpher to a target Class * @param override if registering teh Morpher should override all previously * registered morphers for the target type */ public synchronized void registerMorpher( Morpher morpher, boolean override ) { List registered = (List) morphers.get( morpher.morphsTo() ); if( override || registered == null ){ registered = new ArrayList(); morphers.put( morpher.morphsTo(), registered ); } if( !registered.contains( morpher ) ){ registered.add( morpher ); } } }ezmorph-1.0.6/net/sf/ezmorph/MorphException.java0000644000175000017500000000464310654104342021637 0ustar twernertwerner/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ezmorph; import org.apache.commons.lang.exception.NestableRuntimeException; /** * A MorphException indicates that a call to * Morpher.morph() has failed to complete successfully.
* Based on common-beauntils ConversionException.
* * @author Andres Almiray */ public class MorphException extends NestableRuntimeException { private static final long serialVersionUID = -540093801787033824L; // ----------------------------------------------------------- Constructors /** * The root cause of this ConversionException, compatible * with JDK 1.4's extensions to java.lang.Throwable. */ protected Throwable cause = null; /** * Construct a new exception with the specified message. * * @param message The message describing this exception */ public MorphException( String message ) { super( message ); } /** * Construct a new exception with the specified message and root cause. * * @param message The message describing this exception * @param cause The root cause of this exception */ public MorphException( String message, Throwable cause ) { super( message ); this.cause = cause; } // ------------------------------------------------------------- Properties /** * Construct a new exception with the specified root cause. * * @param cause The root cause of this exception */ public MorphException( Throwable cause ) { super( cause.getMessage() ); this.cause = cause; } /** * Returns the cause of this exception. * * @return a Throwable that represents the cause of this exception */ public Throwable getCause() { return this.cause; } }