Skip to content

Commit

Permalink
Add support for Integer as a typed literal (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardsph authored Jan 22, 2024
1 parent 55eb75d commit bff10b2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ public static Literal asTypedLiteral(final Boolean value, final Graph graph) {
return FACTORY.createLiteral(value.toString(), FACTORY.createIRI("http://www.w3.org/2001/XMLSchema#boolean"));
}

/**
* Maps a integer value to a literal term.
*
* @param value the value to map
* @param graph ignored
*
* @return an xsd:int typed literal node with the integer value as its lexical value
*
* @throws NullPointerException if the value is null
* @throws NullPointerException if the model is null
*/
public static Literal asTypedLiteral(final Integer value, final Graph graph) {
Objects.requireNonNull(value, VALUE_REQUIRED);
Objects.requireNonNull(graph, GRAPH_REQUIRED);

return FACTORY.createLiteral(value.toString(), FACTORY.createIRI("http://www.w3.org/2001/XMLSchema#int"));
}

/**
* Maps a term to itself.
*
Expand Down
18 changes: 18 additions & 0 deletions jena/src/main/java/com/inrupt/rdf/wrapping/jena/NodeMappings.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ public static Literal asTypedLiteral(final Boolean value, final Model model) {
return model.createTypedLiteral(value);
}

/**
* Maps a integer value to a literal node.
*
* @param value the value to map
* @param model the graph used to create the node
*
* @return an xsd:int typed literal node with the integer value as its lexical value
*
* @throws NullPointerException if the value is null
* @throws NullPointerException if the model is null
*/
public static Literal asTypedLiteral(final Integer value, final Model model) {
Objects.requireNonNull(value, VALUE_REQUIRED);
Objects.requireNonNull(model, GRAPH_REQUIRED);

return model.createTypedLiteral(value);
}

/**
* Maps a term to itself.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ void asTypedLiteralBooleanTest() {
hasProperty("datatypeURI", is(XSD.xboolean.getURI()))));
}

@Test
void asTypedLiteralIntegerTest() {
assertThrows(NullPointerException.class, () -> asTypedLiteral((Integer) null, null));
assertThrows(NullPointerException.class, () -> asTypedLiteral(1, null));

assertThat(asTypedLiteral(1, MODEL), both(
instanceOf(Literal.class)).and(
hasProperty("lexicalForm", is("1"))).and(
hasProperty("datatypeURI", is(XSD.xint.getURI()))));
}

@Test
void identityTest() {
final Resource blank = MODEL.createResource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ public static Literal asTypedLiteral(final Boolean value, final Model model) {
return FACTORY.createLiteral(value.toString(), XSD.BOOLEAN);
}

/**
* Maps a integer value to a literal term.
*
* @param value the value to map
* @param model ignored
*
* @return an xsd:int typed literal node with the integer value as its lexical value
*
* @throws NullPointerException if the value is null
* @throws NullPointerException if the model is null
*/
public static Literal asTypedLiteral(final Integer value, final Model model) {
Objects.requireNonNull(value, VALUE_REQUIRED);
Objects.requireNonNull(model, GRAPH_REQUIRED);

return FACTORY.createLiteral(value.toString(), XSD.INT);
}

/**
* Maps a term to itself.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,21 @@ void asTypedLiteralBooleanTest() {

assertThat(asTypedLiteral(true, MODEL), both(
instanceOf(Literal.class)).and(
hasProperty("label", is("true".toString()))).and(
hasProperty("label", is("true"))).and(
hasProperty("datatype", is(XSD.BOOLEAN))));
}

@Test
void asTypedLiteralIntegerTest() {
assertThrows(NullPointerException.class, () -> asTypedLiteral((Integer) null, null));
assertThrows(NullPointerException.class, () -> asTypedLiteral(1, null));

assertThat(asTypedLiteral(1, MODEL), both(
instanceOf(Literal.class)).and(
hasProperty("label", is("1"))).and(
hasProperty("datatype", is(XSD.INT))));
}

@Test
void identityTest() {
final BNode blank = SimpleValueFactory.getInstance().createBNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ void asTypedLiteralBooleanTest() {
hasProperty("datatype", is(xsdBoolean))));
}

@Test
void asTypedLiteralIntegerTest() {
final IRI xsdInt = FACTORY.createIRI("http://www.w3.org/2001/XMLSchema#int");

assertThrows(NullPointerException.class, () -> asTypedLiteral((Integer) null, null));
assertThrows(NullPointerException.class, () -> asTypedLiteral(1, null));

assertThat(asTypedLiteral(1, GRAPH), both(
instanceOf(Literal.class)).and(
hasProperty("lexicalForm", is("1"))).and(
hasProperty("datatype", is(xsdInt))));
}

@Test
void identityTest() {
final BlankNode blank = FACTORY.createBlankNode();
Expand Down

0 comments on commit bff10b2

Please sign in to comment.