| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ClassFieldMapping |
|
| 0.0;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 | * <outerobject> | |
| 28 | * <listcontainer> | |
| 29 | * <listelement> | |
| 30 | * <objectcontent> | |
| 31 | * </listelement> | |
| 32 | * </listcontainer> | |
| 33 | * ... | |
| 34 | * </outerobject> | |
| 35 | * </pre> | |
| 36 | * or (not currently used in OS) | |
| 37 | * <pre> | |
| 38 | * <person> | |
| 39 | * <emails> | |
| 40 | * <email> | |
| 41 | * <type></type> | |
| 42 | * <value></value> | |
| 43 | * </email> | |
| 44 | * ... | |
| 45 | * </emails> | |
| 46 | * ... | |
| 47 | * </person> | |
| 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 | } |