##### Coverage Report - org.yaml.snakeyaml.nodes.Tag

Classes in this File | Line Coverage | Branch Coverage | Complexity
Tag |
98% | 74/75
100% | 22/22
| 2.6153846153846154;2.615

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.nodes;
18 |  |
19 |  |
     import java.math.BigDecimal;
20 |  |
     import java.math.BigInteger;
21 |  |
     import java.net.URI;
22 |  |
     import java.sql.Timestamp;
23 |  |
     import java.util.Date;
24 |  |
     import java.util.HashMap;
25 |  |
     import java.util.HashSet;
26 |  |
     import java.util.Map;
27 |  |
     import java.util.Set;
28 |  |
29 |  |
     import org.yaml.snakeyaml.error.YAMLException;
30 |  |
     import org.yaml.snakeyaml.util.UriEncoder;
31 |  |
32 | 0 |
     public final class Tag implements Comparable<Tag> {
33 |  |
         public static final String PREFIX = "tag:yaml.org,2002:";
34 | 1 |
         public static final Tag YAML = new Tag(PREFIX + "yaml");
35 | 1 |
         public static final Tag VALUE = new Tag(PREFIX + "value");
36 | 1 |
         public static final Tag MERGE = new Tag(PREFIX + "merge");
37 | 1 |
         public static final Tag SET = new Tag(PREFIX + "set");
38 | 1 |
         public static final Tag PAIRS = new Tag(PREFIX + "pairs");
39 | 1 |
         public static final Tag OMAP = new Tag(PREFIX + "omap");
40 | 1 |
         public static final Tag BINARY = new Tag(PREFIX + "binary");
41 | 1 |
         public static final Tag INT = new Tag(PREFIX + "int");
42 | 1 |
         public static final Tag FLOAT = new Tag(PREFIX + "float");
43 | 1 |
         public static final Tag TIMESTAMP = new Tag(PREFIX + "timestamp");
44 | 1 |
         public static final Tag BOOL = new Tag(PREFIX + "bool");
45 | 1 |
         public static final Tag NULL = new Tag(PREFIX + "null");
46 | 1 |
         public static final Tag STR = new Tag(PREFIX + "str");
47 | 1 |
         public static final Tag SEQ = new Tag(PREFIX + "seq");
48 | 1 |
         public static final Tag MAP = new Tag(PREFIX + "map");
49 |  |
         public static final Map<Tag, Set<Class<?>>> COMPATIBILITY_MAP;
50 |  |
         static {
51 | 1 |
             COMPATIBILITY_MAP = new HashMap<Tag, Set<Class<?>>>();
52 | 1 |
             Set<Class<?>> floatSet = new HashSet<Class<?>>();
53 | 1 |
             floatSet.add(Double.class);
54 | 1 |
             floatSet.add(Float.class);
55 | 1 |
             floatSet.add(BigDecimal.class);
56 | 1 |
             COMPATIBILITY_MAP.put(FLOAT, floatSet);
57 |  |
             //
58 | 1 |
             Set<Class<?>> intSet = new HashSet<Class<?>>();
59 | 1 |
             intSet.add(Integer.class);
60 | 1 |
             intSet.add(Long.class);
61 | 1 |
             intSet.add(BigInteger.class);
62 | 1 |
             COMPATIBILITY_MAP.put(INT, intSet);
63 |  |
             //
64 | 1 |
             Set<Class<?>> timestampSet = new HashSet<Class<?>>();
65 | 1 |
             timestampSet.add(Date.class);
66 | 1 |
             timestampSet.add(java.sql.Date.class);
67 | 1 |
             timestampSet.add(Timestamp.class);
68 | 1 |
             COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet);
69 | 1 |
         }
70 |  |
71 |  |
         private final String value;
72 |  |
73 | 6274 |
         public Tag(String tag) {
74 | 6274 |
             if (tag == null) {
75 | 1 |
                 throw new NullPointerException("Tag must be provided.");
76 | 6273 |
             } else if (tag.length() == 0) {
77 | 1 |
                 throw new IllegalArgumentException("Tag must not be empty.");
78 | 6272 |
             } else if (tag.trim().length() != tag.length()) {
79 | 1 |
                 throw new IllegalArgumentException("Tag must not contain leading or trailing spaces.");
80 |  |
             }
81 | 6271 |
             this.value = UriEncoder.encode(tag);
82 | 6271 |
         }
