View Javadoc
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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.rdf.simple;
19  
20  import org.apache.commons.rdf.api.IRI;
21  
22  import java.util.Collections;
23  import java.util.LinkedHashSet;
24  import java.util.Optional;
25  import java.util.Set;
26  
27  /**
28   * Types from the RDF and XML Schema vocabularies.
29   */
30  public final class Types implements IRI, SimpleRDF.SimpleRDFTerm {
31  
32      /**
33       * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML</tt>
34       */
35      public static final Types RDF_HTML = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML");
36  
37      /**
38       * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#langString</tt>
39       */
40      public static final Types RDF_LANGSTRING = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
41  
42      /**
43       * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral</tt>
44       *
45       * @deprecated Not used in RDF-1.1
46       */
47      @Deprecated
48      public static final Types RDF_PLAINLITERAL = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral");
49  
50      /**
51       * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral</tt>
52       */
53      public static final Types RDF_XMLLITERAL = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral");
54  
55      /**
56       * <tt>http://www.w3.org/2001/XMLSchema#anyURI</tt>
57       */
58      public static final Types XSD_ANYURI = new Types("http://www.w3.org/2001/XMLSchema#anyURI");
59  
60      /**
61       * <tt>http://www.w3.org/2001/XMLSchema#base64Binary</tt>
62       */
63      public static final Types XSD_BASE64BINARY = new Types("http://www.w3.org/2001/XMLSchema#base64Binary");
64  
65      /**
66       * <tt>http://www.w3.org/2001/XMLSchema#boolean</tt>
67       */
68      public static final Types XSD_BOOLEAN = new Types("http://www.w3.org/2001/XMLSchema#boolean");
69  
70      /**
71       * <tt>http://www.w3.org/2001/XMLSchema#byte</tt>
72       */
73      public static final Types XSD_BYTE = new Types("http://www.w3.org/2001/XMLSchema#byte");
74  
75      /**
76       * <tt>http://www.w3.org/2001/XMLSchema#date</tt>
77       */
78      public static final Types XSD_DATE = new Types("http://www.w3.org/2001/XMLSchema#date");
79  
80      /**
81       * <tt>http://www.w3.org/2001/XMLSchema#dateTime</tt>
82       */
83      public static final Types XSD_DATETIME = new Types("http://www.w3.org/2001/XMLSchema#dateTime");
84  
85      /**
86       * <tt>http://www.w3.org/2001/XMLSchema#dayTimeDuration</tt>
87       */
88      public static final Types XSD_DAYTIMEDURATION = new Types("http://www.w3.org/2001/XMLSchema#dayTimeDuration");
89  
90      /**
91       * <tt>http://www.w3.org/2001/XMLSchema#decimal</tt>
92       */
93      public static final Types XSD_DECIMAL = new Types("http://www.w3.org/2001/XMLSchema#decimal");
94  
95      /**
96       * <tt>http://www.w3.org/2001/XMLSchema#double</tt>
97       */
98      public static final Types XSD_DOUBLE = new Types("http://www.w3.org/2001/XMLSchema#double");
99  
100     /**
101      * <tt>http://www.w3.org/2001/XMLSchema#duration</tt>
102      */
103     public static final Types XSD_DURATION = new Types("http://www.w3.org/2001/XMLSchema#duration");
104 
105     /**
106      * <tt>http://www.w3.org/2001/XMLSchema#float</tt>
107      */
108     public static final Types XSD_FLOAT = new Types("http://www.w3.org/2001/XMLSchema#float");
109 
110     /**
111      * <tt>http://www.w3.org/2001/XMLSchema#gDay</tt>
112      */
113     public static final Types XSD_GDAY = new Types("http://www.w3.org/2001/XMLSchema#gDay");
114 
115     /**
116      * <tt>http://www.w3.org/2001/XMLSchema#gMonth</tt>
117      */
118     public static final Types XSD_GMONTH = new Types("http://www.w3.org/2001/XMLSchema#gMonth");
119 
120     /**
121      * <tt>http://www.w3.org/2001/XMLSchema#gMonthDay</tt>
122      */
123     public static final Types XSD_GMONTHDAY = new Types("http://www.w3.org/2001/XMLSchema#gMonthDay");
124 
125     /**
126      * <tt>http://www.w3.org/2001/XMLSchema#gYear</tt>
127      */
128     public static final Types XSD_GYEAR = new Types("http://www.w3.org/2001/XMLSchema#gYear");
129 
130     /**
131      * <tt>http://www.w3.org/2001/XMLSchema#gYearMonth</tt>
132      */
133     public static final Types XSD_GYEARMONTH = new Types("http://www.w3.org/2001/XMLSchema#gYearMonth");
134 
135     /**
136      * <tt>http://www.w3.org/2001/XMLSchema#hexBinary</tt>
137      */
138     public static final Types XSD_HEXBINARY = new Types("http://www.w3.org/2001/XMLSchema#hexBinary");
139 
140     /**
141      * <tt>http://www.w3.org/2001/XMLSchema#int</tt>
142      */
143     public static final Types XSD_INT = new Types("http://www.w3.org/2001/XMLSchema#int");
144 
145     /**
146      * <tt>http://www.w3.org/2001/XMLSchema#integer</tt>
147      */
148     public static final Types XSD_INTEGER = new Types("http://www.w3.org/2001/XMLSchema#integer");
149 
150     /**
151      * <tt>http://www.w3.org/2001/XMLSchema#language</tt>
152      */
153     public static final Types XSD_LANGUAGE = new Types("http://www.w3.org/2001/XMLSchema#language");
154 
155     /**
156      * <tt>http://www.w3.org/2001/XMLSchema#long</tt>
157      */
158     public static final Types XSD_LONG = new Types("http://www.w3.org/2001/XMLSchema#long");
159 
160     /**
161      * <tt>http://www.w3.org/2001/XMLSchema#Name</tt>
162      */
163     public static final Types XSD_NAME = new Types("http://www.w3.org/2001/XMLSchema#Name");
164 
165     /**
166      * <tt>http://www.w3.org/2001/XMLSchema#NCName</tt>
167      */
168     public static final Types XSD_NCNAME = new Types("http://www.w3.org/2001/XMLSchema#NCName");
169 
170     /**
171      * <tt>http://www.w3.org/2001/XMLSchema#negativeInteger</tt>
172      */
173     public static final Types XSD_NEGATIVEINTEGER = new Types("http://www.w3.org/2001/XMLSchema#negativeInteger");
174 
175     /**
176      * <tt>http://www.w3.org/2001/XMLSchema#NMTOKEN</tt>
177      */
178     public static final Types XSD_NMTOKEN = new Types("http://www.w3.org/2001/XMLSchema#NMTOKEN");
179 
180     /**
181      * <tt>http://www.w3.org/2001/XMLSchema#nonNegativeInteger</tt>
182      */
183     public static final Types XSD_NONNEGATIVEINTEGER = new Types("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
184 
185     /**
186      * <tt>http://www.w3.org/2001/XMLSchema#nonPositiveInteger</tt>
187      */
188     public static final Types XSD_NONPOSITIVEINTEGER = new Types("http://www.w3.org/2001/XMLSchema#nonPositiveInteger");
189 
190     /**
191      * <tt>http://www.w3.org/2001/XMLSchema#normalizedString</tt>
192      */
193     public static final Types XSD_NORMALIZEDSTRING = new Types("http://www.w3.org/2001/XMLSchema#normalizedString");
194 
195     /**
196      * <tt>http://www.w3.org/2001/XMLSchema#positiveInteger</tt>
197      */
198     public static final Types XSD_POSITIVEINTEGER = new Types("http://www.w3.org/2001/XMLSchema#positiveInteger");
199 
200     /**
201      * <tt>http://www.w3.org/2001/XMLSchema#short</tt>
202      */
203     public static final Types XSD_SHORT = new Types("http://www.w3.org/2001/XMLSchema#short");
204 
205     /**
206      * <tt>http://www.w3.org/2001/XMLSchema#string</tt>
207      */
208     public static final Types XSD_STRING = new Types("http://www.w3.org/2001/XMLSchema#string");
209 
210     /**
211      * <tt>http://www.w3.org/2001/XMLSchema#time</tt>
212      */
213     public static final Types XSD_TIME = new Types("http://www.w3.org/2001/XMLSchema#time");
214 
215     /**
216      * <tt>http://www.w3.org/2001/XMLSchema#token</tt>
217      */
218     public static final Types XSD_TOKEN = new Types("http://www.w3.org/2001/XMLSchema#token");
219 
220     /**
221      * <tt>http://www.w3.org/2001/XMLSchema#unsignedByte</tt>
222      */
223     public static final Types XSD_UNSIGNEDBYTE = new Types("http://www.w3.org/2001/XMLSchema#unsignedByte");
224 
225     /**
226      * <tt>http://www.w3.org/2001/XMLSchema#unsignedInt</tt>
227      */
228     public static final Types XSD_UNSIGNEDINT = new Types("http://www.w3.org/2001/XMLSchema#unsignedInt");
229 
230     /**
231      * <tt>http://www.w3.org/2001/XMLSchema#unsignedLong</tt>
232      */
233     public static final Types XSD_UNSIGNEDLONG = new Types("http://www.w3.org/2001/XMLSchema#unsignedLong");
234 
235     /**
236      * <tt>http://www.w3.org/2001/XMLSchema#unsignedShort</tt>
237      */
238     public static final Types XSD_UNSIGNEDSHORT = new Types("http://www.w3.org/2001/XMLSchema#unsignedShort");
239 
240     private static final Set<IRI> ALL_TYPES;
241 
242     static {
243         final Set<IRI> tempTypes = new LinkedHashSet<>();
244         tempTypes.add(RDF_HTML);
245         tempTypes.add(RDF_LANGSTRING);
246         tempTypes.add(RDF_PLAINLITERAL);
247         tempTypes.add(RDF_XMLLITERAL);
248         tempTypes.add(XSD_ANYURI);
249         tempTypes.add(XSD_BASE64BINARY);
250         tempTypes.add(XSD_BOOLEAN);
251         tempTypes.add(XSD_BYTE);
252         tempTypes.add(XSD_DATE);
253         tempTypes.add(XSD_DATETIME);
254         tempTypes.add(XSD_DAYTIMEDURATION);
255         tempTypes.add(XSD_DECIMAL);
256         tempTypes.add(XSD_DOUBLE);
257         tempTypes.add(XSD_DURATION);
258         tempTypes.add(XSD_FLOAT);
259         tempTypes.add(XSD_GDAY);
260         tempTypes.add(XSD_GMONTH);
261         tempTypes.add(XSD_GMONTHDAY);
262         tempTypes.add(XSD_GYEAR);
263         tempTypes.add(XSD_GYEARMONTH);
264         tempTypes.add(XSD_HEXBINARY);
265         tempTypes.add(XSD_INT);
266         tempTypes.add(XSD_INTEGER);
267         tempTypes.add(XSD_LANGUAGE);
268         tempTypes.add(XSD_LONG);
269         tempTypes.add(XSD_NAME);
270         tempTypes.add(XSD_NCNAME);
271         tempTypes.add(XSD_NEGATIVEINTEGER);
272         tempTypes.add(XSD_NMTOKEN);
273         tempTypes.add(XSD_NONNEGATIVEINTEGER);
274         tempTypes.add(XSD_NONPOSITIVEINTEGER);
275         tempTypes.add(XSD_NORMALIZEDSTRING);
276         tempTypes.add(XSD_POSITIVEINTEGER);
277         tempTypes.add(XSD_SHORT);
278         tempTypes.add(XSD_STRING);
279         tempTypes.add(XSD_TIME);
280         tempTypes.add(XSD_TOKEN);
281         tempTypes.add(XSD_UNSIGNEDBYTE);
282         tempTypes.add(XSD_UNSIGNEDINT);
283         tempTypes.add(XSD_UNSIGNEDLONG);
284         tempTypes.add(XSD_UNSIGNEDSHORT);
285         ALL_TYPES = Collections.unmodifiableSet(tempTypes);
286     }
287 
288     private final IRI field;
289 
290     private Types(final String field) {
291         this.field = new IRIImpl(field);
292     }
293 
294     @Override
295     public String getIRIString() {
296         return this.field.getIRIString();
297     }
298 
299     @Override
300     public String ntriplesString() {
301         return this.field.ntriplesString();
302     }
303 
304     @Override
305     public boolean equals(final Object other) {
306         return this.field.equals(other);
307     }
308 
309     @Override
310     public int hashCode() {
311         return this.field.hashCode();
312     }
313 
314     @Override
315     public String toString() {
316         return this.field.toString();
317     }
318 
319     /**
320      * Get an immutable set of the IRIs used by the RDF-1.1 specification to
321      * define types, from the RDF and XML Schema vocabularies.
322      *
323      * @return A {@link Set} containing all of the IRIs in this collection.
324      */
325     public static Set<IRI> values() {
326         return ALL_TYPES;
327     }
328 
329     /**
330      * Get the IRI from this collection if it is present, or return
331      * {@link Optional#empty()} otherwise.
332      *
333      * @param nextIRI
334      *            The IRI to look for.
335      * @return An {@link Optional} containing the IRI from this collection or
336      *         {@link Optional#empty()} if it is not present here.
337      */
338     public static Optional<IRI> get(final IRI nextIRI) {
339         if (ALL_TYPES.contains(nextIRI)) {
340             // If we know about this IRI, then look through our set to find the
341             // object that matches and return it
342             for (final IRI nextType : ALL_TYPES) {
343                 if (nextType.equals(nextIRI)) {
344                     return Optional.of(nextType);
345                 }
346             }
347         }
348         return Optional.empty();
349     }
350 }