Package org.eclipse.rdf4j.model.util
Class Literals
java.lang.Object
org.eclipse.rdf4j.model.util.Literals
Various utility methods related to
Literal
.- Author:
- Arjohn Kampman, Peter Ansell
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canCreateLiteral
(Object object) Deprecated.since 3.5.0static Literal
createLiteral
(ValueFactory valueFactory, Object object) Deprecated.UseValues.literal(Object)
instead.static Literal
createLiteralOrFail
(ValueFactory valueFactory, Object object) Deprecated.UseValues.literal(Object, boolean)
instead.static boolean
getBooleanValue
(Literal l, boolean fallback) Gets the boolean value of the supplied literal.static boolean
getBooleanValue
(Value v, boolean fallback) Returns the result ofgetBooleanValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static byte
getByteValue
(Literal l, byte fallback) Gets the byte value of the supplied literal.static byte
getByteValue
(Value v, byte fallback) Returns the result ofgetByteValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static XMLGregorianCalendar
getCalendarValue
(Literal l, XMLGregorianCalendar fallback) Gets the calendar value of the supplied literal.static XMLGregorianCalendar
getCalendarValue
(Value v, XMLGregorianCalendar fallback) Returns the result ofgetCalendarValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static BigDecimal
getDecimalValue
(Literal l, BigDecimal fallback) Gets the decimal value of the supplied literal.static BigDecimal
getDecimalValue
(Value v, BigDecimal fallback) Returns the result ofgetDecimalValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static double
getDoubleValue
(Literal l, double fallback) Gets the double value of the supplied literal.static double
getDoubleValue
(Value v, double fallback) Returns the result ofgetDoubleValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static Duration
getDurationValue
(Literal l, Duration fallback) Gets theDuration
value of the supplied literal.static float
getFloatValue
(Literal l, float fallback) Gets the float value of the supplied literal.static float
getFloatValue
(Value v, float fallback) Returns the result ofgetFloatValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static BigInteger
getIntegerValue
(Literal l, BigInteger fallback) Gets the integer value of the supplied literal.static BigInteger
getIntegerValue
(Value v, BigInteger fallback) Returns the result ofgetIntegerValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static int
getIntValue
(Literal l, int fallback) Gets the int value of the supplied literal.static int
getIntValue
(Value v, int fallback) Returns the result ofgetIntValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static String
static String
Gets the label of the supplied literal.static String
Returns the result ofgetLabel((Literal)v, fallback
in case the supplied value is a literal, returns the fallback value otherwise.static long
getLongValue
(Literal l, long fallback) Gets the long value of the supplied literal.static long
getLongValue
(Value v, long fallback) Returns the result ofgetLongValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static short
getShortValue
(Literal l, short fallback) Gets the short value of the supplied literal.static short
getShortValue
(Value v, short fallback) Returns the result ofgetShortValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise.static Optional<XSD.Datatype>
Deprecated, for removal: This API element is subject to removal in a future version.UseLiteral.getCoreDatatype()
instead.static boolean
isLanguageLiteral
(Literal literal) Helper method to determine whether a literal is a language literal, and not a typed literal.static boolean
isValidLanguageTag
(String languageTag) Checks if the given string is a well-formed BCP47 language tag according to the rules defined in RFC 5646, section 2.1.1.static boolean
langMatches
(String langTag, String langRange) Implements language range filtering for SPARQL langMatches (https://www.w3.org/TR/sparql11-query/#func-langMatches).static String
normalizeLanguageTag
(String languageTag) Normalizes the given BCP47 language tag according to the rules defined in RFC 5646, section 2.1.1:
-
Constructor Details
-
Literals
protected Literals()
-
-
Method Details
-
getLabel
Gets the label of the supplied literal. The fallback value is returned in case the supplied literal is null.- Parameters:
l
- The literal to get the label for.fallback
- The value to fall back to in case the supplied literal is null.- Returns:
- Either the literal's label, or the fallback value.
-
getLabel
Returns the result ofgetLabel((Literal)v, fallback
in case the supplied value is a literal, returns the fallback value otherwise. -
getLabel
-
getXsdDatatype
@Deprecated(since="4.0.0", forRemoval=true) public static Optional<XSD.Datatype> getXsdDatatype(Literal l) Deprecated, for removal: This API element is subject to removal in a future version.UseLiteral.getCoreDatatype()
instead.Retrieves theXSD.Datatype
value for the supplied Literal, if it has one.- Parameters:
l
- a Literal- Returns:
- an Optional
XSD.Datatype
enum, if one is available. Note that the absence of this enum does not indicate that the literal has no datatype, merely that it has no cached enum representation of that datatype. - Since:
- 3.5.0
-
getByteValue
Gets the byte value of the supplied literal. The fallback value is returned in caseLiteral.byteValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the byte value for.fallback
- The value to fall back to in case no byte value could gotten from the literal.- Returns:
- Either the literal's byte value, or the fallback value.
-
getByteValue
Returns the result ofgetByteValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getShortValue
Gets the short value of the supplied literal. The fallback value is returned in caseLiteral.shortValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the short value for.fallback
- The value to fall back to in case no short value could gotten from the literal.- Returns:
- Either the literal's short value, or the fallback value.
-
getShortValue
Returns the result ofgetShortValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getIntValue
Gets the int value of the supplied literal. The fallback value is returned in caseLiteral.intValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the int value for.fallback
- The value to fall back to in case no int value could gotten from the literal.- Returns:
- Either the literal's int value, or the fallback value.
-
getIntValue
Returns the result ofgetIntValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getLongValue
Gets the long value of the supplied literal. The fallback value is returned in caseLiteral.longValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the long value for.fallback
- The value to fall back to in case no long value could gotten from the literal.- Returns:
- Either the literal's long value, or the fallback value.
-
getLongValue
Returns the result ofgetLongValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getIntegerValue
Gets the integer value of the supplied literal. The fallback value is returned in caseLiteral.integerValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the integer value for.fallback
- The value to fall back to in case no integer value could gotten from the literal.- Returns:
- Either the literal's integer value, or the fallback value.
-
getIntegerValue
Returns the result ofgetIntegerValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getDecimalValue
Gets the decimal value of the supplied literal. The fallback value is returned in caseLiteral.decimalValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the decimal value for.fallback
- The value to fall back to in case no decimal value could gotten from the literal.- Returns:
- Either the literal's decimal value, or the fallback value.
-
getDecimalValue
Returns the result ofgetDecimalValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getFloatValue
Gets the float value of the supplied literal. The fallback value is returned in caseLiteral.floatValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the float value for.fallback
- The value to fall back to in case no float value could gotten from the literal.- Returns:
- Either the literal's float value, or the fallback value.
-
getFloatValue
Returns the result ofgetFloatValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getDoubleValue
Gets the double value of the supplied literal. The fallback value is returned in caseLiteral.doubleValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the double value for.fallback
- The value to fall back to in case no double value could gotten from the literal.- Returns:
- Either the literal's double value, or the fallback value.
-
getDoubleValue
Returns the result ofgetDoubleValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getBooleanValue
Gets the boolean value of the supplied literal. The fallback value is returned in caseLiteral.booleanValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the boolean value for.fallback
- The value to fall back to in case no boolean value could gotten from the literal.- Returns:
- Either the literal's boolean value, or the fallback value.
-
getBooleanValue
Returns the result ofgetBooleanValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
getCalendarValue
Gets the calendar value of the supplied literal. The fallback value is returned in caseLiteral.calendarValue()
throws aNumberFormatException
.- Parameters:
l
- The literal to get the calendar value for.fallback
- The value to fall back to in case no calendar value could gotten from the literal.- Returns:
- Either the literal's calendar value, or the fallback value.
-
getDurationValue
Gets theDuration
value of the supplied literal. The fallback value is returned in caseXMLDatatypeUtil.parseDuration(String)
throws an exception.- Parameters:
l
- The literal to get theDuration
value for.fallback
- The value to fall back to in case no Duration value could gotten from the literal.- Returns:
- Either the literal's Duration value, or the fallback value.
-
getCalendarValue
Returns the result ofgetCalendarValue((Literal)value, fallback)
in case the supplied value is a literal, returns the fallback value otherwise. -
createLiteral
@Deprecated(since="3.5.0") public static Literal createLiteral(ValueFactory valueFactory, Object object) Deprecated.UseValues.literal(Object)
instead.Creates a typedLiteral
out of the supplied object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping is available, the method returns a literal with the string representation of the supplied object as the value, andXSD.STRING
as the datatype. Recognized types areBoolean
,Byte
,Double
,Float
,Integer
,Long
,Short
,XMLGregorianCalendar
, andDate
.- Parameters:
valueFactory
-object
- an object to be converted to a typed literal.- Returns:
- a typed literal representation of the supplied object.
- Throws:
NullPointerException
- If the object was null.
-
createLiteralOrFail
@Deprecated(since="3.5.0") public static Literal createLiteralOrFail(ValueFactory valueFactory, Object object) throws LiteralUtilException Deprecated.UseValues.literal(Object, boolean)
instead.Creates a typedLiteral
out of the supplied object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping is available, the method throws aLiteralUtilException
. Recognized types areBoolean
,Byte
,Double
,Float
,Integer
,Long
,Short
,XMLGregorianCalendar
, andDate
.- Parameters:
valueFactory
-object
- an object to be converted to a typed literal.- Returns:
- a typed literal representation of the supplied object.
- Throws:
LiteralUtilException
- If the literal could not be created.NullPointerException
- If the object was null.
-
canCreateLiteral
Deprecated.since 3.5.0Helper method for determining whether a literal could be created from an object using aValueFactory
.- Parameters:
object
- an object to check for the possibility of being converted to a typed literal.- Returns:
- True if a literal could be created from the given object, based solely on its type and the methods
available on the
ValueFactory
interface and false otherwise. Returns false if the object is null.
-
isLanguageLiteral
Helper method to determine whether a literal is a language literal, and not a typed literal.- Parameters:
literal
- The literal to check- Returns:
- True if the literal has a language tag attached to it and false otherwise.
-
normalizeLanguageTag
Normalizes the given BCP47 language tag according to the rules defined in RFC 5646, section 2.1.1:All subtags, including extension and private use subtags, use lowercase letters with two exceptions: two-letter and four-letter subtags that neither appear at the start of the tag nor occur after singletons. Such two-letter subtags are all uppercase (as in the tags "en-CA-x-ca" or "sgn-BE-FR") and four- letter subtags are titlecase (as in the tag "az-Latn-x-latn").
- Parameters:
languageTag
- An unnormalized, valid, language tag- Returns:
- A normalized version of the given language tag
- Throws:
IllformedLocaleException
- If the given language tag is ill-formed according to the rules specified in BCP47 (RFC 5646).
-
isValidLanguageTag
Checks if the given string is a well-formed BCP47 language tag according to the rules defined in RFC 5646, section 2.1.1.- Parameters:
languageTag
- A language tag- Returns:
true
if the given language tag is well-formed according to the rules specified in BCP47.
-
langMatches
Implements language range filtering for SPARQL langMatches (https://www.w3.org/TR/sparql11-query/#func-langMatches).- Parameters:
langTag
- the tag to filterlangRange
- the range to filter against- Returns:
- true if langTag matches langRange
-