001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query;
007:
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.Collections;
011: import java.util.List;
012: import java.util.Locale;
013:
014: /**
015: * A type-safe enumeration for RDF query languages. QueryLanguage objects are
016: * identified by their name, which is treated in as case-insensitive way.
017: */
018: public class QueryLanguage {
019:
020: /*-----------*
021: * Constants *
022: *-----------*/
023:
024: public static final QueryLanguage SERQL = new QueryLanguage("SeRQL");
025:
026: public static final QueryLanguage SPARQL = new QueryLanguage(
027: "SPARQL");
028:
029: public static final QueryLanguage SERQO = new QueryLanguage("SeRQO");
030:
031: /*------------------*
032: * Static variables *
033: *------------------*/
034:
035: /**
036: * List of known query languages.
037: */
038: private static List<QueryLanguage> QUERY_LANGUAGES = new ArrayList<QueryLanguage>(
039: 4);
040:
041: /*--------------------*
042: * Static initializer *
043: *--------------------*/
044:
045: static {
046: register(SERQL);
047: register(SPARQL);
048: register(SERQO);
049: }
050:
051: /*----------------*
052: * Static methods *
053: *----------------*/
054:
055: /**
056: * Returns all known/registered query languages.
057: */
058: public static Collection<QueryLanguage> values() {
059: return Collections.unmodifiableList(QUERY_LANGUAGES);
060: }
061:
062: /**
063: * Registers the specified query language.
064: *
065: * @param name
066: * The name of the query language, e.g. "SPARQL".
067: */
068: public static QueryLanguage register(String name) {
069: QueryLanguage ql = new QueryLanguage(name);
070: register(ql);
071: return ql;
072: }
073:
074: /**
075: * Registers the specified query language.
076: */
077: public static void register(QueryLanguage ql) {
078: QUERY_LANGUAGES.add(ql);
079: }
080:
081: /**
082: * Returns the query language whose name matches the specified name.
083: *
084: * @param qlName
085: * A query language name.
086: * @return The query language whose name matches the specified name, or
087: * <tt>null</tt> if there is no such query language.
088: */
089: public static QueryLanguage valueOf(String qlName) {
090: for (QueryLanguage ql : QUERY_LANGUAGES) {
091: if (ql.getName().equalsIgnoreCase(qlName)) {
092: return ql;
093: }
094: }
095:
096: return null;
097: }
098:
099: /*-----------*
100: * Variables *
101: *-----------*/
102:
103: /**
104: * The query language's name.
105: */
106: private String name;
107:
108: /*--------------*
109: * Constructors *
110: *--------------*/
111:
112: /**
113: * Creates a new QueryLanguage object.
114: *
115: * @param name
116: * The (case-insensitive) name of the query language, e.g. "SPARQL".
117: */
118: public QueryLanguage(String name) {
119: assert name != null : "name must not be null";
120:
121: this .name = name;
122: }
123:
124: /*---------*
125: * Methods *
126: *---------*/
127:
128: /**
129: * Gets the name of this query language.
130: *
131: * @return A human-readable format name, e.g. "SPARQL".
132: */
133: public String getName() {
134: return name;
135: }
136:
137: public boolean hasName(String name) {
138: return this .name.equalsIgnoreCase(name);
139: }
140:
141: @Override
142: public boolean equals(Object other) {
143: if (other instanceof QueryLanguage) {
144: QueryLanguage o = (QueryLanguage) other;
145: return this .hasName(o.getName());
146: }
147:
148: return false;
149: }
150:
151: @Override
152: public int hashCode() {
153: return getName().toUpperCase(Locale.ENGLISH).hashCode();
154: }
155:
156: @Override
157: public String toString() {
158: return getName();
159: }
160: }
|