Coverage Report - org.apache.shindig.social.core.util.xstream.ClassFieldMapping
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassFieldMapping
50%
8/16
17%
1/6
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  
 /**
 21  
  * This represents the mapping between a class and a field, potentially with a
 22  
  * parent element. It is used to define the element names that are used to
 23  
  * serialize the contents of a class.
 24  
  * 
 25  
  * eg
 26  
  * <pre>
 27  
  * &lt;outerobject&gt;
 28  
  * &lt;listcontainer&gt;
 29  
  *    &lt;listelement&gt;
 30  
  *       &lt;objectcontent&gt;
 31  
  *    &lt;/listelement&gt;
 32  
  * &lt;/listcontainer&gt;
 33  
  * ...
 34  
  * &lt;/outerobject&gt;
 35  
  * </pre>
 36  
  * or (not currently used in OS)
 37  
  * <pre>
 38  
  * &lt;person&gt;
 39  
  *    &lt;emails&gt;
 40  
  *       &lt;email&gt;
 41  
  *          &lt;type&gt;&lt;/type&gt;
 42  
  *          &lt;value&gt;&lt;/value&gt;
 43  
  *       &lt;/email&gt;
 44  
  *       ...
 45  
  *    &lt;/emails&gt;
 46  
  *    ...
 47  
  * &lt;/person&gt;
 48  
  * </pre>
 49  
  * For a more compact mapping {@link ItemFieldMapping}.
 50  
  * 
 51  
  * 
 52  
  */
 53  
 /**
 54  
  *
 55  
  */
 56  
 public class ClassFieldMapping {
 57  
 
 58  
   /**
 59  
    * The name of the element to map the class to.
 60  
    */
 61  
   private String elementName;
 62  
 
 63  
   /**
 64  
    * The class being mapped.
 65  
    */
 66  
   private Class<?> mappedClazz;
 67  
   /**
 68  
    * An optional parent element name.
 69  
    */
 70  
   private String fieldParentName;
 71  
 
 72  
   /**
 73  
    * Create a simple element class mapping, applicable to all parent elements.
 74  
    * 
 75  
    * @param elementName
 76  
    *          the name of the element
 77  
    * @param mappedClazz
 78  
    *          the class to map to the name of the element
 79  
    */
 80  33
   public ClassFieldMapping(String elementName, Class<?> mappedClazz) {
 81  33
     this.elementName = elementName;
 82  33
     this.mappedClazz = mappedClazz;
 83  33
     this.fieldParentName = null;
 84  33
   }
 85  
 
 86  
   /**
 87  
    * Create a element class mapping, that only applies to one parent element
 88  
    * name.
 89  
    * 
 90  
    * @param parentName
 91  
    *          the name of the parent element that this mapping applies to
 92  
    * @param elementName
 93  
    *          the name of the element
 94  
    * @param mappedClazz
 95  
    *          the class to map to the name of the element
 96  
    */
 97  
   public ClassFieldMapping(String parentName, String elementName,
 98  0
       Class<?> mappedClazz) {
 99  0
     this.elementName = elementName;
 100  0
     this.mappedClazz = mappedClazz;
 101  0
     this.fieldParentName = parentName;
 102  0
   }
 103  
 
 104  
   /**
 105  
    * @return get the element name.
 106  
    */
 107  
   public String getElementName() {
 108  67
     return elementName;
 109  
   }
 110  
 
 111  
   /**
 112  
    * @return get the mapped class.
 113  
    */
 114  
   public Class<?> getMappedClass() {
 115  0
     return mappedClazz;
 116  
   }
 117  
 
 118  
   /**
 119  
    * Does this ClassFieldMapping match the supplied parent and type.
 120  
    * 
 121  
    * @param parent
 122  
    *          the parent element, which may be null
 123  
    * @param type
 124  
    *          the type of the field being stored
 125  
    * @return true if this mapping is a match for the combination
 126  
    */
 127  
   public boolean matches(String parent, Class<?> type) {
 128  1175
     if (fieldParentName == null) {
 129  1175
       return mappedClazz.isAssignableFrom(type);
 130  
     } else {
 131  0
       boolean result = fieldParentName.equals(parent)
 132  
           && mappedClazz.isAssignableFrom(type);
 133  0
       return result;
 134  
     }
 135  
   }
 136  
 
 137  
 }