From 3e0159dcd4efcfe70b221ab87bd70f57c8ef172d Mon Sep 17 00:00:00 2001 From: Sandro La Bruzzo Date: Fri, 28 Apr 2023 11:22:45 +0200 Subject: [PATCH] added LookUp method for RelClass, this method replaces the ModelSupport.findRelation --- .../java/eu/dnetlib/dhp/schema/oaf/Relation.java | 16 +++++++++++++--- .../eu/dnetlib/dhp/schema/oaf/RelationTest.java | 10 +++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java b/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java index 11812da..9254f2a 100644 --- a/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java +++ b/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java @@ -3,9 +3,7 @@ package eu.dnetlib.dhp.schema.oaf; import java.beans.Transient; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.util.*; /** * Relation models any edge between two nodes in the OpenAIRE graph. It has a source id and a target id pointing to @@ -179,6 +177,18 @@ public class Relation extends Oaf implements Serializable { throw new IllegalArgumentException("missing SubRel mapping for" + this); } + /** + * This mehtod is an implementation of the valueOF case insensitive + * @param value the input Value + * @return the RELCLASS + */ + public static RELCLASS lookUp(String value) { + Optional rvlaue = Arrays.stream(RELCLASS.values()).filter(e -> e.name().equalsIgnoreCase(value)).findAny(); + if (rvlaue.isPresent()) + return rvlaue.get(); + throw new IllegalArgumentException("value: "+value+" not found"); + } + @Transient public RELCLASS getInverse() { switch (this) { diff --git a/src/test/java/eu/dnetlib/dhp/schema/oaf/RelationTest.java b/src/test/java/eu/dnetlib/dhp/schema/oaf/RelationTest.java index 6b14242..a54ece7 100644 --- a/src/test/java/eu/dnetlib/dhp/schema/oaf/RelationTest.java +++ b/src/test/java/eu/dnetlib/dhp/schema/oaf/RelationTest.java @@ -2,6 +2,7 @@ package eu.dnetlib.dhp.schema.oaf; import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class RelationTest { @@ -13,7 +14,7 @@ public class RelationTest { } @Test - public void checSubRelType() { + public void checkSubRelType() { Relation.RELCLASS rc = Relation.RELCLASS.merges; assertEquals(Relation.SUBRELTYPE.dedup, rc.getSubRel()); @@ -23,4 +24,11 @@ public class RelationTest { } } + @Test + public void lookRelClassUpTest() { + Assertions.assertEquals(Relation.RELCLASS.Compiles, Relation.RELCLASS.lookUp("compiles")); + Assertions.assertEquals(Relation.RELCLASS.IsMetadataFor, Relation.RELCLASS.lookUp("isMetaDATAFOr")); + Assertions.assertThrows(IllegalArgumentException.class, () -> Relation.RELCLASS.lookUp("hello")); + } + }