83 |  |
84 | 41527 |
         public Tag(Class<? extends Object> clazz) {
85 | 41527 |
             if (clazz == null) {
86 | 3 |
                 throw new NullPointerException("Class for tag must be provided.");
87 |  |
             }
88 | 41524 |
             this.value = Tag.PREFIX + UriEncoder.encode(clazz.getName());
89 | 41524 |
         }
90 |  |
91 | 2 |
         public Tag(URI uri) {
92 | 2 |
             if (uri == null) {
93 | 1 |
                 throw new NullPointerException("URI for tag must be provided.");
94 |  |
             }
95 | 1 |
             this.value = uri.toASCIIString();
96 | 1 |
         }
97 |  |
98 |  |
         public String getValue() {
99 | 2221762 |
             return value;
100 |  |
         }
101 |  |
102 |  |
         public boolean startsWith(String prefix) {
103 | 434 |
             return value.startsWith(prefix);
104 |  |
         }
105 |  |
106 |  |
         public String getClassName() {
107 | 312 |
             if (!value.startsWith(Tag.PREFIX)) {
108 | 4 |
                 throw new YAMLException("Invalid tag: " + value);
109 |  |
             }
110 | 308 |
             return UriEncoder.decode(value.substring(Tag.PREFIX.length()));
111 |  |
         }
112 |  |
113 |  |
         public int getLength() {
114 | 1 |
             return value.length();
115 |  |
         }
116 |  |
117 |  |
         @Override
118 |  |
         public String toString() {
119 | 95 |
             return value;
120 |  |
         }
121 |  |
122 |  |
         @Override
123 |  |
         public boolean equals(Object obj) {
124 | 1556697 |
             if (obj == null) {
125 | 1 |
                 return false;
126 |  |
             }
127 | 1556696 |
             if (obj instanceof Tag) {
128 | 1556689 |
                 return value.equals(((Tag) obj).getValue());
129 | 7 |
             } else if (obj instanceof String) {
130 | 6 |
                 if (value.equals(obj.toString())) {
131 |  |
                     // TODO to be removed later (version 2.0?)
132 | 2 |
                     System.err.println("Comparing Tag and String is deprecated.");
133 | 2 |
                     return true;
134 |  |
                 }
135 |  |
             }
136 | 5 |
             return false;
137 |  |
         }
138 |  |
139 |  |
         @Override
140 |  |
         public int hashCode() {
141 | 814393 |
             return value.hashCode();
142 |  |
         }
143 |  |
144 |  |
         /**
145 |  |
          * Java has more then 1 class compatible with a language-independent tag
146 |  |
          * (!!int, !!float, !!timestamp etc)
147 |  |
          *
148 |  |
          * @param clazz
149 |  |
          *            - Class to check compatibility
150 |  |
          * @return true when the Class can be represented by this
151 |  |
          *         language-independent tag
152 |  |
          */
153 |  |
         public boolean isCompatible(Class<?> clazz) {
154 | 380095 |
             Set<Class<?>> set = COMPATIBILITY_MAP.get(this);
155 | 380095 |
             if (set != null) {
156 | 101135 |
                 return set.contains(clazz);
157 |  |
             } else {
158 | 278960 |
                 return false;
159 |  |
             }
160 |  |
         }
161 |  |
162 |  |
         /**
163 |  |
          * Check whether this tag matches the global tag for the Class
164 |  |
          *
165 |  |
          * @param clazz
166 |  |
          *            - Class to check
167 |  |
          * @return true when the this tag can be used as a global tag for the Class
168 |  |
          */
169 |  |
         public boolean matches(Class<? extends Object> clazz) {
170 | 200116 |
             return value.equals(Tag.PREFIX + clazz.getName());
171 |  |
         }
172 |  |
173 |  |
         public int compareTo(Tag o) {
174 | 1 |
             return value.compareTo(o.getValue());
175 |  |
         }
176 |  |
     }