| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SwitchingMorpher |
|
| 4.0;4 |
| 1 | /* | |
| 2 | * Copyright 2006-2007 the original author or authors. | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | ||
| 17 | package net.sf.ezmorph.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 | 12 | private Map classMap = new HashMap(); |
| 40 | private MorpherRegistry morpherRegistry; | |
| 41 | ||
| 42 | 9 | public SwitchingMorpher( Map classMap, MorpherRegistry morpherRegistry ) |
| 43 | 3 | { |
| 44 | 12 | this.morpherRegistry = morpherRegistry; |
| 45 | 12 | if( classMap == null || classMap.isEmpty() ){ |
| 46 | 0 | throw new MorphException( "Must specify at least one mapping" ); |
| 47 | } | |
| 48 | 12 | this.classMap.putAll( classMap ); |
| 49 | 12 | } |
| 50 | ||
| 51 | public boolean equals( Object obj ) | |
| 52 | { | |
| 53 | 0 | if( this == obj ){ |
| 54 | 0 | return true; |
| 55 | } | |
| 56 | 0 | if( obj == null ){ |
| 57 | 0 | return false; |
| 58 | } | |
| 59 | ||
| 60 | 0 | if( !(obj instanceof NumberMorpher) ){ |
| 61 | 0 | return false; |
| 62 | } | |
| 63 | ||
| 64 | 0 | SwitchingMorpher other = (SwitchingMorpher) obj; |
| 65 | 0 | if( classMap.size() != other.classMap.size() ){ |
| 66 | 0 | return false; |
| 67 | } | |
| 68 | 0 | for( Iterator entries = classMap.entrySet() |
| 69 | 0 | .iterator(); entries.hasNext(); ){ |
| 70 | 0 | Map.Entry entry = (Map.Entry) entries.next(); |
| 71 | 0 | if( !other.classMap.containsKey( entry.getKey() ) ){ |
| 72 | 0 | return false; |
| 73 | } | |
| 74 | 0 | if( !entry.getValue() |
| 75 | 0 | .equals( other.classMap.get( entry.getKey() ) ) ){ |
| 76 | 0 | return false; |
| 77 | } | |
| 78 | 0 | } |
| 79 | 0 | return true; |
| 80 | } | |
| 81 | ||
| 82 | public int hashCode() | |
| 83 | { | |
| 84 | 0 | HashCodeBuilder builder = new HashCodeBuilder(); |
| 85 | 0 | for( Iterator entries = classMap.entrySet() |
| 86 | 0 | .iterator(); entries.hasNext(); ){ |
| 87 | 0 | Map.Entry entry = (Map.Entry) entries.next(); |
| 88 | 0 | builder.append( entry.getKey() ); |
| 89 | 0 | builder.append( entry.getValue() ); |
| 90 | 0 | } |
| 91 | 0 | return builder.toHashCode(); |
| 92 | } | |
| 93 | ||
| 94 | public Object morph( Object value ) | |
| 95 | { | |
| 96 | 12 | if( value == null ){ |
| 97 | 4 | return null; |
| 98 | } | |
| 99 | ||
| 100 | 8 | Class target = (Class) classMap.get( value.getClass() ); |
| 101 | 8 | return morpherRegistry.morph( target, value ); |
| 102 | } | |
| 103 | ||
| 104 | public Class morphsTo() | |
| 105 | { | |
| 106 | 0 | return Object.class; |
| 107 | } | |
| 108 | ||
| 109 | public boolean supports( Class clazz ) | |
| 110 | { | |
| 111 | 0 | return true; |
| 112 | } | |
| 113 | } |