Skip to content

Commit

Permalink
GH-688: Added getLabel OptionalMethod for Optional (#2279)
Browse files Browse the repository at this point in the history
* GH-688: Added getLabel OptionalMethod for Optional

Signed-off-by: SakshiSaini17092 <sakshi17092@iiitd.ac.in>

* GH-688: Fixed the getLabel for Optional

Signed-off-by: SakshiSaini17092 <sakshi17092@iiitd.ac.in>
  • Loading branch information
SakshiSaini17092 authored May 28, 2020
1 parent 88015c7 commit 55b5b54
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.IllformedLocaleException;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
Expand Down Expand Up @@ -51,6 +52,11 @@ public static String getLabel(Value v, String fallback) {
return v instanceof Literal ? getLabel((Literal) v, fallback) : fallback;
}

public static String getLabel(Optional v, String fallback) {

return v != null ? getLabel((Value) v.orElseGet(null), fallback) : fallback;
}

/**
* Gets the byte value of the supplied literal. The fallback value is returned in case {@link Literal#byteValue()}
* throws a {@link NumberFormatException}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Optional;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.XSD;
import org.junit.Ignore;
Expand All @@ -35,6 +39,9 @@
public class LiteralsTest {

private static final ValueFactory vf = SimpleValueFactory.getInstance();
private static final Model model = new LinkedHashModel();
private static final IRI foo = vf.createIRI("http://example.org/foo");
private static final IRI bar = vf.createIRI("http://example.org/bar");

/**
* Test method for
Expand Down Expand Up @@ -819,4 +826,34 @@ public void testNormaliseBCP47Tag() throws Exception {
assertEquals("fr-FR", Literals.normalizeLanguageTag("FR-FR"));
assertEquals("fr-FR", Literals.normalizeLanguageTag("FR-fr"));
}

/**
* Test method for {@link org.eclipse.rdf4j.model.util.Literals#getLabel(Optional, String)}} .
*/
@Test
public void testGetLabelForOptional() throws Exception {

Literal lit = vf.createLiteral(1.0);
model.add(foo, bar, lit);

Optional result = Models.object(model);
String label = Literals.getLabel(result, "fallback");
assertNotNull(label);
assertTrue(label.equals("1.0"));
}

/**
* Test method for {@link org.eclipse.rdf4j.model.util.Literals#getLabel(Optional, String)}} .
*/
@Test
public void testGetLabelForOptionalInFallback() throws Exception {

Literal lit = vf.createLiteral(1.0);
model.add(foo, bar, lit);

Optional result = Models.object(model);
String label = Literals.getLabel((Optional) null, "fallback");
assertNotNull(label);
assertTrue(label.equals("fallback"));
}
}

0 comments on commit 55b5b54

Please sign in to comment.