Search in sources :

Example 6 with SimpleNamespace

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;
        }
    };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) AbstractMap(java.util.AbstractMap) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) AbstractMap(java.util.AbstractMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with SimpleNamespace

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);
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Literal(org.eclipse.rdf4j.model.Literal) ArrayList(java.util.ArrayList) Value(org.eclipse.rdf4j.model.Value) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace)

Example 8 with SimpleNamespace

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Test(org.junit.Test)

Example 9 with SimpleNamespace

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));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Test(org.junit.Test)

Example 10 with SimpleNamespace

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));
}
Also used : SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Aggregations

Namespace (org.eclipse.rdf4j.model.Namespace)13 SimpleNamespace (org.eclipse.rdf4j.model.impl.SimpleNamespace)13 LinkedHashSet (java.util.LinkedHashSet)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Literal (org.eclipse.rdf4j.model.Literal)1 Value (org.eclipse.rdf4j.model.Value)1 BindingSet (org.eclipse.rdf4j.query.BindingSet)1 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)1 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)1 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)1