From bfd1dd3a66e30a3898e374d12c7360960af2550b Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Thu, 11 Jan 2018 16:29:23 +0100 Subject: [PATCH] Minor code cleanup. Upgraded to JSON-LD 0.11.1 (test dependency). --- pom.xml | 2 +- .../cvut/kbss/jsonld/common/BeanAnnotationProcessor.java | 8 ++------ .../jsonld/deserialization/util/DataTypeTransformer.java | 3 +-- .../cvut/kbss/jsonld/serialization/model/LiteralNode.java | 6 ++---- .../kbss/jsonld/common/BeanAnnotationProcessorTest.java | 4 ++-- .../deserialization/DefaultInstanceBuilderTest.java | 6 +++--- .../deserialization/PropertiesInstanceContextTest.java | 2 +- .../kbss/jsonld/serialization/JsonLdTreeBuilderTest.java | 4 ++-- .../serialization/traversal/ObjectGraphTraverserTest.java | 4 ++-- 9 files changed, 16 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 780fe0f..e41fd32 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ 4.12 1.10.19 - 0.8.3 + 0.11.1 diff --git a/src/main/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessor.java b/src/main/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessor.java index bf04db6..d6960fd 100644 --- a/src/main/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessor.java +++ b/src/main/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessor.java @@ -192,18 +192,14 @@ public static boolean hasPropertiesField(Class cls) { * @throws IllegalArgumentException When the specified class does not have a {@link Properties} field */ public static Field getPropertiesField(Class cls) { - // TODO Rewrite this to return optional? final List fields = getSerializableFields(cls); final Optional propsField = fields.stream().filter(BeanAnnotationProcessor::isPropertiesField).findAny(); - if (propsField.isPresent()) { - return propsField.get(); - } - throw new IllegalArgumentException(cls + " does not have a @Properties field."); + return propsField.orElseThrow(() -> new IllegalArgumentException(cls + " does not have a @Properties field.")); } /** * Extracts types field from the specified class or any of its ancestors. - * + *

* This method assumes there is at most one types field in the class hierarchy. * * @param cls The class to scan diff --git a/src/main/java/cz/cvut/kbss/jsonld/deserialization/util/DataTypeTransformer.java b/src/main/java/cz/cvut/kbss/jsonld/deserialization/util/DataTypeTransformer.java index 823fce0..98c051b 100644 --- a/src/main/java/cz/cvut/kbss/jsonld/deserialization/util/DataTypeTransformer.java +++ b/src/main/java/cz/cvut/kbss/jsonld/deserialization/util/DataTypeTransformer.java @@ -105,8 +105,7 @@ public boolean equals(Object o) { TransformationRuleIdentifier that = (TransformationRuleIdentifier) o; - if (!sourceType.equals(that.sourceType)) return false; - return targetType.equals(that.targetType); + return sourceType.equals(that.sourceType) && targetType.equals(that.targetType); } diff --git a/src/main/java/cz/cvut/kbss/jsonld/serialization/model/LiteralNode.java b/src/main/java/cz/cvut/kbss/jsonld/serialization/model/LiteralNode.java index 9e4347d..dc916a5 100644 --- a/src/main/java/cz/cvut/kbss/jsonld/serialization/model/LiteralNode.java +++ b/src/main/java/cz/cvut/kbss/jsonld/serialization/model/LiteralNode.java @@ -45,10 +45,8 @@ public boolean equals(Object o) { LiteralNode that = (LiteralNode) o; - if (getName() != null && !getName().equals(that.getName()) || getName() == null && that.getName() != null) { - return false; - } - return value.equals(that.value); + return (getName() == null || getName().equals(that.getName())) && + (getName() != null || that.getName() == null) && value.equals(that.value); } diff --git a/src/test/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessorTest.java b/src/test/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessorTest.java index c342399..5655057 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessorTest.java +++ b/src/test/java/cz/cvut/kbss/jsonld/common/BeanAnnotationProcessorTest.java @@ -140,13 +140,13 @@ public void getAttributeIdentifierThrowsIllegalArgumentWhenFieldIsNotJsonLdSeria } @Test - public void getInstanceIdentifierExtractsIdFieldValue() throws Exception { + public void getInstanceIdentifierExtractsIdFieldValue() { final Organization org = Generator.generateOrganization(); assertEquals(org.getUri(), BeanAnnotationProcessor.getInstanceIdentifier(org)); } @Test - public void getInstanceIdentifierExtractsIdFieldFromAncestorClass() throws Exception { + public void getInstanceIdentifierExtractsIdFieldFromAncestorClass() { final Employee e = Generator.generateEmployee(); assertEquals(e.getUri(), BeanAnnotationProcessor.getInstanceIdentifier(e)); } diff --git a/src/test/java/cz/cvut/kbss/jsonld/deserialization/DefaultInstanceBuilderTest.java b/src/test/java/cz/cvut/kbss/jsonld/deserialization/DefaultInstanceBuilderTest.java index 1e062aa..f400e62 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/deserialization/DefaultInstanceBuilderTest.java +++ b/src/test/java/cz/cvut/kbss/jsonld/deserialization/DefaultInstanceBuilderTest.java @@ -117,7 +117,7 @@ public void closeObjectDoesNothingForRootObject() throws Exception { } @Test - public void addValueAddsValuesToCurrentlyOpenCollection() throws Exception { + public void addValueAddsValuesToCurrentlyOpenCollection() { final List items = new ArrayList<>(); for (int i = 0; i < Generator.randomCount(10); i++) { items.add(Generator.randomCount(Integer.MAX_VALUE)); @@ -132,7 +132,7 @@ public void addValueAddsValuesToCurrentlyOpenCollection() throws Exception { } @Test - public void openObjectByPropertyCreatesObjectOfCorrectType() throws Exception { + public void openObjectByPropertyCreatesObjectOfCorrectType() { deserializer.openObject(Employee.class); deserializer.openObject(Vocabulary.IS_MEMBER_OF); assertTrue(deserializer.getCurrentRoot() instanceof Organization); @@ -149,7 +149,7 @@ public void openObjectByPropertySetsNewInstanceAsFieldValueAndReplacesCurrentCon } @Test - public void addValueSetsPropertyValue() throws Exception { + public void addValueSetsPropertyValue() { deserializer.openObject(User.class); final String firstName = "Catherine"; final String lastName = "Halsey"; diff --git a/src/test/java/cz/cvut/kbss/jsonld/deserialization/PropertiesInstanceContextTest.java b/src/test/java/cz/cvut/kbss/jsonld/deserialization/PropertiesInstanceContextTest.java index 22055c6..9e5dc08 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/deserialization/PropertiesInstanceContextTest.java +++ b/src/test/java/cz/cvut/kbss/jsonld/deserialization/PropertiesInstanceContextTest.java @@ -109,7 +109,7 @@ public void addItemInsertsValuesOfCorrectTypes() throws Exception { } @Test - public void addItemConvertsValuesToCorrectType() throws Exception { + public void addItemConvertsValuesToCorrectType() { final Map> map = new HashMap<>(); final InstanceContext ctx = new PropertiesInstanceContext(map, Vocabulary.IS_ADMIN, personProperties); ctx.addItem(true); diff --git a/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdTreeBuilderTest.java b/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdTreeBuilderTest.java index ebeb38e..ab4f142 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdTreeBuilderTest.java +++ b/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdTreeBuilderTest.java @@ -140,7 +140,7 @@ public void closeInstancePopsOriginalCurrentFromStack() throws Exception { } @Test - public void openCollectionCreatesCollectionNode() throws Exception { + public void openCollectionCreatesCollectionNode() { treeBuilder.openCollection(Collections.singleton(Generator.generateEmployee())); final CompositeNode root = treeBuilder.getTreeRoot(); assertNotNull(root); @@ -288,7 +288,7 @@ public void testAddCollectionItemAfterVisitKnownInstanceWasCalledInObject() thro } @Test - public void testBuildTreeWithRootCollection() throws Exception { + public void testBuildTreeWithRootCollection() { final Set users = Generator.generateUsers(); treeBuilder.openCollection(users); for (User u : users) { diff --git a/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java b/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java index 5242ae2..07626c0 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java +++ b/src/test/java/cz/cvut/kbss/jsonld/serialization/traversal/ObjectGraphTraverserTest.java @@ -86,7 +86,7 @@ public void traverseTraversesObjectReferences() throws Exception { } @Test - public void traverseTraversesObjectPropertyCollection() throws Exception { + public void traverseTraversesObjectPropertyCollection() { final Organization org = Generator.generateOrganization(); generateEmployees(org); traverser.traverse(org); @@ -126,7 +126,7 @@ public void traverseRecognizesAlreadyVisitedInstances() throws Exception { } @Test - public void traverseOnCollectionOpensCollectionAddsInstancesAndClosesCollection() throws Exception { + public void traverseOnCollectionOpensCollectionAddsInstancesAndClosesCollection() { final Set users = Generator.generateUsers(); traverser.traverse(users);