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.model.impl;
007:
008: import java.math.BigDecimal;
009: import java.math.BigInteger;
010:
011: import javax.xml.datatype.XMLGregorianCalendar;
012:
013: import org.openrdf.model.Literal;
014: import org.openrdf.model.URI;
015: import org.openrdf.model.datatypes.XMLDatatypeUtil;
016:
017: /**
018: * An implementation of the {@link Literal} interface.
019: *
020: * @author Arjohn Kampman
021: * @author David Huynh
022: */
023: public class LiteralImpl implements Literal {
024:
025: private static final long serialVersionUID = -1649571784782592271L;
026:
027: /*-----------*
028: * Variables *
029: *-----------*/
030:
031: /**
032: * The literal's label.
033: */
034: private final String label;
035:
036: /**
037: * The literal's language tag (null if not applicable).
038: */
039: private final String language;
040:
041: /**
042: * The literal's datatype (null if not applicable).
043: */
044: private final URI datatype;
045:
046: /*--------------*
047: * Constructors *
048: *--------------*/
049:
050: /**
051: * Creates a new plain literal with the supplied label.
052: *
053: * @param label
054: * The label for the literal, must not be <tt>null</tt>.
055: */
056: public LiteralImpl(String label) {
057: this (label, null, null);
058: }
059:
060: /**
061: * Creates a new plain literal with the supplied label and language tag.
062: *
063: * @param label
064: * The label for the literal, must not be <tt>null</tt>.
065: * @param language
066: * The language tag for the literal.
067: */
068: public LiteralImpl(String label, String language) {
069: this (label, language, null);
070: }
071:
072: /**
073: * Creates a new datyped literal with the supplied label and datatype.
074: *
075: * @param label
076: * The label for the literal, must not be <tt>null</tt>.
077: * @param datatype
078: * The datatype for the literal.
079: */
080: public LiteralImpl(String label, URI datatype) {
081: this (label, null, datatype);
082: }
083:
084: /**
085: * Creates a new Literal object, initializing the variables with the supplied
086: * parameters.
087: */
088: private LiteralImpl(String label, String language, URI datatype) {
089: assert label != null;
090:
091: this .label = label;
092: this .language = (language == null) ? null : language
093: .toLowerCase();
094: this .datatype = datatype;
095: }
096:
097: /*---------*
098: * Methods *
099: *---------*/
100:
101: public String getLabel() {
102: return label;
103: }
104:
105: public String getLanguage() {
106: return language;
107: }
108:
109: public URI getDatatype() {
110: return datatype;
111: }
112:
113: // Overrides Object.equals(Object), implements Literal.equals(Object)
114: @Override
115: public boolean equals(Object o) {
116: if (this == o) {
117: return true;
118: }
119:
120: if (o instanceof Literal) {
121: Literal other = (Literal) o;
122:
123: // Compare labels
124: if (!label.equals(other.getLabel())) {
125: return false;
126: }
127:
128: // Compare datatypes
129: if (datatype == null) {
130: if (other.getDatatype() != null) {
131: return false;
132: }
133: } else {
134: if (!datatype.equals(other.getDatatype())) {
135: return false;
136: }
137: }
138:
139: // Compare language tags
140: if (language == null) {
141: if (other.getLanguage() != null) {
142: return false;
143: }
144: } else {
145: if (!language.equals(other.getLanguage())) {
146: return false;
147: }
148: }
149:
150: return true;
151: }
152:
153: return false;
154: }
155:
156: // overrides Object.hashCode(), implements hashCode()
157: @Override
158: public int hashCode() {
159: return label.hashCode();
160: }
161:
162: /**
163: * Returns the label of the literal.
164: */
165: @Override
166: public String toString() {
167: StringBuilder sb = new StringBuilder(label.length() * 2);
168:
169: sb.append('"');
170: sb.append(label);
171: sb.append('"');
172:
173: if (language != null) {
174: sb.append('@');
175: sb.append(language);
176: }
177:
178: if (datatype != null) {
179: sb.append("^^");
180: sb.append(datatype.toString());
181: }
182:
183: return sb.toString();
184: }
185:
186: public String stringValue() {
187: return label;
188: }
189:
190: public boolean booleanValue() {
191: return XMLDatatypeUtil.parseBoolean(getLabel());
192: }
193:
194: public byte byteValue() {
195: return XMLDatatypeUtil.parseByte(getLabel());
196: }
197:
198: public short shortValue() {
199: return XMLDatatypeUtil.parseShort(getLabel());
200: }
201:
202: public int intValue() {
203: return XMLDatatypeUtil.parseInt(getLabel());
204: }
205:
206: public long longValue() {
207: return XMLDatatypeUtil.parseLong(getLabel());
208: }
209:
210: public float floatValue() {
211: return XMLDatatypeUtil.parseFloat(getLabel());
212: }
213:
214: public double doubleValue() {
215: return XMLDatatypeUtil.parseDouble(getLabel());
216: }
217:
218: public BigInteger integerValue() {
219: return XMLDatatypeUtil.parseInteger(getLabel());
220: }
221:
222: public BigDecimal decimalValue() {
223: return XMLDatatypeUtil.parseDecimal(getLabel());
224: }
225:
226: public XMLGregorianCalendar calendarValue() {
227: return XMLDatatypeUtil.parseCalendar(getLabel());
228: }
229: }
|