Skip to content

Commit

Permalink
Merge pull request #3963 from eclipse/GH-3948-SimpleLiteral-Serializable
Browse files Browse the repository at this point in the history
GH-3948 Serializable CoreDatatype.NONE
  • Loading branch information
hmottestad authored Jun 6, 2022
2 parents 613adb0 + d51fbae commit 19ca714
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

public interface CoreDatatype {

CoreDatatype NONE = () -> {
throw new IllegalStateException();
};
CoreDatatype NONE = DefaultDatatype.NONE;

/**
* Checks whether the supplied datatype is an XML Schema Datatype.
Expand Down Expand Up @@ -220,7 +218,6 @@ public boolean isFloatingPointDatatype() {
* and/or times.
*
* @return true if it is a calendar type
*
* @see XMLGregorianCalendar
*/
public boolean isCalendarDatatype() {
Expand All @@ -232,7 +229,6 @@ public boolean isCalendarDatatype() {
* These are the datatypes that represents durations.
*
* @return true if it is a duration type
*
* @see Duration
*/
public boolean isDurationDatatype() {
Expand Down Expand Up @@ -354,3 +350,15 @@ public String toString() {
}

}

/**
* This needs to be its own enum because we need it to be serializable.
*/
enum DefaultDatatype implements CoreDatatype {
NONE;

@Override
public IRI getIri() {
throw new IllegalStateException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.DateTimeException;
Expand Down Expand Up @@ -79,7 +86,6 @@ public abstract class LiteralTest {
* Creates a test literal instance.
*
* @param label the label of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label);
Expand All @@ -89,7 +95,6 @@ public abstract class LiteralTest {
*
* @param label the label of the literal
* @param language the language of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label, String language);
Expand All @@ -99,7 +104,6 @@ public abstract class LiteralTest {
*
* @param label the label of the literal
* @param datatype the datatype of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label, IRI datatype);
Expand All @@ -109,7 +113,6 @@ public abstract class LiteralTest {
*
* @param label the label of the literal
* @param datatype the CoreDatatype of the literal
*
* @return a new instance of the concrete literal class under test
*/
protected abstract Literal literal(String label, CoreDatatype datatype);
Expand All @@ -118,7 +121,6 @@ public abstract class LiteralTest {
* Creates a test datatype IRI instance.
*
* @param iri the IRI of the datatype
*
* @return a new instance of the concrete datatype class under test
*/
protected abstract IRI datatype(String iri);
Expand Down Expand Up @@ -1501,4 +1503,67 @@ public final void testCoreDatatypeEqualsAndHashCodeXSDString() {
assertThat(plain.hashCode()).isEqualTo(typed.hashCode());
}

@Test
public final void testSerializationWithCoreDatatypeXsd() {
Literal literal = literal("1", datatype(XSD_INT));

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.XSD.INT, roundTrip.getCoreDatatype());
}

@Test
public final void testSerializationWithCoreDatatypeRdfLangString() {
Literal literal = literal("hei", "no");
assertEquals(CoreDatatype.RDF.LANGSTRING, literal.getCoreDatatype());

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.RDF.LANGSTRING, roundTrip.getCoreDatatype());
}

@Test
public final void testSerializationWithCoreDatatypeGEO() {
Literal literal = literal("1", CoreDatatype.GEO.WKT_LITERAL);

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.GEO.WKT_LITERAL, roundTrip.getCoreDatatype());
}

@Test
public final void testSerializationWithCoreDatatype4() {
Literal literal = literal("1", datatype("http://example.org/dt1"));
assertEquals(CoreDatatype.XSD.NONE, literal.getCoreDatatype());

byte[] bytes = objectToBytes(literal);
Literal roundTrip = (Literal) bytesToObject(bytes);

assertEquals(CoreDatatype.XSD.NONE, roundTrip.getCoreDatatype());
}

private byte[] objectToBytes(Serializable object) {
try (var byteArrayOutputStream = new ByteArrayOutputStream()) {
try (var objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
objectOutputStream.writeObject(object);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private Object bytesToObject(byte[] str) {
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str)) {
try (ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
return objectInputStream.readObject();
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ public class SimpleLiteral extends AbstractLiteral {
* The literal's language tag.
*/
private String language;
// Cache Optional instance for the language, or null if not yet computed. Marked as transient because Optional is
// not serializable.
transient private Optional<String> optionalLanguageCache = null;

/**
* The literal's datatype.
*/
private IRI datatype;

// Cached CoreDatatype, or null if not yet computed.
private CoreDatatype coreDatatype = null;

/*--------------*
Expand Down

0 comments on commit 19ca714

Please sign in to comment.