Coverage Report - org.dozer.MappedFieldsTracker
 
Classes in this File Line Coverage Branch Coverage Complexity
MappedFieldsTracker
100%
18/18
91%
11/12
4.5
 
 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;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.IdentityHashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 /**
 25  
  * Keeps track of mapped object during this mapping process execution.
 26  
  * Objects, which are referenced multiple types in object hierarchy will be fetched from here
 27  
  * to retain referential integrity of resulting object graph.
 28  
  *
 29  
  * @author dmitry.buzdin
 30  
  */
 31  132064
 public class MappedFieldsTracker {
 32  
 
 33  
   // Hash Code is ignored as it can serve application specific needs
 34  
   // <srcObject, <hashCodeOfDestination, mappedDestinationObject>>
 35  132064
   private final Map<Object, Map<Integer,Object>> mappedFields = new IdentityHashMap<Object, Map<Integer,Object>>();
 36  
 
 37  
   public void put(Object src, Object dest) {
 38  304372
     int destId = System.identityHashCode(dest);
 39  
 
 40  304372
     Map<Integer,Object> mappedTo = mappedFields.get(src);
 41  304372
     if (mappedTo == null) {
 42  172874
       mappedTo = new HashMap<Integer, Object>();
 43  172874
       mappedFields.put(src, mappedTo);
 44  
     }
 45  304372
     if (!mappedTo.containsKey(destId)) {
 46  172943
       mappedTo.put(destId, dest);
 47  
     }
 48  304372
   }
 49  
 
 50  
   public Object getMappedValue(Object src, Class<?> destType) {
 51  88883
     Map<Integer,Object> alreadyMappedValues = mappedFields.get(src);
 52  88883
     if (alreadyMappedValues != null) {
 53  108
       for (Object alreadyMappedValue : alreadyMappedValues.values()) {
 54  114
         if (alreadyMappedValue != null) {
 55  
           // 1664984 - bi-directionnal mapping with sets & subclasses
 56  114
           if (destType.isAssignableFrom(alreadyMappedValue.getClass())) {
 57  
             // Source value has already been mapped to the required destFieldType.
 58  59
             return alreadyMappedValue;
 59  
           }
 60  
         }
 61  55
       }
 62  
     }
 63  88824
     return null;
 64  
   }
 65  
 }