-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create specific class to manage a map of URI prefixes
- Loading branch information
Julian Mendez
committed
Jun 10, 2017
1 parent
7b276fb
commit 26b3af6
Showing
10 changed files
with
217 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
tabula-core/src/main/java/de/tudresden/inf/lat/tabula/table/PrefixMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package de.tudresden.inf.lat.tabula.table; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This models a map of URI prefixes. | ||
*/ | ||
public interface PrefixMap { | ||
|
||
/** | ||
* Start of the prefix. | ||
*/ | ||
String PREFIX_AMPERSAND = "&"; | ||
|
||
/** | ||
* End of the prefix. | ||
*/ | ||
String PREFIX_SEMICOLON = ";"; | ||
|
||
/** | ||
* Returns the expansion for the given prefix. | ||
* | ||
* @param key | ||
* the prefix | ||
* @return expansion for the given prefix | ||
*/ | ||
Optional<URI> get(URI key); | ||
|
||
/** | ||
* Assigns a prefix to an expansion. | ||
* | ||
* @param key | ||
* prefix | ||
* @param value | ||
* expansion | ||
* @return an optional containing the previous value of the given key, or | ||
* empty if there was no value for that key | ||
*/ | ||
Optional<URI> put(URI key, URI value); | ||
|
||
/** | ||
* Returns a URI with the prefix, i.e. a shortened URI. | ||
* | ||
* @param uri | ||
* URI | ||
* @return a URI with the prefix | ||
*/ | ||
URI getWithPrefix(URI uri); | ||
|
||
/** | ||
* Returns a URI without the prefix, i.e. with the prefix already expanded. | ||
* | ||
* @param uri | ||
* URI | ||
* @return a URI without the prefix | ||
*/ | ||
URI getWithoutPrefix(URI uri); | ||
|
||
/** | ||
* Returns an optional with the prefix for the given URI, or empty if there | ||
* is none. | ||
* | ||
* @param uri | ||
* URI | ||
* @return an optional with the prefix for the given URI, or empty if there | ||
* is none | ||
*/ | ||
Optional<URI> getPrefixFor(URI uri); | ||
|
||
/** | ||
* Returns a stream to iterate on the keys. | ||
* | ||
* @return a stream to iterate on the keys | ||
*/ | ||
Stream<URI> getKeysAsStream(); | ||
|
||
/** | ||
* Clears the content. | ||
*/ | ||
void clear(); | ||
|
||
/** | ||
* Returns the size of this prefix map. | ||
* | ||
* @return the size of this prefix map | ||
*/ | ||
int size(); | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
tabula-core/src/main/java/de/tudresden/inf/lat/tabula/table/PrefixMapImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package de.tudresden.inf.lat.tabula.table; | ||
|
||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.TreeMap; | ||
import java.util.stream.Stream; | ||
|
||
import de.tudresden.inf.lat.tabula.common.OptMap; | ||
import de.tudresden.inf.lat.tabula.common.OptMapImpl; | ||
|
||
/** | ||
* An object of this class is a map of URI prefixes. This implementation | ||
* iterates on the keys keeping the order in which they were added for the first | ||
* time. | ||
* | ||
*/ | ||
public class PrefixMapImpl implements PrefixMap { | ||
|
||
private OptMap<URI, URI> prefixMap = new OptMapImpl<URI, URI>(new TreeMap<>()); | ||
private List<URI> keyList = new ArrayList<URI>(); | ||
|
||
public Optional<URI> get(URI key) { | ||
return this.prefixMap.get(key); | ||
} | ||
|
||
public Optional<URI> put(URI key, URI value) { | ||
if (!this.prefixMap.containsKey(key)) { | ||
this.keyList.add(key); | ||
} | ||
return this.prefixMap.put(key, value); | ||
} | ||
|
||
@Override | ||
public Optional<URI> getPrefixFor(URI uri) { | ||
String uriStr = uri.toASCIIString(); | ||
Optional<URI> key = prefixMap.keySet().stream() | ||
.filter((URI e) -> uriStr.startsWith(prefixMap.get(e).get().toASCIIString())).findFirst(); | ||
if (key.isPresent()) { | ||
return Optional.of(key.get()); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public URI getWithoutPrefix(URI uri) { | ||
URI ret = uri; | ||
String uriStr = uri.toASCIIString(); | ||
if (uriStr.startsWith(PREFIX_AMPERSAND)) { | ||
int pos = uriStr.indexOf(PREFIX_SEMICOLON, PREFIX_AMPERSAND.length()); | ||
if (pos != -1) { | ||
URI prefix = URI.create(uriStr.substring(PREFIX_AMPERSAND.length(), pos)); | ||
Optional<URI> optExpansion = this.prefixMap.get(prefix); | ||
if (optExpansion.isPresent()) { | ||
ret = URI.create( | ||
optExpansion.get().toASCIIString() + uriStr.substring(pos + PREFIX_SEMICOLON.length())); | ||
} | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
@Override | ||
public URI getWithPrefix(URI uri) { | ||
URI ret = uri; | ||
Optional<URI> optPrefix = getPrefixFor(uri); | ||
if (optPrefix.isPresent()) { | ||
String uriStr = uri.toASCIIString(); | ||
URI key = optPrefix.get(); | ||
String keyStr = key.toASCIIString(); | ||
Optional<URI> optExpansion = get(key); | ||
String expansionStr = optExpansion.get().toASCIIString(); | ||
if (keyStr.isEmpty()) { | ||
ret = URI.create(uriStr.substring(expansionStr.length())); | ||
} else { | ||
ret = URI.create(PREFIX_AMPERSAND + keyStr + PREFIX_SEMICOLON + uriStr.substring(expansionStr.length())); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
@Override | ||
public Stream<URI> getKeysAsStream() { | ||
return this.keyList.stream(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
this.prefixMap.clear(); | ||
this.keyList.clear(); | ||
} | ||
|
||
public int size() { | ||
return this.prefixMap.size(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.