Skip to content

Commit

Permalink
[Enhancement kbss-cvut#294] Add support for primitive char Java type
Browse files Browse the repository at this point in the history
  • Loading branch information
luxbe committed Dec 17, 2024
1 parent 643e8f9 commit c509e48
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ void registerConverter(Class<?> attributeType, ConverterWrapper<?, ?> converter)
Map.entry(LangString.class, new ToLangStringConverter()),
Map.entry(URI.class, new ToURIConverter()),
Map.entry(URL.class, new ToURLConverter()),
Map.entry(Character.class, new CharacterConverter()));
Map.entry(Character.class, new CharacterConverter()),
Map.entry(char.class, new CharacterConverter()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package cz.cvut.kbss.jopa.model.metamodel;

import cz.cvut.kbss.jopa.datatype.DatatypeTransformer;
import cz.cvut.kbss.jopa.model.IRI;
import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.model.annotations.CascadeType;
Expand Down Expand Up @@ -133,7 +134,9 @@ private void resolveEnumType(PropertyInfo propertyInfo, Class<?> fieldValueCls)
}

String resolveLanguage(Class<?> fieldValueCls) {
return MultilingualString.class.equals(fieldValueCls) || Character.class.equals(fieldValueCls) ? null : typeBuilderContext.getPuLanguage();
return MultilingualString.class.equals(fieldValueCls) || Character.class.equals(fieldValueCls) || char.class.equals(fieldValueCls)
? null
: typeBuilderContext.getPuLanguage();
}

static PropertyAttributes create(PropertyInfo field, FieldMappingValidator validator, TypeBuilderContext<?> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class OWLClassBB implements HasUri {
@OWLDataProperty(iri = Vocabulary.P_BB_DOUBLE_ATTRIBUTE)
private double doubleAttribute;

@OWLDataProperty(iri = Vocabulary.P_BB_CHAR_ATTRIBUTE)
private char charAttribute;

public OWLClassBB() {
}

Expand Down Expand Up @@ -121,6 +124,14 @@ public void setBooleanAttribute(boolean booleanAttribute) {
this.booleanAttribute = booleanAttribute;
}

public char getCharAttribute() {
return charAttribute;
}

public void setCharAttribute(char charAttribute) {
this.charAttribute = charAttribute;
}

@Override
public String toString() {
return "OWLClassBB{" +
Expand All @@ -132,6 +143,7 @@ public String toString() {
", longAttribute=" + longAttribute +
", floatAttribute=" + floatAttribute +
", doubleAttribute=" + doubleAttribute +
", charAttribute=" + charAttribute +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private void init() {
entityBB.setLongAttribute(20L);
entityBB.setFloatAttribute(25.5f);
entityBB.setDoubleAttribute(30.7d);
entityBB.setCharAttribute('j');
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ void testPersistEntityWithBasicPrimitiveTypeAttributes() {
assertEquals(entityBB.getLongAttribute(), res.getLongAttribute());
assertEquals(entityBB.getFloatAttribute(), res.getFloatAttribute());
assertEquals(entityBB.getDoubleAttribute(), res.getDoubleAttribute());
assertEquals(entityBB.getCharAttribute(), res.getCharAttribute());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void testRetrievePrimitive() {
assertEquals(entityBB.getLongAttribute(), res.getLongAttribute());
assertEquals(entityBB.getFloatAttribute(), res.getFloatAttribute());
assertEquals(entityBB.getDoubleAttribute(), res.getDoubleAttribute());
assertEquals(entityBB.getCharAttribute(), res.getCharAttribute());
assertTrue(em.contains(res));
}

Expand All @@ -147,6 +148,7 @@ void testRetrieveMissingPrimitive() throws Exception {
assertEquals(0L, res.getLongAttribute());
assertEquals(0.0f, res.getFloatAttribute());
assertEquals(0.0d, res.getDoubleAttribute());
assertEquals('\u0000', res.getCharAttribute());
assertTrue(em.contains(res));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
import cz.cvut.kbss.jopa.test.environment.Quad;
import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
import cz.cvut.kbss.jopa.vocabulary.RDF;
import cz.cvut.kbss.jopa.vocabulary.XSD;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -1063,6 +1064,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() {
entityBB.setLongAttribute(20L);
entityBB.setFloatAttribute(25.5f);
entityBB.setDoubleAttribute(30.7);
entityBB.setCharAttribute('c');
persist(entityBB);

final int newIntValue = 20;
Expand All @@ -1072,6 +1074,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() {
final long newLongValue = 9L;
final float newFloatValue = 3.2f;
final double newDoubleValue = 8.9d;
final char newCharValue = 'o';

em.getTransaction().begin();
final OWLClassBB toUpdate = findRequired(OWLClassBB.class, entityBB.getUri());
Expand All @@ -1082,6 +1085,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() {
toUpdate.setLongAttribute(newLongValue);
toUpdate.setFloatAttribute(newFloatValue);
toUpdate.setDoubleAttribute(newDoubleValue);
toUpdate.setCharAttribute(newCharValue);
em.getTransaction().commit();

verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_INT_ATTRIBUTE, XSD.INT);
Expand All @@ -1091,6 +1095,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() {
verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_LONG_ATTRIBUTE, XSD.LONG);
verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_FLOAT_ATTRIBUTE, XSD.FLOAT);
verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_DOUBLE_ATTRIBUTE, XSD.DOUBLE);
verifyValueDatatype(entityBB.getUri(), Vocabulary.P_BB_CHAR_ATTRIBUTE, XSD.STRING);
final OWLClassBB res = findRequired(OWLClassBB.class, entityBB.getUri());
assertEquals(entityBB.getUri(), res.getUri());
assertEquals(newIntValue, res.getIntAttribute());
Expand All @@ -1100,6 +1105,7 @@ void updateSupportsUpdatingPrimitiveLiteralValue() {
assertEquals(newLongValue, res.getLongAttribute());
assertEquals(newFloatValue, res.getFloatAttribute());
assertEquals(newDoubleValue, res.getDoubleAttribute());
assertEquals(newCharValue, res.getCharAttribute());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public static Literal createLiteral(Object value, String language, ValueFactory

if (value instanceof Integer) {
return vf.createLiteral((Integer) value);
} else if(value instanceof Character c) {
return vf.createLiteral(c.toString());
} else if (value instanceof String) {
return language != null ? vf.createLiteral((String) value, language) : vf.createLiteral((String) value);
} else if (value instanceof LangString ls) {
Expand Down

0 comments on commit c509e48

Please sign in to comment.