From 016f13f54bc955b87a6a1b9a37dd605aa280d2e3 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 3 Apr 2019 14:56:56 +0100 Subject: [PATCH] Add CurieUtil.getCuriePrefix(iri) for Issue #4 --- src/main/java/org/prefixcommons/CurieUtil.java | 8 ++++++++ src/test/java/org/prefixcommons/CurieUtilTest.java | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/org/prefixcommons/CurieUtil.java b/src/main/java/org/prefixcommons/CurieUtil.java index 9f616cf..10087c9 100644 --- a/src/main/java/org/prefixcommons/CurieUtil.java +++ b/src/main/java/org/prefixcommons/CurieUtil.java @@ -92,6 +92,14 @@ public Optional getCurie(String iri) { } } + public Optional getCuriePrefix(String iri) { + String prefix = trie.getMatchingPrefix(iri); + if (prefix.equals("")) { + return Optional.empty(); + } + return Optional.of(curieMap.inverse().get(prefix)); + } + /*** * Expands a CURIE to a full IRI, if mapped. * diff --git a/src/test/java/org/prefixcommons/CurieUtilTest.java b/src/test/java/org/prefixcommons/CurieUtilTest.java index 114ade7..cfb5fff 100644 --- a/src/test/java/org/prefixcommons/CurieUtilTest.java +++ b/src/test/java/org/prefixcommons/CurieUtilTest.java @@ -88,4 +88,9 @@ public void fromJsonLdFile() throws IOException { is(Optional.of("http://purl.obolibrary.org/obo/XAO_foo"))); } + @Test + public void getIriPrefix() { + assertThat(util.getCuriePrefix("http://x.org/a_12345"), is(Optional.of("A"))); + assertThat(util.getCuriePrefix("http://x.org/C_C12345"), is(Optional.of("CC"))); + } }