Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor tidy-up #7

Merged
merged 7 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 11 additions & 4 deletions src/main/java/org/prefixcommons/CurieUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +16,7 @@


public class CurieUtil {
final private Trie trie;
private final Trie trie;
private final ImmutableBiMap<String, String> curieMap;

public CurieUtil(Map<String, String> mapping) {
Expand Down Expand Up @@ -92,14 +91,22 @@ public Optional<String> getCurie(String iri) {
}
}

public Optional<String> 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.
*
* @param curie The curie to expand.
* @return an {@link Optional} IRI
*/
public Optional<String> 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)) {
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/org/prefixcommons/trie/Trie.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -25,7 +25,7 @@ public void insert(String word) {

// Traverse through all characters of given word
for (int level = 0; level < length; level++) {
HashMap<Character, TrieNode> child = crawl.getChildren();
Map<Character, TrieNode> child = crawl.getChildren();
char ch = word.charAt(level);

// If there is already a child for current character of given word
Expand All @@ -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<Character, TrieNode> child = crawl.getChildren();
Map<Character, TrieNode> 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();
}
}

}
5 changes: 3 additions & 2 deletions src/main/java/org/prefixcommons/trie/TrieNode.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.prefixcommons.trie;

import java.util.HashMap;
import java.util.Map;

/**
* Represents a Node in a Trie.
*
*/
class TrieNode {
private char value;
private HashMap<Character, TrieNode> children;
private Map<Character, TrieNode> children;
private boolean isLeaf;

public TrieNode(char ch) {
Expand All @@ -17,7 +18,7 @@ public TrieNode(char ch) {
isLeaf = false;
}

public HashMap<Character, TrieNode> getChildren() {
public Map<Character, TrieNode> getChildren() {
return children;
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/prefixcommons/CurieUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down Expand Up @@ -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")));
}
}