| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TypeDescription |
|
| 1.0;1 |
| 1 | /** | |
| 2 | * Copyright (c) 2008-2012, http://www.snakeyaml.org | |
| 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.yaml.snakeyaml; | |
| 17 | ||
| 18 | import java.util.HashMap; | |
| 19 | import java.util.Map; | |
| 20 | ||
| 21 | import org.yaml.snakeyaml.nodes.Tag; | |
| 22 | ||
| 23 | /** | |
| 24 | * Provides additional runtime information necessary to create a custom Java | |
| 25 | * instance. | |
| 26 | */ | |
| 27 | public final class TypeDescription { | |
| 28 | private final Class<? extends Object> type; | |
| 29 | private Tag tag; | |
| 30 | private Map<String, Class<? extends Object>> listProperties; | |
| 31 | private Map<String, Class<? extends Object>> keyProperties; | |
| 32 | private Map<String, Class<? extends Object>> valueProperties; | |
| 33 | ||
| 34 | 132990 | public TypeDescription(Class<? extends Object> clazz, Tag tag) { |
| 35 | 132990 | this.type = clazz; |
| 36 | 132990 | this.tag = tag; |
| 37 | 132990 | listProperties = new HashMap<String, Class<? extends Object>>(); |
| 38 | 132990 | keyProperties = new HashMap<String, Class<? extends Object>>(); |
| 39 | 132990 | valueProperties = new HashMap<String, Class<? extends Object>>(); |
| 40 | 132990 | } |
| 41 | ||
| 42 | public TypeDescription(Class<? extends Object> clazz, String tag) { | |
| 43 | 23 | this(clazz, new Tag(tag)); |
| 44 | 23 | } |
| 45 | ||
| 46 | public TypeDescription(Class<? extends Object> clazz) { | |
| 47 | 132960 | this(clazz, (Tag) null); |
| 48 | 132960 | } |
| 49 | ||
| 50 | /** | |
| 51 | * Get tag which shall be used to load or dump the type (class). | |
| 52 | * | |
| 53 | * @return tag to be used. It may be a tag for Language-Independent Types | |
| 54 | * (http://www.yaml.org/type/) | |
| 55 | */ | |
| 56 | public Tag getTag() { | |
| 57 | 132987 | return tag; |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Set tag to be used to load or dump the type (class). | |
| 62 | * | |
| 63 | * @param tag | |
| 64 | * local or global tag | |
| 65 | */ | |
| 66 | public void setTag(Tag tag) { | |
| 67 | 2 | this.tag = tag; |
| 68 | 2 | } |
| 69 | ||
| 70 | public void setTag(String tag) { | |
| 71 | 1 | setTag(new Tag(tag)); |
| 72 | 1 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Get represented type (class) | |
| 76 | * | |
| 77 | * @return type (class) to be described. | |
| 78 | */ | |
| 79 | public Class<? extends Object> getType() { | |
| 80 | 398961 | return type; |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Specify that the property is a type-safe <code>List</code>. | |
| 85 | * | |
| 86 | * @param property | |
| 87 | * name of the JavaBean property | |
| 88 | * @param type | |
| 89 | * class of List values | |
| 90 | */ | |
| 91 | public void putListPropertyType(String property, Class<? extends Object> type) { | |
| 92 | 10 | listProperties.put(property, type); |
| 93 | 10 | } |
| 94 | ||
| 95 | /** | |
| 96 | * Get class of List values for provided JavaBean property. | |
| 97 | * | |
| 98 | * @param property | |
| 99 | * property name | |
| 100 | * @return class of List values | |
| 101 | */ | |
| 102 | public Class<? extends Object> getListPropertyType(String property) { | |
| 103 | 47 | return listProperties.get(property); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Specify that the property is a type-safe <code>Map</code>. | |
| 108 | * | |
| 109 | * @param property | |
| 110 | * property name of this JavaBean | |
| 111 | * @param key | |
| 112 | * class of keys in Map | |
| 113 | * @param value | |
| 114 | * class of values in Map | |
| 115 | */ | |
| 116 | public void putMapPropertyType(String property, Class<? extends Object> key, | |
| 117 | Class<? extends Object> value) { | |
| 118 | 9 | keyProperties.put(property, key); |
| 119 | 9 | valueProperties.put(property, value); |
| 120 | 9 | } |
| 121 | ||
| 122 | /** | |
| 123 | * Get keys type info for this JavaBean | |
| 124 | * | |
| 125 | * @param property | |
| 126 | * property name of this JavaBean | |
| 127 | * @return class of keys in the Map | |
| 128 | */ | |
| 129 | public Class<? extends Object> getMapKeyType(String property) { | |
| 130 | 152 | return keyProperties.get(property); |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Get values type info for this JavaBean | |
| 135 | * | |
| 136 | * @param property | |
| 137 | * property name of this JavaBean | |
| 138 | * @return class of values in the Map | |
| 139 | */ | |
| 140 | public Class<? extends Object> getMapValueType(String property) { | |
| 141 | 27 | return valueProperties.get(property); |
| 142 | } | |
| 143 | ||
| 144 | @Override | |
| 145 | public String toString() { | |
| 146 | 1 | return "TypeDescription for " + getType() + " (tag='" + getTag() + "')"; |
| 147 | } | |
| 148 | } |