Coverage Report - org.dozer.fieldmap.HintContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
HintContainer
91%
32/35
87%
14/16
2.429
 
 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.fieldmap;
 17  
 
 18  
 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
 19  
 import org.apache.commons.lang3.builder.ToStringStyle;
 20  
 import org.dozer.util.MappingUtils;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.StringTokenizer;
 25  
 
 26  
 /**
 27  
  * Only intended for internal use.
 28  
  * 
 29  
  * @author garsombke.franz
 30  
  * @author sullins.ben
 31  
  * @author tierney.matt
 32  
  * 
 33  
  */
 34  4521
 public class HintContainer {
 35  
   private String hintName;
 36  
   private List<Class<?>> hints;
 37  
 
 38  
   public Class<?> getHint() {
 39  
     Class<?> result;
 40  1440
     if (hasMoreThanOneHint()) {
 41  98
       return null;
 42  
     } else {
 43  1342
       result = getHints().get(0);
 44  
     }
 45  1342
     return result;
 46  
   }
 47  
 
 48  
   public Class<?> getHint(int index) {
 49  247
     return getHints().get(index);
 50  
   }
 51  
 
 52  
   public boolean hasMoreThanOneHint() {
 53  1681
     return getHints().size() > 1;
 54  
   }
 55  
 
 56  
   public List<Class<?>> getHints() {
 57  4364
     if (hints == null) {
 58  438
       List<Class<?>> list = new ArrayList<Class<?>>();
 59  438
       StringTokenizer st = new StringTokenizer(this.hintName, ",");
 60  944
       while (st.hasMoreElements()) {
 61  506
         String theHintName = st.nextToken().trim();
 62  
 
 63  506
         Class<?> clazz = MappingUtils.loadClass(theHintName);
 64  506
         list.add(clazz);
 65  506
       }
 66  438
       hints = list;
 67  
     }
 68  4364
     return hints;
 69  
   }
 70  
 
 71  
   //TODO: Refactor/Relocate.  This method doesn't seem to belong in this class
 72  
   public Class<?> getHint(Class<?> clazz, List<Class<?>> clazzHints) {
 73  544
     List<Class<?>> hints = getHints();
 74  544
     int hintsSize = hints.size();
 75  544
     if (hintsSize == 1) {
 76  354
       return getHint();
 77  
     }
 78  
     // validate sizes
 79  190
     if (clazzHints.size() != hintsSize) {
 80  0
       MappingUtils
 81  
           .throwMappingException("When using multiple source and destination hints there must be exactly the same number of hints on the source and the destination.");
 82  
     }
 83  190
     int count = 0;
 84  190
     String myClazName = MappingUtils.getRealClass(clazz).getName();
 85  190
     int size = clazzHints.size();
 86  268
     for (int i = 0; i < size; i++) {
 87  268
       Class<?> element = clazzHints.get(i);
 88  268
       if (element.getName().equals(myClazName)) {
 89  190
         return hints.get(count);
 90  
       }
 91  78
       count++;
 92  
     }
 93  0
     return clazz;
 94  
   }
 95  
 
 96  
   public void setHintName(String hintName) {
 97  4517
     this.hintName = hintName;
 98  4517
   }
 99  
 
 100  
   @Override
 101  
   public String toString() {
 102  0
     return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
 103  
   }
 104  
 }