Coverage Report - org.apache.shindig.social.core.util.xstream.ImplicitCollectionFieldMapping
 
Classes in this File Line Coverage Branch Coverage Complexity
ImplicitCollectionFieldMapping
82%
9/11
50%
4/8
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.thoughtworks.xstream.mapper.Mapper.ImplicitCollectionMapping;
 21  
 
 22  
 /**
 23  
  * <p>
 24  
  * ItemFieldMapping defines a mapping of a class within a class to an element
 25  
  * name. Where classes are tested, the must implement or extend the specified
 26  
  * classes, unlike the standard behaviour of XStream they dont need to be the
 27  
  * classes in question.
 28  
  * </p>
 29  
  * <p>
 30  
  * The structure is used for implicit collections of the form *
 31  
  * </p>
 32  
  *
 33  
  * <pre>
 34  
  * &lt;outerobject&gt;
 35  
  *    &lt;listelement&gt;
 36  
  *       &lt;objectcontent&gt;
 37  
  *    &lt;/listelement&gt;
 38  
  *    &lt;listelement&gt;
 39  
  *       &lt;objectcontent&gt;
 40  
  *    &lt;/listelement&gt;
 41  
  * ...
 42  
  * &lt;/outerobject&gt;
 43  
  * </pre>
 44  
  * <p>
 45  
  * or
 46  
  * </p>
 47  
  *
 48  
  * <pre>
 49  
  * &lt;person&gt;
 50  
  *     &lt;emails&gt;
 51  
  *        &lt;type&gt;&lt;/type&gt;
 52  
  *        &lt;value&gt;&lt;/value&gt;
 53  
  *     &lt;/emails&gt;
 54  
  *     &lt;emails&gt;
 55  
  *        &lt;type&gt;&lt;/type&gt;
 56  
  *        &lt;value&gt;&lt;/value&gt;
 57  
  *     &lt;/emails&gt;
 58  
  *     &lt;emails&gt;
 59  
  *        &lt;type&gt;&lt;/type&gt;
 60  
  *        &lt;value&gt;&lt;/value&gt;
 61  
  *     &lt;/emails&gt;
 62  
  *     ...
 63  
  * &lt;/person&gt;
 64  
  * </pre>
 65  
  * <p>
 66  
  * would be specified with NewItemFieldMapping(Person.class, "emails",
 67  
  * ListField.class, "emails");
 68  
  * </p>
 69  
  */
 70  
 public class ImplicitCollectionFieldMapping implements ImplicitCollectionMapping {
 71  
 
 72  
   /**
 73  
    * The Class that the field is defined in.
 74  
    */
 75  
   private Class<?> definedIn;
 76  
   /**
 77  
    * The class of the item that is being defined.
 78  
    */
 79  
   private Class<?> itemType;
 80  
   /**
 81  
    * The name of the fields in the class (get and set methods)
 82  
    */
 83  
   private String fieldName;
 84  
   /**
 85  
    * The name of the element that should be used for this field.
 86  
    */
 87  
   private String itemFieldName;
 88  
 
 89  
   /**
 90  
    * Create a Item Field Mapping object specifying that where the class itemType
 91  
    * appears in the Class definedIn, the elementName should be used for the
 92  
    * Element Name.
 93  
    *
 94  
    * @param definedIn
 95  
    *          the class which contains the method
 96  
    * @param fieldName
 97  
    *          the name of the method/field in the class.
 98  
    * @param itemType
 99  
    *          the type of the method/field in the class.
 100  
    * @param itemFieldName
 101  
    *          the name of element in the xml
 102  
    *
 103  
    */
 104  
   public ImplicitCollectionFieldMapping(Class<?> definedIn, String fieldName,
 105  26
       Class<?> itemType, String itemFieldName) {
 106  26
     this.definedIn = definedIn;
 107  26
     this.itemType = itemType;
 108  26
     this.itemFieldName = itemFieldName;
 109  26
     this.fieldName = fieldName;
 110  26
   }
 111  
 
 112  
   /**
 113  
    * Does this ItemFieldMapping match the supplied classes.
 114  
    *
 115  
    * @param definedIn
 116  
    *          the class that the target test class is defiend in, this is a real
 117  
    *          class
 118  
    * @param itemType
 119  
    *          the target class, the real class
 120  
    * @return true if the defiendIn class implements the defiendIn class of this
 121  
    *         ItemFieldMapping and the itemType class implements the itemType
 122  
    *         class of this ItemFieldMapping.
 123  
    */
 124  
   public boolean matches(Class<?> definedIn, Class<?> itemType) {
 125  0
     return (this.definedIn.isAssignableFrom(definedIn) && this.itemType
 126  
         .isAssignableFrom(itemType));
 127  
   }
 128  
 
 129  
   public boolean matches(Class<?> definedIn, String fieldName) {
 130  8411
     return (this.definedIn.isAssignableFrom(definedIn) && this.fieldName
 131  
         .equals(fieldName));
 132  
   }
 133  
 
 134  
   /**
 135  
    * @return
 136  
    */
 137  
   public String getFieldName() {
 138  0
     return fieldName;
 139  
   }
 140  
 
 141  
   /**
 142  
    * @return
 143  
    */
 144  
   public String getItemFieldName() {
 145  124
     return itemFieldName;
 146  
   }
 147  
 
 148  
   /**
 149  
    * @return
 150  
    */
 151  
   public Class<?> getItemType() {
 152  76
     return itemType;
 153  
   }
 154  
 
 155  
 }