use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class ModelsTest method testGetPropertyInvalidInput.
@Test
public void testGetPropertyInvalidInput() {
Literal lit1 = VF.createLiteral(1.0);
model1.add(foo, bar, lit1);
model1.add(foo, bar, foo);
try {
Models.getProperty(model1, foo, null).orElse(null);
fail("should have resulted in exception");
} catch (NullPointerException e) {
// expected
}
try {
Models.getProperty(model1, null, bar).orElse(null);
fail("should have resulted in exception");
} catch (NullPointerException e) {
// expected
}
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class ModelsTest method testObject.
public void testObject() {
Literal lit = VF.createLiteral(1.0);
model1.add(foo, bar, lit);
model1.add(foo, bar, foo);
Value result = Models.object(model1).orElse(null);
assertNotNull(result);
assertTrue(result.equals(lit) || result.equals(foo));
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class QueryResults method bindingSetsMatch.
private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNode, BNode> bNodeMapping) {
if (bs1.size() != bs2.size()) {
return false;
}
for (Binding binding1 : bs1) {
Value value1 = binding1.getValue();
Value value2 = bs2.getValue(binding1.getName());
if (value1 instanceof BNode && value2 instanceof BNode) {
BNode mappedBNode = bNodeMapping.get(value1);
if (mappedBNode != null) {
// bNode 'value1' was already mapped to some other bNode
if (!value2.equals(mappedBNode)) {
// 'value1' and 'value2' do not match
return false;
}
} else {
// possible mapping candidate
if (bNodeMapping.containsValue(value2)) {
// 'value2' is already mapped to some other value.
return false;
}
}
} else {
// values are not (both) bNodes
if (value1 instanceof Literal && value2 instanceof Literal) {
// do literal value-based comparison for supported datatypes
Literal leftLit = (Literal) value1;
Literal rightLit = (Literal) value2;
IRI dt1 = leftLit.getDatatype();
IRI dt2 = rightLit.getDatatype();
if (dt1 != null && dt2 != null && dt1.equals(dt2) && XMLDatatypeUtil.isValidValue(leftLit.getLabel(), dt1) && XMLDatatypeUtil.isValidValue(rightLit.getLabel(), dt2)) {
Integer compareResult = null;
if (dt1.equals(XMLSchema.DOUBLE)) {
compareResult = Double.compare(leftLit.doubleValue(), rightLit.doubleValue());
} else if (dt1.equals(XMLSchema.FLOAT)) {
compareResult = Float.compare(leftLit.floatValue(), rightLit.floatValue());
} else if (dt1.equals(XMLSchema.DECIMAL)) {
compareResult = leftLit.decimalValue().compareTo(rightLit.decimalValue());
} else if (XMLDatatypeUtil.isIntegerDatatype(dt1)) {
compareResult = leftLit.integerValue().compareTo(rightLit.integerValue());
} else if (dt1.equals(XMLSchema.BOOLEAN)) {
Boolean leftBool = Boolean.valueOf(leftLit.booleanValue());
Boolean rightBool = Boolean.valueOf(rightLit.booleanValue());
compareResult = leftBool.compareTo(rightBool);
} else if (XMLDatatypeUtil.isCalendarDatatype(dt1)) {
XMLGregorianCalendar left = leftLit.calendarValue();
XMLGregorianCalendar right = rightLit.calendarValue();
compareResult = left.compare(right);
}
if (compareResult != null) {
if (compareResult.intValue() != 0) {
return false;
}
} else if (!value1.equals(value2)) {
return false;
}
} else if (!value1.equals(value2)) {
return false;
}
} else if (!value1.equals(value2)) {
return false;
}
}
}
return true;
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class SimpleLiteralTest method testStringLong.
/**
* Test method for {@link org.eclipse.rdf4j.model.impl.SimpleLiteral#SimpleLiteral(java.lang.String)}.
*/
@Test
public final void testStringLong() throws Exception {
StringBuilder testBuilder = new StringBuilder(1000000);
for (int i = 0; i < 1000000; i++) {
testBuilder.append(Integer.toHexString(i % 16));
}
Literal test = new SimpleLiteral(testBuilder.toString());
assertEquals(testBuilder.toString(), test.getLabel());
assertFalse(test.getLanguage().isPresent());
assertEquals(XMLSchema.STRING, test.getDatatype());
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class SimpleLiteralTest method testStringIRIEmptyNull.
/**
* Test method for
* {@link org.eclipse.rdf4j.model.impl.SimpleLiteral#SimpleLiteral(java.lang.String, org.eclipse.rdf4j.model.IRI)}
* .
*/
@Test
public final void testStringIRIEmptyNull() throws Exception {
String label = "";
IRI datatype = null;
Literal test = new SimpleLiteral(label, datatype);
assertEquals("", test.getLabel());
assertFalse(test.getLanguage().isPresent());
assertEquals(XMLSchema.STRING, test.getDatatype());
}
Aggregations