Skip to content

Commit

Permalink
Merge pull request #3619 from eclipse/GH-3585-improve-sorting-3
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad authored Jan 22, 2022
2 parents 55f932e + 89aeef7 commit 6b1a018
Show file tree
Hide file tree
Showing 89 changed files with 2,545 additions and 918 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public interface ValueFactory {
*/
Literal createLiteral(String label, CoreDatatype datatype);

/**
* Creates a new literal with the supplied label and datatype.
*
* @param label The literal's label, must not be <var>null</var>.
* @param datatype The literal's datatype. If it is null, the datatype
* <a href="http://www.w3.org/2001/XMLSchema#string">{@code xsd:string}</a> will be assigned to this
* literal.
*/
Literal createLiteral(String label, IRI datatype, CoreDatatype coreDatatype);

/**
* Creates a new <var>xsd:boolean</var>-typed literal representing the specified value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ static class TypedLiteral extends AbstractLiteral {
this.datatype = datatype.getIri();
}

TypedLiteral(String label, IRI datatype, CoreDatatype coreDatatype) {
assert datatype != null;
assert coreDatatype != null;
assert coreDatatype == CoreDatatype.NONE || datatype == coreDatatype.getIri();

this.label = label;
this.datatype = datatype;
this.coreDatatype = coreDatatype;
}

@Override
public String getLabel() {
return label;
Expand Down Expand Up @@ -379,7 +389,7 @@ private static String toString(double value) {
protected Number value;

private final String label;
private final CoreDatatype datatype;
private final CoreDatatype.XSD datatype;

NumberLiteral(byte value) {
this(value, Byte.toString(value), CoreDatatype.XSD.BYTE);
Expand All @@ -405,7 +415,7 @@ private static String toString(double value) {
this(value, toString(value), CoreDatatype.XSD.DOUBLE);
}

NumberLiteral(Number value, String label, CoreDatatype datatype) {
NumberLiteral(Number value, String label, CoreDatatype.XSD datatype) {
this.value = value;
this.label = label;
this.datatype = datatype;
Expand Down Expand Up @@ -585,7 +595,7 @@ static class TemporalAccessorLiteral extends AbstractLiteral {

.toFormatter();

private static final Map<Integer, CoreDatatype> DATATYPES = datatypes();
private static final Map<Integer, CoreDatatype.XSD> DATATYPES = datatypes();
private static final Map<CoreDatatype.XSD, DateTimeFormatter> FORMATTERS = formatters();

static TemporalAccessor parseTemporalAccessor(String label) throws DateTimeException {
Expand All @@ -601,14 +611,14 @@ static TemporalAccessor parseTemporalAccessor(String label) throws DateTimeExcep
return value;
}

private static Map<Integer, CoreDatatype> datatypes() {
private static Map<Integer, CoreDatatype.XSD> datatypes() {

int date = key(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH);
int time = key(HOUR_OF_DAY, MINUTE_OF_HOUR, SECOND_OF_MINUTE);
int nano = key(NANO_OF_SECOND);
int zone = key(OFFSET_SECONDS);

Map<Integer, CoreDatatype> datatypes = new HashMap<>();
Map<Integer, CoreDatatype.XSD> datatypes = new HashMap<>();

datatypes.put(date + time, CoreDatatype.XSD.DATETIME);
datatypes.put(date + time + nano, CoreDatatype.XSD.DATETIME);
Expand Down Expand Up @@ -689,7 +699,7 @@ private static int key(Predicate<ChronoField> include, ChronoField... fields) {
private final TemporalAccessor value;

private final String label;
private final CoreDatatype datatype;
private final CoreDatatype.XSD datatype;

TemporalAccessorLiteral(TemporalAccessor value) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ public Literal createLiteral(String label, CoreDatatype datatype) {
return new TypedLiteral(label, datatype);
}

@Override
public Literal createLiteral(String label, IRI datatype, CoreDatatype coreDatatype) {
Objects.requireNonNull(label, "Label may not be null");
Objects.requireNonNull(datatype, "Datatype may not be null");
Objects.requireNonNull(coreDatatype, "CoreDatatype may not be null");

if (reserved(coreDatatype)) {
throw new IllegalArgumentException("reserved datatype <" + datatype + ">");
}

return new TypedLiteral(label, datatype, coreDatatype);
}

@Override
public Literal createLiteral(String label, String language) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,17 @@ public Optional<XSD> asXSDDatatype() {
return optional;
}

@Override
public String toString() {
return iri.toString();
}

}

enum RDF implements CoreDatatype {

HTML(iri("HTML")),
XMLLITERAL(iri("XMLLiteral")),
LANGSTRING(iri("langString"));

public static final String NAMESPACE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
Expand Down Expand Up @@ -299,6 +306,11 @@ public Optional<RDF> asRDFDatatype() {
return optional;
}

@Override
public String toString() {
return iri.toString();
}

}

enum GEO implements CoreDatatype {
Expand Down Expand Up @@ -335,6 +347,10 @@ public Optional<GEO> asGEODatatype() {
return optional;
}

@Override
public String toString() {
return iri.toString();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@InternalUseOnly
class CoreDatatypeHelper {

static Map<IRI, CoreDatatype> reverseLookup;
private static Map<IRI, CoreDatatype> reverseLookup;

static Map<IRI, CoreDatatype> getReverseLookup() {

Expand Down Expand Up @@ -55,7 +55,7 @@ static class DatatypeIRI extends AbstractIRI {
public DatatypeIRI(String namespace, String localName) {
this.namespace = namespace;
this.localName = localName;
this.stringValue = namespace.concat(localName);
this.stringValue = namespace.concat(localName).intern();
}

@Override
Expand All @@ -73,5 +73,9 @@ public String getLocalName() {
return localName;
}

@Override
public String toString() {
return stringValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package org.eclipse.rdf4j.model.base;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -24,9 +26,9 @@ public class CoreDatatypeTest {

@Test
public void testOrderOfXSD() {
ArrayList<CoreDatatype.XSD> datatypes = getDatatypesShuffled();
ArrayList<CoreDatatype.XSD> datatypes = getXSDDatatypesShuffled();

List<String> datatypeIRIs = getDatatypesShuffled().stream()
List<String> datatypeIRIs = getXSDDatatypesShuffled().stream()
.map(CoreDatatype::getIri)
.map(Object::toString)
.collect(Collectors.toList());
Expand All @@ -42,13 +44,48 @@ public void testOrderOfXSD() {

}

private ArrayList<CoreDatatype.XSD> getDatatypesShuffled() {
Random random = new Random(42353245);
@Test
public void testOrderOfRDF() {
ArrayList<CoreDatatype.RDF> datatypes = getRDFDatatypesShuffled();

ArrayList<CoreDatatype.XSD> xsds = new ArrayList<>(Arrays.asList(CoreDatatype.XSD.values()));
List<String> datatypeIRIs = getRDFDatatypesShuffled().stream()
.map(CoreDatatype::getIri)
.map(Object::toString)
.collect(Collectors.toList());

Collections.sort(datatypes);
Collections.sort(datatypeIRIs);

List<String> datatypeIRIsSortedByEnum = datatypes.stream()
.map(CoreDatatype::getIri)
.map(Object::toString)
.collect(Collectors.toList());
Assert.assertEquals(datatypeIRIs, datatypeIRIsSortedByEnum);

}

private ArrayList<CoreDatatype.XSD> getXSDDatatypesShuffled() {

ArrayList<CoreDatatype.XSD> datatypes = new ArrayList<>(Arrays.asList(CoreDatatype.XSD.values()));

Collections.shuffle(datatypes, new Random(42353245));
return datatypes;
}

private ArrayList<CoreDatatype.RDF> getRDFDatatypesShuffled() {

ArrayList<CoreDatatype.RDF> datatypes = new ArrayList<>(Arrays.asList(CoreDatatype.RDF.values()));

Collections.shuffle(datatypes, new Random(42353245));
return datatypes;
}

@Test
public void testToString() {
for (CoreDatatype value : CoreDatatypeHelper.getReverseLookup().values()) {
assertSame(value.toString(), value.getIri().toString());
}

Collections.shuffle(xsds);
return xsds;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,77 +32,57 @@ public class RDF {
public static final Namespace NS = Vocabularies.createNamespace(PREFIX, NAMESPACE);

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#type */
public final static IRI TYPE;
public final static IRI TYPE = Vocabularies.createIRI(RDF.NAMESPACE, "type");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#Property */
public final static IRI PROPERTY;
public final static IRI PROPERTY = Vocabularies.createIRI(RDF.NAMESPACE, "Property");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral */
public final static IRI XMLLITERAL;
public final static IRI XMLLITERAL = CoreDatatype.RDF.XMLLITERAL.getIri();

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#subject */
public final static IRI SUBJECT;
public final static IRI SUBJECT = Vocabularies.createIRI(RDF.NAMESPACE, "subject");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate */
public final static IRI PREDICATE;
public final static IRI PREDICATE = Vocabularies.createIRI(RDF.NAMESPACE, "predicate");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#object */
public final static IRI OBJECT;
public final static IRI OBJECT = Vocabularies.createIRI(RDF.NAMESPACE, "object");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement */
public final static IRI STATEMENT;
public final static IRI STATEMENT = Vocabularies.createIRI(RDF.NAMESPACE, "Statement");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag */
public final static IRI BAG;
public final static IRI BAG = Vocabularies.createIRI(RDF.NAMESPACE, "Bag");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt */
public final static IRI ALT;
public final static IRI ALT = Vocabularies.createIRI(RDF.NAMESPACE, "Alt");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq */
public final static IRI SEQ;
public final static IRI SEQ = Vocabularies.createIRI(RDF.NAMESPACE, "Seq");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#value */
public final static IRI VALUE;
public final static IRI VALUE = Vocabularies.createIRI(RDF.NAMESPACE, "value");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#li */
public final static IRI LI;
public final static IRI LI = Vocabularies.createIRI(RDF.NAMESPACE, "li");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#List */
public final static IRI LIST;
public final static IRI LIST = Vocabularies.createIRI(RDF.NAMESPACE, "List");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#first */
public final static IRI FIRST;
public final static IRI FIRST = Vocabularies.createIRI(RDF.NAMESPACE, "first");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#rest */
public final static IRI REST;
public final static IRI REST = Vocabularies.createIRI(RDF.NAMESPACE, "rest");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#nil */
public final static IRI NIL;
public final static IRI NIL = Vocabularies.createIRI(RDF.NAMESPACE, "nil");

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#langString */
public static final IRI LANGSTRING;
public static final IRI LANGSTRING = CoreDatatype.RDF.LANGSTRING.getIri();

/** http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML */
public static final IRI HTML;

static {
TYPE = Vocabularies.createIRI(RDF.NAMESPACE, "type");
PROPERTY = Vocabularies.createIRI(RDF.NAMESPACE, "Property");
XMLLITERAL = Vocabularies.createIRI(RDF.NAMESPACE, "XMLLiteral");
SUBJECT = Vocabularies.createIRI(RDF.NAMESPACE, "subject");
PREDICATE = Vocabularies.createIRI(RDF.NAMESPACE, "predicate");
OBJECT = Vocabularies.createIRI(RDF.NAMESPACE, "object");
STATEMENT = Vocabularies.createIRI(RDF.NAMESPACE, "Statement");
BAG = Vocabularies.createIRI(RDF.NAMESPACE, "Bag");
ALT = Vocabularies.createIRI(RDF.NAMESPACE, "Alt");
SEQ = Vocabularies.createIRI(RDF.NAMESPACE, "Seq");
VALUE = Vocabularies.createIRI(RDF.NAMESPACE, "value");
LI = Vocabularies.createIRI(RDF.NAMESPACE, "li");
LIST = Vocabularies.createIRI(RDF.NAMESPACE, "List");
FIRST = Vocabularies.createIRI(RDF.NAMESPACE, "first");
REST = Vocabularies.createIRI(RDF.NAMESPACE, "rest");
NIL = Vocabularies.createIRI(RDF.NAMESPACE, "nil");
LANGSTRING = CoreDatatype.RDF.LANGSTRING.getIri();
HTML = Vocabularies.createIRI(RDF.NAMESPACE, "HTML");
}
public static final IRI HTML = CoreDatatype.RDF.HTML.getIri();

}
Loading

0 comments on commit 6b1a018

Please sign in to comment.