From 016f13f54bc955b87a6a1b9a37dd605aa280d2e3 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 3 Apr 2019 14:56:56 +0100 Subject: [PATCH 1/6] 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"))); + } } From 46e32edc134b816a5d43d910ffc7f601ee3ec061 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 3 Apr 2019 15:17:55 +0100 Subject: [PATCH 2/6] Use Map interface in TrieNode --- src/main/java/org/prefixcommons/trie/Trie.java | 6 +++--- src/main/java/org/prefixcommons/trie/TrieNode.java | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/prefixcommons/trie/Trie.java b/src/main/java/org/prefixcommons/trie/Trie.java index 399b0b3..6369edc 100644 --- a/src/main/java/org/prefixcommons/trie/Trie.java +++ b/src/main/java/org/prefixcommons/trie/Trie.java @@ -1,6 +1,6 @@ package org.prefixcommons.trie; -import java.util.HashMap; +import java.util.Map; /** * Data structure specific for characters, to efficiently resolve IRI prefixes to CURIEs prefixes. @@ -25,7 +25,7 @@ public void insert(String word) { // Traverse through all characters of given word for (int level = 0; level < length; level++) { - HashMap child = crawl.getChildren(); + Map child = crawl.getChildren(); char ch = word.charAt(level); // If there is already a child for current character of given word @@ -62,7 +62,7 @@ public String getMatchingPrefix(String input) { char ch = input.charAt(level); // HashMap of current Trie node to traverse down - HashMap child = crawl.getChildren(); + Map child = crawl.getChildren(); // See if there is a Trie edge for the current character if (child.containsKey(ch)) { diff --git a/src/main/java/org/prefixcommons/trie/TrieNode.java b/src/main/java/org/prefixcommons/trie/TrieNode.java index 13f4f46..6e73eed 100644 --- a/src/main/java/org/prefixcommons/trie/TrieNode.java +++ b/src/main/java/org/prefixcommons/trie/TrieNode.java @@ -1,6 +1,7 @@ package org.prefixcommons.trie; import java.util.HashMap; +import java.util.Map; /** * Represents a Node in a Trie. @@ -8,7 +9,7 @@ */ class TrieNode { private char value; - private HashMap children; + private Map children; private boolean isLeaf; public TrieNode(char ch) { @@ -17,7 +18,7 @@ public TrieNode(char ch) { isLeaf = false; } - public HashMap getChildren() { + public Map getChildren() { return children; } From b9aa33d9de2f4b9817d46312863ae17d9af299c0 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 3 Apr 2019 15:28:16 +0100 Subject: [PATCH 3/6] Use StringBuilder for concatenating strings in Trie - Issue #6 Remove superfluous comments --- src/main/java/org/prefixcommons/trie/Trie.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/prefixcommons/trie/Trie.java b/src/main/java/org/prefixcommons/trie/Trie.java index 6369edc..95f47dd 100644 --- a/src/main/java/org/prefixcommons/trie/Trie.java +++ b/src/main/java/org/prefixcommons/trie/Trie.java @@ -48,8 +48,8 @@ public void insert(String word) { * @return the longest matching prefix */ public String getMatchingPrefix(String input) { - String result = ""; // Initialize resultant string - int length = input.length(); // Find length of the input string + StringBuilder result = new StringBuilder(); + int length = input.length(); // Initialize reference to traverse through Trie TrieNode crawl = root; @@ -66,7 +66,7 @@ public String getMatchingPrefix(String input) { // See if there is a Trie edge for the current character if (child.containsKey(ch)) { - result += ch; // Update result + result.append(ch); // Update result crawl = child.get(ch); // Update crawl to move down in Trie // If this is end of a word, then update prevMatch @@ -78,11 +78,11 @@ public String getMatchingPrefix(String input) { // If the last processed character did not match end of a word, // return the previously matching prefix - if (!crawl.isLeaf()) + if (!crawl.isLeaf()) { return result.substring(0, prevMatch); - - else - return result; + } else { + return result.toString(); + } } } From dbf3762baf99ef03f98d7e3dab8ba27ecdbdd430 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 3 Apr 2019 15:30:08 +0100 Subject: [PATCH 4/6] Tidy-up Trie - add braces to if/else, reduce scope of level variable --- src/main/java/org/prefixcommons/trie/Trie.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prefixcommons/trie/Trie.java b/src/main/java/org/prefixcommons/trie/Trie.java index 95f47dd..5d88d0a 100644 --- a/src/main/java/org/prefixcommons/trie/Trie.java +++ b/src/main/java/org/prefixcommons/trie/Trie.java @@ -56,8 +56,8 @@ public String getMatchingPrefix(String input) { // Iterate through all characters of input string 'str' and traverse // down the Trie - int level, prevMatch = 0; - for (level = 0; level < length; level++) { + int prevMatch = 0; + for (int level = 0; level < length; level++) { // Find current character of str char ch = input.charAt(level); @@ -70,10 +70,12 @@ public String getMatchingPrefix(String input) { crawl = child.get(ch); // Update crawl to move down in Trie // If this is end of a word, then update prevMatch - if (crawl.isLeaf()) + if (crawl.isLeaf()) { prevMatch = level + 1; - } else + } + } else { break; + } } // If the last processed character did not match end of a word, From 13e7138ffb3bace5fff3bc62b6fe825c73dcc604 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 3 Apr 2019 15:35:40 +0100 Subject: [PATCH 5/6] Add JetBrains template to .gitignore --- .gitignore | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/.gitignore b/.gitignore index e3ef491..ec87d5c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,28 @@ bin/ .project .classpath .settings/ + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/** +# File-based project format +*.iws +*.iml + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties From 75f15f64832e792e6f17c2be5ee7d950f3c3e301 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 3 Apr 2019 16:01:42 +0100 Subject: [PATCH 6/6] Replace Guava Preconditions.checkNotNull with standard Objects.requireNotNull --- src/main/java/org/prefixcommons/CurieUtil.java | 7 +++---- src/test/java/org/prefixcommons/CurieUtilTest.java | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prefixcommons/CurieUtil.java b/src/main/java/org/prefixcommons/CurieUtil.java index 10087c9..7983581 100644 --- a/src/main/java/org/prefixcommons/CurieUtil.java +++ b/src/main/java/org/prefixcommons/CurieUtil.java @@ -1,12 +1,11 @@ package org.prefixcommons; -import static com.google.common.base.Preconditions.checkNotNull; - import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.Optional; import org.prefixcommons.trie.Trie; @@ -17,7 +16,7 @@ public class CurieUtil { - final private Trie trie; + private final Trie trie; private final ImmutableBiMap curieMap; public CurieUtil(Map mapping) { @@ -107,7 +106,7 @@ public Optional getCuriePrefix(String iri) { * @return an {@link Optional} IRI */ public Optional getIri(String curie) { - String[] parts = checkNotNull(curie).split(":"); + String[] parts = Objects.requireNonNull(curie).split(":"); if (parts.length > 1) { String prefix = parts[0]; if (curieMap.containsKey(prefix)) { diff --git a/src/test/java/org/prefixcommons/CurieUtilTest.java b/src/test/java/org/prefixcommons/CurieUtilTest.java index 32d3ed5..3f5bf1c 100644 --- a/src/test/java/org/prefixcommons/CurieUtilTest.java +++ b/src/test/java/org/prefixcommons/CurieUtilTest.java @@ -50,6 +50,11 @@ public void fullIri_whenInputHasNoPrefix() { assertThat(util.getIri(":foo").get(), is("http://x.org/foo")); } + @Test(expected = NullPointerException.class) + public void throwsNullPointerWithNullInput() { + util.getIri(null); + } + @Test public void curie_whenShortMappingIsPresent() { assertThat(util.getCurie("http://x.org/foo"), is(Optional.of(":foo")));