Home | History | Annotate | Download | only in snakeyaml
      1 /**
      2  * Copyright (c) 2008, 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     public TypeDescription(Class<? extends Object> clazz, Tag tag) {
     35         this.type = clazz;
     36         this.tag = tag;
     37         listProperties = new HashMap<String, Class<? extends Object>>();
     38         keyProperties = new HashMap<String, Class<? extends Object>>();
     39         valueProperties = new HashMap<String, Class<? extends Object>>();
     40     }
     41 
     42     public TypeDescription(Class<? extends Object> clazz, String tag) {
     43         this(clazz, new Tag(tag));
     44     }
     45 
     46     public TypeDescription(Class<? extends Object> clazz) {
     47         this(clazz, (Tag) null);
     48     }
     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         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         this.tag = tag;
     68     }
     69 
     70     public void setTag(String tag) {
     71         setTag(new Tag(tag));
     72     }
     73 
     74     /**
     75      * Get represented type (class)
     76      *
     77      * @return type (class) to be described.
     78      */
     79     public Class<? extends Object> getType() {
     80         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         listProperties.put(property, type);
     93     }
     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         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         keyProperties.put(property, key);
    119         valueProperties.put(property, value);
    120     }
    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         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         return valueProperties.get(property);
    142     }
    143 
    144     @Override
    145     public String toString() {
    146         return "TypeDescription for " + getType() + " (tag='" + getTag() + "')";
    147     }
    148 }
    149