View Javadoc

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