public static Literal convertLiteral(RyaType literal) { if (XMLSchema.STRING.equals(literal.getDataType())) { return new LiteralImpl(literal.getData()); } else { return new LiteralImpl(literal.getData(), literal.getDataType()); } //TODO: No Language support yet }
/** * Creates a string {@link RyaType} object. * @param value the {@link String} object. * @return the {@link RyaType} with the data type set to * {@link XMLSchema#STRING} and the data set to the specified {@code value}. */ public static RyaType stringRyaType(final String value) { return new RyaType(XMLSchema.STRING, value); }
@Override public int hashCode() { if (ryatype == null) { return 0; } else { return ryatype.hashCode(); } } }
/** * Determine equality based on string representations of data, datatype, and * language. * @param o The object to compare with * @return {@code true} if the other object is also a RyaType and the data, * datatype, and language all match. */ @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || !(o instanceof RyaType)) { return false; } final RyaType other = (RyaType) o; final EqualsBuilder builder = new EqualsBuilder() .append(getData(), other.getData()) .append(getDataType(), other.getDataType()) .append(getLanguage(), other.getLanguage()); return builder.isEquals(); }
@Override public byte[][] serializeType(final RyaType ryaType) throws RyaTypeResolverException { final StringBuilder dataBuilder = new StringBuilder(); dataBuilder.append(ryaType.getData()); final String validatedLanguage = LiteralLanguageUtils.validateLanguage(ryaType.getLanguage(), dataType); if (validatedLanguage != null) { dataBuilder.append(LiteralLanguageUtils.LANGUAGE_DELIMITER); dataBuilder.append(validatedLanguage); } final byte[] bytes = serializeData(dataBuilder.toString()).getBytes(StandardCharsets.UTF_8); return new byte[][]{bytes, Bytes.concat(TYPE_DELIM_BYTES, markerBytes)}; }
@Test public void testConvertLiteral_normalString() { final String expectedData = "Hello"; final Literal literal = VF.createLiteral(expectedData); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); assertEquals(XMLSchema.STRING, ryaType.getDataType()); assertEquals(expectedData, ryaType.getData()); final RyaType expectedRyaType = new RyaType(XMLSchema.STRING, expectedData); assertEquals(expectedRyaType, ryaType); assertNull(ryaType.getLanguage()); } }
public String getPrefix() { String data = getStart().getData(); return data.substring(0, data.length() - 1); } }
public void testDoubleSerialization() throws Exception { Double d = randomDouble(); RyaType ryaType = new RyaType(XMLSchema.DOUBLE, d.toString()); byte[] serialize = new DoubleRyaTypeResolver().serialize(ryaType); assertEquals(d, Double.parseDouble(new DoubleRyaTypeResolver().deserialize(serialize).getData())); }
public byte[][] serializeType(final RyaType ryaType) throws RyaTypeResolverException { final RyaTypeResolver ryaTypeResolver = retrieveResolver(ryaType.getDataType()); if (ryaTypeResolver != null) { return ryaTypeResolver.serializeType(ryaType); } return null; }
@Test public void testHashCodeEquals() throws Exception { Assert.assertEquals("Same data and same type should yield same hash code.", a.hashCode(), same.hashCode()); Assert.assertEquals("Same type and both null data should yield same hash code.", nullData.hashCode(), new RyaType(XMLSchema.STRING, null).hashCode()); Assert.assertEquals("Same data and both null type should yield same hash code.", nullType.hashCode(), new RyaType(null, "http://www.example.com/Alice").hashCode()); Assert.assertEquals("Null type and null data should yield same hash code.", nullBoth.hashCode(), new RyaType(null, null).hashCode()); } }
@Test public void testEqualsNullFields() throws Exception { Assert.assertFalse("equals(null) should return false.", a.equals(null)); Assert.assertFalse("Same data, one null datatype should be unequal.", a.equals(nullType)); Assert.assertFalse("Same datatype, one null data should be unequal.", a.equals(nullData)); Assert.assertFalse("Same datatype, data, one null lang should be unequal.", aLang.equals(nullLang)); final RyaType sameNull = new RyaType(null, null); Assert.assertTrue("Matching null fields should be equal.", sameNull.equals(nullBoth)); }
@Test public void testConvertLiteral_nullDataType() { final Literal literal = mock(SimpleLiteral.class); final String expectedData = "Ice Cream"; when(literal.getLabel()).thenReturn(expectedData); when(literal.stringValue()).thenReturn(expectedData); // Don't think this is possible but test anyways. Need to mock to force this null value. when(literal.getDatatype()).thenReturn(null); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); final RyaType expected = new RyaType(XMLSchema.STRING, expectedData); assertEquals(expected, ryaType); assertNull(ryaType.getLanguage()); }
private static boolean isResult(final RyaType query, final RyaType result) { return (query == null) || query.equals(result); }
@Test public void testEqualsCompareToConsistency() throws Exception { Assert.assertEquals("equals and compareTo inconsistent for matching values and types.", a.equals(same), a.compareTo(same) == 0); Assert.assertEquals("equals and compareTo inconsistent for different values with same types.", a.equals(b), a.compareTo(b) == 0); Assert.assertEquals("equals and compareTo inconsistent for same values having different types.", a.equals(aIri), a.compareTo(aIri) == 0); Assert.assertEquals("equals and compareTo inconsistent for different values and different types.", a.equals(bIri), a.compareTo(bIri) == 0); Assert.assertEquals("equals and compareTo inconsistent for different lang and same types/data.", aLang.equals(bLang), aLang.compareTo(bLang) == 0); }
@Override public int compareTo(RyaTypeWritable o) { return ryatype.compareTo(o.ryatype); }
@Override public byte[][] serializeType(final RyaType ryaType) throws RyaTypeResolverException { final StringBuilder dataBuilder = new StringBuilder(); dataBuilder.append(ryaType.getData()); final String validatedLanguage = LiteralLanguageUtils.validateLanguage(ryaType.getLanguage(), ryaType.getDataType()); if (validatedLanguage != null) { dataBuilder.append(LiteralLanguageUtils.LANGUAGE_DELIMITER); dataBuilder.append(validatedLanguage); } // Combine data and language final byte[] bytes = serializeData(dataBuilder.toString()).getBytes(StandardCharsets.UTF_8); return new byte[][]{bytes, Bytes.concat(TYPE_DELIM_BYTES, ryaType.getDataType().stringValue().getBytes(StandardCharsets.UTF_8), TYPE_DELIM_BYTES, markerBytes)}; }
@Test public void testConvertLiteral_undeterminedLanguage() { final String expectedData = "Hello"; final String language = LiteralLanguageUtils.UNDETERMINED_LANGUAGE; assertTrue(Literals.isValidLanguageTag(language)); final Literal literal = VF.createLiteral(expectedData, language); final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal); assertEquals(RDF.LANGSTRING, ryaType.getDataType()); assertEquals(expectedData, ryaType.getData()); final RyaType expectedRyaType = new RyaType(RDF.LANGSTRING, expectedData, language); assertEquals(expectedRyaType, ryaType); assertEquals(LiteralLanguageUtils.UNDETERMINED_LANGUAGE, ryaType.getLanguage()); }
/** * Converts a {@link RyaType} into a {@link IRI} representation of the * {@code ryaType}. * @param ryaType the {@link RyaType} to convert. * @return the {@link IRI} representation of the {@code ryaType}. */ private static IRI convertIRI(final RyaType ryaType) { return VF.createIRI(ryaType.getData()); }
public byte[] serialize(final RyaType ryaType) throws RyaTypeResolverException { final RyaTypeResolver ryaTypeResolver = retrieveResolver(ryaType.getDataType()); if (ryaTypeResolver != null) { return ryaTypeResolver.serialize(ryaType); } return null; }