001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.model.util;
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.Value;
015:
016: /**
017: * Various utility methods related to {@link Literal}.
018: *
019: * @author Arjohn Kampman
020: */
021: public class LiteralUtil {
022:
023: /**
024: * Gets the label of the supplied literal. The fallback value is returned in
025: * case the supplied literal is <tt>null</tt>.
026: *
027: * @param l
028: * The literal to get the label for.
029: * @param fallback
030: * The value to fall back to in case the supplied literal is
031: * <tt>null</tt>.
032: * @return Either the literal's label, or the fallback value.
033: */
034: public static String getLabel(Literal l, String fallback) {
035: return l != null ? l.getLabel() : fallback;
036: }
037:
038: /**
039: * Returns the result of
040: * {@link #getLabel(Literal, String) getLabel((Literal)v, fallback} in case
041: * the supplied value is a literal, returns the fallback value otherwise.
042: */
043: public static String getLabel(Value v, String fallback) {
044: return v instanceof Literal ? getLabel((Literal) v, fallback)
045: : fallback;
046: }
047:
048: /**
049: * Gets the byte value of the supplied literal. The fallback value is
050: * returned in case {@link Literal#byteValue()} throws a
051: * {@link NumberFormatException}.
052: *
053: * @param l
054: * The literal to get the byte value for.
055: * @param fallback
056: * The value to fall back to in case no byte value could gotten from
057: * the literal.
058: * @return Either the literal's byte value, or the fallback value.
059: */
060: public static byte getByteValue(Literal l, byte fallback) {
061: try {
062: return l.byteValue();
063: } catch (NumberFormatException e) {
064: return fallback;
065: }
066: }
067:
068: /**
069: * Returns the result of
070: * {@link #getByteValue(Literal, byte) getByteValue((Literal)value, fallback)}
071: * in case the supplied value is a literal, returns the fallback value
072: * otherwise.
073: */
074: public static byte getByteValue(Value v, byte fallback) {
075: if (v instanceof Literal) {
076: return getByteValue((Literal) v, fallback);
077: } else {
078: return fallback;
079: }
080: }
081:
082: /**
083: * Gets the short value of the supplied literal. The fallback value is
084: * returned in case {@link Literal#shortValue()} throws a
085: * {@link NumberFormatException}.
086: *
087: * @param l
088: * The literal to get the short value for.
089: * @param fallback
090: * The value to fall back to in case no short value could gotten from
091: * the literal.
092: * @return Either the literal's short value, or the fallback value.
093: */
094: public static short getShortValue(Literal l, short fallback) {
095: try {
096: return l.shortValue();
097: } catch (NumberFormatException e) {
098: return fallback;
099: }
100: }
101:
102: /**
103: * Returns the result of
104: * {@link #getByteValue(Literal, short) getByteValue((Literal)value, fallback)}
105: * in case the supplied value is a literal, returns the fallback value
106: * otherwise.
107: */
108: public static short getShortValue(Value v, short fallback) {
109: if (v instanceof Literal) {
110: return getShortValue((Literal) v, fallback);
111: } else {
112: return fallback;
113: }
114: }
115:
116: /**
117: * Gets the int value of the supplied literal. The fallback value is returned
118: * in case {@link Literal#intValue()} throws a {@link NumberFormatException}.
119: *
120: * @param l
121: * The literal to get the int value for.
122: * @param fallback
123: * The value to fall back to in case no int value could gotten from
124: * the literal.
125: * @return Either the literal's int value, or the fallback value.
126: */
127: public static int getIntValue(Literal l, int fallback) {
128: try {
129: return l.intValue();
130: } catch (NumberFormatException e) {
131: return fallback;
132: }
133: }
134:
135: /**
136: * Returns the result of
137: * {@link #getByteValue(Literal, int) getByteValue((Literal)value, fallback)}
138: * in case the supplied value is a literal, returns the fallback value
139: * otherwise.
140: */
141: public static int getIntValue(Value v, int fallback) {
142: if (v instanceof Literal) {
143: return getIntValue((Literal) v, fallback);
144: } else {
145: return fallback;
146: }
147: }
148:
149: /**
150: * Gets the long value of the supplied literal. The fallback value is
151: * returned in case {@link Literal#longValue()} throws a
152: * {@link NumberFormatException}.
153: *
154: * @param l
155: * The literal to get the long value for.
156: * @param fallback
157: * The value to fall back to in case no long value could gotten from
158: * the literal.
159: * @return Either the literal's long value, or the fallback value.
160: */
161: public static long getLongValue(Literal l, long fallback) {
162: try {
163: return l.longValue();
164: } catch (NumberFormatException e) {
165: return fallback;
166: }
167: }
168:
169: /**
170: * Returns the result of
171: * {@link #getByteValue(Literal, long) getByteValue((Literal)value, fallback)}
172: * in case the supplied value is a literal, returns the fallback value
173: * otherwise.
174: */
175: public static long getLongValue(Value v, long fallback) {
176: if (v instanceof Literal) {
177: return getLongValue((Literal) v, fallback);
178: } else {
179: return fallback;
180: }
181: }
182:
183: /**
184: * Gets the integer value of the supplied literal. The fallback value is
185: * returned in case {@link Literal#integerValue()} throws a
186: * {@link NumberFormatException}.
187: *
188: * @param l
189: * The literal to get the integer value for.
190: * @param fallback
191: * The value to fall back to in case no integer value could gotten
192: * from the literal.
193: * @return Either the literal's integer value, or the fallback value.
194: */
195: public static BigInteger getIntegerValue(Literal l,
196: BigInteger fallback) {
197: try {
198: return l.integerValue();
199: } catch (NumberFormatException e) {
200: return fallback;
201: }
202: }
203:
204: /**
205: * Returns the result of
206: * {@link #getByteValue(Literal, BigInteger) getByteValue((Literal)value, fallback)}
207: * in case the supplied value is a literal, returns the fallback value
208: * otherwise.
209: */
210: public static BigInteger getIntegerValue(Value v,
211: BigInteger fallback) {
212: if (v instanceof Literal) {
213: return getIntegerValue((Literal) v, fallback);
214: } else {
215: return fallback;
216: }
217: }
218:
219: /**
220: * Gets the decimal value of the supplied literal. The fallback value is
221: * returned in case {@link Literal#decimalValue()} throws a
222: * {@link NumberFormatException}.
223: *
224: * @param l
225: * The literal to get the decimal value for.
226: * @param fallback
227: * The value to fall back to in case no decimal value could gotten
228: * from the literal.
229: * @return Either the literal's decimal value, or the fallback value.
230: */
231: public static BigDecimal getDecimalValue(Literal l,
232: BigDecimal fallback) {
233: try {
234: return l.decimalValue();
235: } catch (NumberFormatException e) {
236: return fallback;
237: }
238: }
239:
240: /**
241: * Returns the result of
242: * {@link #getByteValue(Literal, BigDecimal) getByteValue((Literal)value, fallback)}
243: * in case the supplied value is a literal, returns the fallback value
244: * otherwise.
245: */
246: public static BigDecimal getDecimalValue(Value v,
247: BigDecimal fallback) {
248: if (v instanceof Literal) {
249: return getDecimalValue((Literal) v, fallback);
250: } else {
251: return fallback;
252: }
253: }
254:
255: /**
256: * Gets the float value of the supplied literal. The fallback value is
257: * returned in case {@link Literal#floatValue()} throws a
258: * {@link NumberFormatException}.
259: *
260: * @param l
261: * The literal to get the float value for.
262: * @param fallback
263: * The value to fall back to in case no float value could gotten from
264: * the literal.
265: * @return Either the literal's float value, or the fallback value.
266: */
267: public static float getFloatValue(Literal l, float fallback) {
268: try {
269: return l.floatValue();
270: } catch (NumberFormatException e) {
271: return fallback;
272: }
273: }
274:
275: /**
276: * Returns the result of
277: * {@link #getByteValue(Literal, float) getByteValue((Literal)value, fallback)}
278: * in case the supplied value is a literal, returns the fallback value
279: * otherwise.
280: */
281: public static float getFloatValue(Value v, float fallback) {
282: if (v instanceof Literal) {
283: return getFloatValue((Literal) v, fallback);
284: } else {
285: return fallback;
286: }
287: }
288:
289: /**
290: * Gets the double value of the supplied literal. The fallback value is
291: * returned in case {@link Literal#doubleValue()} throws a
292: * {@link NumberFormatException}.
293: *
294: * @param l
295: * The literal to get the double value for.
296: * @param fallback
297: * The value to fall back to in case no double value could gotten from
298: * the literal.
299: * @return Either the literal's double value, or the fallback value.
300: */
301: public static double getDoubleValue(Literal l, double fallback) {
302: try {
303: return l.doubleValue();
304: } catch (NumberFormatException e) {
305: return fallback;
306: }
307: }
308:
309: /**
310: * Returns the result of
311: * {@link #getByteValue(Literal, double) getByteValue((Literal)value, fallback)}
312: * in case the supplied value is a literal, returns the fallback value
313: * otherwise.
314: */
315: public static double getDoubleValue(Value v, double fallback) {
316: if (v instanceof Literal) {
317: return getDoubleValue((Literal) v, fallback);
318: } else {
319: return fallback;
320: }
321: }
322:
323: /**
324: * Gets the boolean value of the supplied literal. The fallback value is
325: * returned in case {@link Literal#booleanValue()} throws a
326: * {@link NumberFormatException}.
327: *
328: * @param l
329: * The literal to get the boolean value for.
330: * @param fallback
331: * The value to fall back to in case no boolean value could gotten
332: * from the literal.
333: * @return Either the literal's boolean value, or the fallback value.
334: */
335: public static boolean getBooleanValue(Literal l, boolean fallback) {
336: try {
337: return l.booleanValue();
338: } catch (IllegalArgumentException e) {
339: return fallback;
340: }
341: }
342:
343: /**
344: * Returns the result of
345: * {@link #getByteValue(Literal, boolean) getByteValue((Literal)value, fallback)}
346: * in case the supplied value is a literal, returns the fallback value
347: * otherwise.
348: */
349: public static boolean getBooleanValue(Value v, boolean fallback) {
350: if (v instanceof Literal) {
351: return getBooleanValue((Literal) v, fallback);
352: } else {
353: return fallback;
354: }
355: }
356:
357: /**
358: * Gets the calendar value of the supplied literal. The fallback value is
359: * returned in case {@link Literal#calendarValue()} throws a
360: * {@link NumberFormatException}.
361: *
362: * @param l
363: * The literal to get the calendar value for.
364: * @param fallback
365: * The value to fall back to in case no calendar value could gotten
366: * from the literal.
367: * @return Either the literal's calendar value, or the fallback value.
368: */
369: public static XMLGregorianCalendar getCalendarValue(Literal l,
370: XMLGregorianCalendar fallback) {
371: try {
372: return l.calendarValue();
373: } catch (IllegalArgumentException e) {
374: return fallback;
375: }
376: }
377:
378: /**
379: * Returns the result of
380: * {@link #getByteValue(Literal, XMLGregorianCalendar) getByteValue((Literal)value, fallback)}
381: * in case the supplied value is a literal, returns the fallback value
382: * otherwise.
383: */
384: public static XMLGregorianCalendar getCalendarValue(Value v,
385: XMLGregorianCalendar fallback) {
386: if (v instanceof Literal) {
387: return getCalendarValue((Literal) v, fallback);
388: } else {
389: return fallback;
390: }
391: }
392: }
|