Coverage Report - org.dozer.metadata.DozerMappingMetadata
 
Classes in this File Line Coverage Branch Coverage Complexity
DozerMappingMetadata
100%
43/43
96%
31/32
3.182
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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  
 package org.dozer.metadata;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.dozer.classmap.ClassMap;
 22  
 import org.dozer.classmap.ClassMappings;
 23  
 
 24  
 /**
 25  
  * Internal use only.
 26  
  * @author  Florian Kunz
 27  
  */
 28  
 public final class DozerMappingMetadata implements MappingMetadata {
 29  
         
 30  
         private final ClassMappings classMappings;
 31  
 
 32  48
         public DozerMappingMetadata(ClassMappings classMappings) {
 33  48
                 this.classMappings = classMappings;
 34  48
         }
 35  
 
 36  
         public List<ClassMappingMetadata> getClassMappings() {
 37  2
                 List<ClassMappingMetadata> classMapMetadata = new ArrayList<ClassMappingMetadata>();
 38  
                 
 39  2
                 for(ClassMap classMap : classMappings.getAll().values()) {
 40  8
                         classMapMetadata.add(new DozerClassMappingMetadata(classMap));
 41  8
                 }
 42  2
                 return classMapMetadata;
 43  
         }
 44  
 
 45  
         public List<ClassMappingMetadata> getClassMappingsBySourceName(String sourceClassName) {
 46  4
                 if (sourceClassName == null) {
 47  2
                         throw new IllegalArgumentException("The source class name cannot be null.");
 48  
                 }
 49  
                 
 50  2
                 return buildMappingListBySourceName(sourceClassName);
 51  
         }
 52  
 
 53  
         public List<ClassMappingMetadata> getClassMappingsByDestinationName(String destinationClassName) {
 54  4
                 if (destinationClassName == null) {
 55  2
                         throw new IllegalArgumentException("The destination class name cannot be null.");
 56  
                 }
 57  
                 
 58  2
                 return buildMappingListByDestinationName(destinationClassName);
 59  
         }
 60  
 
 61  
 
 62  
         public ClassMappingMetadata getClassMappingByName(String sourceClassName, String destinationClassName) {
 63  22
                 if (sourceClassName == null || destinationClassName == null) {
 64  4
                         throw new IllegalArgumentException("The source and destination class names need to be specified.");
 65  
                 }
 66  
                 
 67  18
                 return findMappingByName(sourceClassName, destinationClassName);
 68  
         }
 69  
 
 70  
 
 71  
         public List<ClassMappingMetadata> getClassMappingsBySource(Class<?> sourceClass) {
 72  4
                 if (sourceClass == null) {
 73  2
                         throw new IllegalArgumentException("The source class cannt be null.");
 74  
                 }
 75  
                 
 76  2
                 return buildMappingListBySourceName(sourceClass.getName());
 77  
         }
 78  
 
 79  
         public List<ClassMappingMetadata> getClassMappingsByDestination(Class<?> destinationClass) {
 80  4
                 if (destinationClass == null) {
 81  2
                         throw new IllegalArgumentException("The destination class cannot be null.");
 82  
                 }
 83  
                 
 84  2
                 return buildMappingListByDestinationName(destinationClass.getName());
 85  
         }
 86  
 
 87  
         public ClassMappingMetadata getClassMapping(Class<?> sourceClass, Class<?> destinationClass) {
 88  8
                 if (sourceClass == null || destinationClass == null) {
 89  4
                         throw new IllegalArgumentException("The source and destination classes need to be specified.");
 90  
                 }
 91  
                 
 92  4
                 return findMappingByName(sourceClass.getName(), destinationClass.getName());
 93  
         }
 94  
 
 95  
         private List<ClassMappingMetadata> buildMappingListBySourceName(String sourceClassName) {
 96  4
                 List<ClassMappingMetadata> classMapMetadata = new ArrayList<ClassMappingMetadata>();
 97  4
                 for(ClassMap classMap : classMappings.getAll().values()) {
 98  16
                         if (classMap.getSrcClassName().equals(sourceClassName)) {
 99  4
                                 classMapMetadata.add(new DozerClassMappingMetadata(classMap));
 100  
                         }
 101  16
                 }
 102  4
                 return classMapMetadata;
 103  
         }
 104  
         
 105  
         private List<ClassMappingMetadata> buildMappingListByDestinationName(String destinationClassName) {
 106  4
                 List<ClassMappingMetadata> classMapMetadata = new ArrayList<ClassMappingMetadata>();
 107  4
                 for(ClassMap classMap : classMappings.getAll().values()) {
 108  16
                         if (classMap.getDestClassName().equals(destinationClassName)) {
 109  4
                                 classMapMetadata.add(new DozerClassMappingMetadata(classMap));
 110  
                         }
 111  16
                 }
 112  4
                 return classMapMetadata;
 113  
         }
 114  
         
 115  
         private ClassMappingMetadata findMappingByName(String sourceClassName, String destinationClassName) {
 116  22
                 for(ClassMap classMap : classMappings.getAll().values()) {
 117  70
                         if (classMap.getSrcClassName().equals(sourceClassName)
 118  
                                         && classMap.getDestClassName().equals(destinationClassName)) {
 119  18
                                 return new DozerClassMappingMetadata(classMap);
 120  
                         }
 121  52
                 }
 122  4
                 throw new MetadataLookupException("No mapping definition found for: " + sourceClassName 
 123  
                                 + " -> " + destinationClassName + "." );
 124  
         }
 125  
         
 126  
 }