use of org.eclipse.rdf4j.model.impl.SimpleNamespace in project rdf4j by eclipse.
the class Namespaces method wrap.
/**
* Wraps the given {@link Set} of {@link Namespace}s as a {@link Map} of prefix to URI mappings, so that
* it can be used where a {@link Map} is required by the API. <br>
* NOTE: The Map returned by this method is not synchronized.
*
* @param namespaces
* The Set to wrap.
* @return A Map of prefix to URI mappings which is backed by the given Set of {@link Namespace}s.
*/
public static Map<String, String> wrap(final Set<Namespace> namespaces) {
return new Map<String, String>() {
@Override
public void clear() {
namespaces.clear();
}
@Override
public boolean containsKey(Object nextKey) {
if (nextKey instanceof String) {
for (Namespace nextNamespace : namespaces) {
if (nextNamespace.getPrefix().equals(nextKey)) {
return true;
}
}
}
return false;
}
@Override
public boolean containsValue(Object nextValue) {
if (nextValue instanceof String) {
for (Namespace nextNamespace : namespaces) {
if (nextNamespace.getName().equals(nextValue)) {
return true;
}
}
}
return false;
}
/**
* NOTE: This entry set is immutable, and does not update the internal set through its iterator.
*/
@Override
public Set<java.util.Map.Entry<String, String>> entrySet() {
Set<java.util.Map.Entry<String, String>> result = new LinkedHashSet<Map.Entry<String, String>>();
for (Namespace nextNamespace : namespaces) {
AbstractMap.SimpleImmutableEntry<String, String> nextEntry = new SimpleImmutableEntry<String, String>(nextNamespace.getPrefix(), nextNamespace.getName());
result.add(nextEntry);
}
return Collections.unmodifiableSet(result);
}
@Override
public String get(Object nextKey) {
if (nextKey instanceof String) {
for (Namespace nextNamespace : namespaces) {
if (nextNamespace.getPrefix().equals(nextKey)) {
return nextNamespace.getName();
}
}
}
return null;
}
@Override
public boolean isEmpty() {
return namespaces.isEmpty();
}
@Override
public Set<String> keySet() {
Set<String> result = new LinkedHashSet<String>();
for (Namespace nextNamespace : namespaces) {
result.add(nextNamespace.getPrefix());
}
return result;
}
@Override
public String put(String nextKey, String nextValue) {
String result = null;
for (Namespace nextNamespace : new LinkedHashSet<Namespace>(namespaces)) {
if (nextNamespace.getPrefix().equals(nextKey)) {
result = nextNamespace.getName();
namespaces.remove(nextNamespace);
}
}
namespaces.add(new SimpleNamespace(nextKey, nextValue));
return result;
}
@Override
public void putAll(Map<? extends String, ? extends String> nextSet) {
for (Map.Entry<? extends String, ? extends String> nextEntry : nextSet.entrySet()) {
put(nextEntry.getKey(), nextEntry.getValue());
}
}
@Override
public String remove(Object nextKey) {
String result = null;
for (Namespace nextNamespace : new LinkedHashSet<Namespace>(namespaces)) {
if (nextNamespace.getPrefix().equals(nextKey)) {
result = nextNamespace.getName();
namespaces.remove(nextNamespace);
}
}
return result;
}
@Override
public int size() {
return namespaces.size();
}
@Override
public Collection<String> values() {
List<String> result = new ArrayList<String>();
for (Namespace nextNamespace : namespaces) {
result.add(nextNamespace.getName());
}
return result;
}
};
}
use of org.eclipse.rdf4j.model.impl.SimpleNamespace in project rdf4j by eclipse.
the class HTTPRepositoryConnection method getNamespaces.
public RepositoryResult<Namespace> getNamespaces() throws RepositoryException {
try {
List<Namespace> namespaceList = new ArrayList<Namespace>();
TupleQueryResult namespaces = client.getNamespaces();
try {
while (namespaces.hasNext()) {
BindingSet bindingSet = namespaces.next();
Value prefix = bindingSet.getValue("prefix");
Value namespace = bindingSet.getValue("namespace");
if (prefix instanceof Literal && namespace instanceof Literal) {
String prefixStr = ((Literal) prefix).getLabel();
String namespaceStr = ((Literal) namespace).getLabel();
namespaceList.add(new SimpleNamespace(prefixStr, namespaceStr));
}
}
} finally {
namespaces.close();
}
return createRepositoryResult(namespaceList);
} catch (QueryEvaluationException e) {
throw new RepositoryException(e);
} catch (IOException e) {
throw new RepositoryException(e);
}
}
use of org.eclipse.rdf4j.model.impl.SimpleNamespace in project rdf4j by eclipse.
the class NamespacesTest method testWrapKeySet.
/**
* Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
*/
@Test
public final void testWrapKeySet() throws Exception {
Set<Namespace> testSet = new LinkedHashSet<Namespace>();
Map<String, String> testMap = Namespaces.wrap(testSet);
Set<String> keySet1 = testMap.keySet();
assertNotNull(keySet1);
assertTrue(keySet1.isEmpty());
testSet.add(new SimpleNamespace(testPrefix1, testName1));
Set<String> keySet2 = testMap.keySet();
assertNotNull(keySet2);
assertFalse(keySet2.isEmpty());
assertEquals(1, keySet2.size());
String nextKey = keySet2.iterator().next();
assertEquals(testPrefix1, nextKey);
testSet.clear();
Set<String> keySet3 = testMap.keySet();
assertNotNull(keySet3);
assertTrue(keySet3.isEmpty());
}
use of org.eclipse.rdf4j.model.impl.SimpleNamespace in project rdf4j by eclipse.
the class NamespacesTest method testWrapRemove.
/**
* Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
*/
@Test
public final void testWrapRemove() throws Exception {
Set<Namespace> testSet = new LinkedHashSet<Namespace>();
Map<String, String> testMap = Namespaces.wrap(testSet);
assertTrue(testMap.isEmpty());
assertEquals(0, testMap.size());
assertTrue(testSet.isEmpty());
assertEquals(0, testSet.size());
assertFalse(testMap.containsKey(testPrefix1));
assertFalse(testMap.containsValue(testName1));
// Directly add to Set, and then try to remove it using the Map
testSet.add(new SimpleNamespace(testPrefix1, testName1));
assertFalse(testMap.isEmpty());
assertEquals(1, testMap.size());
assertFalse(testSet.isEmpty());
assertEquals(1, testSet.size());
assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1)));
assertTrue(testMap.containsKey(testPrefix1));
assertTrue(testMap.containsValue(testName1));
testSet.remove(new SimpleNamespace(testPrefix1, testName1));
assertTrue(testMap.isEmpty());
assertEquals(0, testMap.size());
assertTrue(testSet.isEmpty());
assertEquals(0, testSet.size());
assertFalse(testMap.containsKey(testPrefix1));
assertFalse(testMap.containsValue(testName1));
testSet.clear();
// Try again after clear
testSet.add(new SimpleNamespace(testPrefix1, testName1));
assertFalse(testMap.isEmpty());
assertEquals(1, testMap.size());
assertFalse(testSet.isEmpty());
assertEquals(1, testSet.size());
assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1)));
assertTrue(testMap.containsKey(testPrefix1));
assertTrue(testMap.containsValue(testName1));
testSet.remove(new SimpleNamespace(testPrefix1, testName1));
assertTrue(testMap.isEmpty());
assertEquals(0, testMap.size());
assertTrue(testSet.isEmpty());
assertEquals(0, testSet.size());
assertFalse(testMap.containsKey(testPrefix1));
assertFalse(testMap.containsValue(testName1));
}
use of org.eclipse.rdf4j.model.impl.SimpleNamespace in project rdf4j by eclipse.
the class NamespacesTest method testAsMapOne.
/**
* Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#asMap(java.util.Set)}.
*/
@Test
public final void testAsMapOne() {
Set<Namespace> input = new HashSet<Namespace>();
input.add(new SimpleNamespace(RDF.PREFIX, RDF.NAMESPACE));
Map<String, String> map = Namespaces.asMap(input);
assertFalse(map.isEmpty());
assertEquals(1, map.size());
assertTrue(map.containsKey(RDF.PREFIX));
assertEquals(RDF.NAMESPACE, map.get(RDF.PREFIX));
}
Aggregations