Coverage Report - org.apache.shindig.social.core.util.xstream.InterfaceFieldAliasingMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
InterfaceFieldAliasingMapper
35%
12/34
12%
3/24
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util.xstream;
 19  
 
 20  
 import com.google.common.collect.Lists;
 21  
 import com.google.common.collect.Maps;
 22  
 
 23  
 import com.thoughtworks.xstream.mapper.FieldAliasingMapper;
 24  
 import com.thoughtworks.xstream.mapper.Mapper;
 25  
 import com.thoughtworks.xstream.mapper.MapperWrapper;
 26  
 
 27  
 import java.util.ArrayList;
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * 
 34  
  */
 35  
 public class InterfaceFieldAliasingMapper extends MapperWrapper {
 36  
 
 37  222
   private Map<String, List<InterfaceFieldAliasMapping>> serializedMap = Maps.newHashMap();
 38  222
   private Map<String, List<InterfaceFieldAliasMapping>> membersMap = Maps.newHashMap();
 39  
   private WriterStack writerStack;
 40  
 
 41  
   /**
 42  
    * @param wrapped
 43  
    */
 44  
   public InterfaceFieldAliasingMapper(Mapper wrapped, WriterStack writerStack,
 45  
       List<InterfaceFieldAliasMapping> ifaList) {
 46  222
     super(wrapped);
 47  222
     this.writerStack = writerStack;
 48  222
     for (InterfaceFieldAliasMapping ifa : ifaList) {
 49  
 
 50  0
       List<InterfaceFieldAliasMapping> serializedMatches = serializedMap.get(ifa.getFieldName());
 51  0
       if (serializedMatches == null) {
 52  0
         serializedMatches = Lists.newArrayList();
 53  0
         serializedMap.put(ifa.getFieldName(), serializedMatches);
 54  
       }
 55  0
       serializedMatches.add(ifa);
 56  0
       List<InterfaceFieldAliasMapping> memberMatches = membersMap.get(ifa.getAlias());
 57  0
       if (memberMatches == null) {
 58  0
         memberMatches = Lists.newArrayList();
 59  0
         membersMap.put(ifa.getAlias(), memberMatches);
 60  
       }
 61  0
       memberMatches.add(ifa);
 62  0
     }
 63  222
   }
 64  
 
 65  
   /**
 66  
    * {@inheritDoc}
 67  
    * 
 68  
    * @see com.thoughtworks.xstream.mapper.MapperWrapper#realMember(java.lang.Class,
 69  
    *      java.lang.String)
 70  
    */
 71  
   @Override
 72  
   public String realMember(Class type, String serialized) {
 73  
     // get the possible member spec, using the serialized elment as the key.
 74  
     // comes from the map of members.
 75  14
     List<InterfaceFieldAliasMapping> serializedMatches = membersMap
 76  
         .get(serialized);
 77  14
     if (serializedMatches != null) {
 78  0
       for (InterfaceFieldAliasMapping ifa : serializedMatches) {
 79  0
         if (ifa.getType().isAssignableFrom(type)) {
 80  0
           return ifa.getFieldName();
 81  
         }
 82  0
       }
 83  
     }
 84  14
     return super.realMember(type, serialized);
 85  
   }
 86  
 
 87  
   /**
 88  
    * {@inheritDoc}
 89  
    * 
 90  
    * @see com.thoughtworks.xstream.mapper.MapperWrapper#serializedMember(java.lang.Class,
 91  
    *      java.lang.String)
 92  
    */
 93  
   @Override
 94  
   public String serializedMember(Class type, String memberName) {
 95  
     // get the possible serialized spec, using the memberName elment as the key.
 96  
     // comes from the map of serialized elements.
 97  373
     List<InterfaceFieldAliasMapping> memberMatches = serializedMap
 98  
         .get(memberName);
 99  373
     if (memberMatches != null) {
 100  0
       for (InterfaceFieldAliasMapping ifa : memberMatches) {
 101  0
         if (ifa.getParent() == null) {
 102  0
           if (ifa.getType().isAssignableFrom(type)) {
 103  0
             return ifa.getAlias();
 104  
           }
 105  
         } else {
 106  0
           if (ifa.getType().isAssignableFrom(type)
 107  
               && ifa.getParent().equals(writerStack.peek())) {
 108  0
             return ifa.getAlias();
 109  
           }
 110  
         }
 111  0
       }
 112  
     }
 113  373
     return super.serializedMember(type, memberName);
 114  
   }
 115  
 
 116  
 }