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 diff --git a/src/main/java/org/prefixcommons/CurieUtil.java b/src/main/java/org/prefixcommons/CurieUtil.java index 9f616cf..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) { @@ -92,6 +91,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. * @@ -99,7 +106,7 @@ public Optional getCurie(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/main/java/org/prefixcommons/trie/Trie.java b/src/main/java/org/prefixcommons/trie/Trie.java index 399b0b3..5d88d0a 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 @@ -48,41 +48,43 @@ 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; // 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); // 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)) { - 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 - if (crawl.isLeaf()) + if (crawl.isLeaf()) { prevMatch = level + 1; - } else + } + } else { break; + } } // 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(); + } } } 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; } diff --git a/src/test/java/org/prefixcommons/CurieUtilTest.java b/src/test/java/org/prefixcommons/CurieUtilTest.java index 9ad0575..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"))); @@ -106,4 +111,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"))); + } }