View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.ezmorph.object;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import net.sf.ezmorph.MorphException;
24  import net.sf.ezmorph.MorpherRegistry;
25  import net.sf.ezmorph.ObjectMorpher;
26  
27  import org.apache.commons.lang.builder.HashCodeBuilder;
28  
29  /**
30   * An all-purpose Morpher that can morph to several classes.<br>
31   * Because this Morpher accepts any class and morphs to Object it should not be
32   * added to a MorpherRegistry as it may be too generic for some cases and may
33   * result in unwanted transformations.
34   *
35   * @author Andres Almiray <aalmiray@users.sourceforge.net>
36   */
37  public class SwitchingMorpher implements ObjectMorpher
38  {
39     private Map classMap = new HashMap();
40     private MorpherRegistry morpherRegistry;
41  
42     public SwitchingMorpher( Map classMap, MorpherRegistry morpherRegistry )
43     {
44        this.morpherRegistry = morpherRegistry;
45        if( classMap == null || classMap.isEmpty() ){
46           throw new MorphException( "Must specify at least one mapping" );
47        }
48        this.classMap.putAll( classMap );
49     }
50  
51     public boolean equals( Object obj )
52     {
53        if( this == obj ){
54           return true;
55        }
56        if( obj == null ){
57           return false;
58        }
59  
60        if( !(obj instanceof NumberMorpher) ){
61           return false;
62        }
63  
64        SwitchingMorpher other = (SwitchingMorpher) obj;
65        if( classMap.size() != other.classMap.size() ){
66           return false;
67        }
68        for( Iterator entries = classMap.entrySet()
69              .iterator(); entries.hasNext(); ){
70           Map.Entry entry = (Map.Entry) entries.next();
71           if( !other.classMap.containsKey( entry.getKey() ) ){
72              return false;
73           }
74           if( !entry.getValue()
75                 .equals( other.classMap.get( entry.getKey() ) ) ){
76              return false;
77           }
78        }
79        return true;
80     }
81  
82     public int hashCode()
83     {
84        HashCodeBuilder builder = new HashCodeBuilder();
85        for( Iterator entries = classMap.entrySet()
86              .iterator(); entries.hasNext(); ){
87           Map.Entry entry = (Map.Entry) entries.next();
88           builder.append( entry.getKey() );
89           builder.append( entry.getValue() );
90        }
91        return builder.toHashCode();
92     }
93  
94     public Object morph( Object value )
95     {
96        if( value == null ){
97           return null;
98        }
99  
100       Class target = (Class) classMap.get( value.getClass() );
101       return morpherRegistry.morph( target, value );
102    }
103 
104    public Class morphsTo()
105    {
106       return Object.class;
107    }
108 
109    public boolean supports( Class clazz )
110    {
111       return true;
112    }
113 }