Skip to content

Commit

Permalink
Merge pull request eclipse-rdf4j#2792 from eclipse/eclipse-rdf4jGH-2789
Browse files Browse the repository at this point in the history
…-values-language-tagged-literal

eclipse-rdf4jGH-2789 added factory methods for language-tagged literals
  • Loading branch information
abrokenjester committed Feb 2, 2021
2 parents 88de812 + 2160146 commit 0c85853
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
31 changes: 31 additions & 0 deletions core/model/src/main/java/org/eclipse/rdf4j/model/util/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.impl.ValidatingValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.XSD;

/**
Expand Down Expand Up @@ -206,6 +207,36 @@ public static Literal literal(ValueFactory vf, String lexicalValue) {
return vf.createLiteral(Objects.requireNonNull(lexicalValue, "lexicalValue may not be null"));
}

/**
* Creates a new {@link Literal} with the supplied lexical value.
*
* @param lexicalValue the lexical value for the literal
* @param languageTag the language tag for the literal.
*
* @return a new {@link Literal} of type {@link RDF#LANGSTRING}
*
* @throws NullPointerException if the supplied lexical value or language tag is <code>null</code>.
*/
public static Literal literal(String lexicalValue, String languageTag) {
return literal(VALUE_FACTORY, lexicalValue, languageTag);
}

/**
* Creates a new {@link Literal} with the supplied lexical value.
*
* @param vf the {@link ValueFactory} to use for creation of the {@link Literal}
* @param lexicalValue the lexical value for the literal
* @param languageTag the language tag for the literal.
*
* @return a new {@link Literal} of type {@link RDF#LANGSTRING}
*
* @throws NullPointerException if any of the input parameters is <code>null</code>
*/
public static Literal literal(ValueFactory vf, String lexicalValue, String languageTag) {
return vf.createLiteral(Objects.requireNonNull(lexicalValue, "lexicalValue may not be null"),
Objects.requireNonNull(languageTag, "languageTag may not be null"));
}

/**
* Creates a new {@link Literal} with the supplied lexical value and datatype.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,43 @@ public void testStringLiteralNull() {
.hasMessageContaining("lexicalValue may not be null");
}

@Test
public void testLanguageTaggedLiteral() {
String lexValue = "a literal";
String languageTag = "en";
Literal literal = literal(lexValue, languageTag);

assertThat(literal.getLabel()).isEqualTo(lexValue);
assertThat(literal.getLanguage()).isNotEmpty().contains(languageTag);
assertThat(literal.getDatatype()).isEqualTo(RDF.LANGSTRING);
}

@Test
public void testLanguageTaggedLiteral_InjectedValueFactory() {
String lexValue = "a literal";
String languageTag = "en";
literal(vf, lexValue, languageTag);
verify(vf).createLiteral(lexValue, languageTag);
}

@Test
public void testLanguageTaggedLiteralNull1() {
String lexicalValue = null;
String languageTag = "en";
assertThatThrownBy(() -> literal(lexicalValue, languageTag))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("lexicalValue may not be null");
}

@Test
public void testLanguageTaggedLiteralNull2() {
String lexicalValue = "a literal";
String languageTag = null;
assertThatThrownBy(() -> literal(lexicalValue, languageTag))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("languageTag may not be null");
}

@Test
public void testValidTypedLiteral() {
String lexValue = "42";
Expand Down Expand Up @@ -209,7 +246,8 @@ public void testTypedLiteralNull1() {
@Test
public void testTypedLiteralNull2() {
String lexValue = "42";
assertThatThrownBy(() -> literal(lexValue, null))
IRI datatype = null;
assertThatThrownBy(() -> literal(lexValue, datatype))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("datatype may not be null");
}
Expand Down Expand Up @@ -573,4 +611,5 @@ public void testLiteralObjectObject() throws Exception {
assertThat(l).isNotNull();
assertThat(l.getDatatype()).isEqualTo(XSD.STRING);
}

}

0 comments on commit 0c85853

Please sign in to comment.