001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.commons.rdf.simple;
019
020import org.apache.commons.rdf.api.IRI;
021
022import java.util.Collections;
023import java.util.LinkedHashSet;
024import java.util.Optional;
025import java.util.Set;
026
027/**
028 * Types from the RDF and XML Schema vocabularies.
029 */
030public final class Types implements IRI, SimpleRDF.SimpleRDFTerm {
031
032    /**
033     * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML</tt>
034     */
035    public static final Types RDF_HTML = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML");
036
037    /**
038     * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#langString</tt>
039     */
040    public static final Types RDF_LANGSTRING = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
041
042    /**
043     * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral</tt>
044     *
045     * @deprecated Not used in RDF-1.1
046     */
047    @Deprecated
048    public static final Types RDF_PLAINLITERAL = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral");
049
050    /**
051     * <tt>http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral</tt>
052     */
053    public static final Types RDF_XMLLITERAL = new Types("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral");
054
055    /**
056     * <tt>http://www.w3.org/2001/XMLSchema#anyURI</tt>
057     */
058    public static final Types XSD_ANYURI = new Types("http://www.w3.org/2001/XMLSchema#anyURI");
059
060    /**
061     * <tt>http://www.w3.org/2001/XMLSchema#base64Binary</tt>
062     */
063    public static final Types XSD_BASE64BINARY = new Types("http://www.w3.org/2001/XMLSchema#base64Binary");
064
065    /**
066     * <tt>http://www.w3.org/2001/XMLSchema#boolean</tt>
067     */
068    public static final Types XSD_BOOLEAN = new Types("http://www.w3.org/2001/XMLSchema#boolean");
069
070    /**
071     * <tt>http://www.w3.org/2001/XMLSchema#byte</tt>
072     */
073    public static final Types XSD_BYTE = new Types("http://www.w3.org/2001/XMLSchema#byte");
074
075    /**
076     * <tt>http://www.w3.org/2001/XMLSchema#date</tt>
077     */
078    public static final Types XSD_DATE = new Types("http://www.w3.org/2001/XMLSchema#date");
079
080    /**
081     * <tt>http://www.w3.org/2001/XMLSchema#dateTime</tt>
082     */
083    public static final Types XSD_DATETIME = new Types("http://www.w3.org/2001/XMLSchema#dateTime");
084
085    /**
086     * <tt>http://www.w3.org/2001/XMLSchema#dayTimeDuration</tt>
087     */
088    public static final Types XSD_DAYTIMEDURATION = new Types("http://www.w3.org/2001/XMLSchema#dayTimeDuration");
089
090    /**
091     * <tt>http://www.w3.org/2001/XMLSchema#decimal</tt>
092     */
093    public static final Types XSD_DECIMAL = new Types("http://www.w3.org/2001/XMLSchema#decimal");
094
095    /**
096     * <tt>http://www.w3.org/2001/XMLSchema#double</tt>
097     */
098    public static final Types XSD_DOUBLE = new Types("http://www.w3.org/2001/XMLSchema#double");
099
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}