Example usage for org.apache.commons.collections15 MultiMap get

List of usage examples for org.apache.commons.collections15 MultiMap get

Introduction

In this page you can find the example usage for org.apache.commons.collections15 MultiMap get.

Prototype

Collection<V> get(Object key);

Source Link

Document

Gets the collection of values associated with the specified key.

Usage

From source file:de.dhke.projects.cutil.collections.MultiMapUtil.java

public static <V> Collection<V> getTransitive(final MultiMap<V, V> multiMap, final V key,
        final Collection<V> targetSet) {
    Collection<V> keyValues = multiMap.get(key);
    if (keyValues != null) {
        for (V keyValue : keyValues) {
            if (!targetSet.contains(keyValue)) {
                targetSet.add(keyValue);
                targetSet.addAll(getTransitive(multiMap, keyValue, targetSet));
            }//from  w w  w . j  av a2s .  c  o m
        }
    }
    return targetSet;
}

From source file:de.dhke.projects.cutil.collections.MultiMapUtil.java

public static <K, V> boolean deepEquals(MultiMap<K, V> m1, MultiMap<? extends K, ? extends V> m2) {
    for (Map.Entry<K, Collection<V>> entry1 : m1.entrySet()) {
        if (!m2.containsKey(entry1.getKey()))
            return false;
        else {//  ww  w  .jav a  2s  .c  o  m
            final Collection<? extends V> values2 = m2.get(entry1.getKey());
            if ((!entry1.getValue().containsAll(values2)) || (!values2.containsAll(entry1.getValue())))
                return false;
        }
    }
    return m1.keySet().containsAll(m2.keySet());
}

From source file:modalLogic.tableau.World.java

/**
 * Update clashes for an incoming literal.
 *
 * @param literal the literal//from w w  w .ja  v a2 s. c o m
 * @param literals the other literals
 */
private void addClashes(LabelledFormula<P> literal, MultiMap<P, LabelledFormula<P>> literals,
        MultiMap<Constant, LabelledFormula<P>> constants) {
    Collection<LabelledFormula<P>> clashing = literals.get(literal.getFormula().getProposition());
    if (clashing == null) {
        clashing = new ArrayList<LabelledFormula<P>>();
    }
    clashing.addAll(constants.values());

    if (!clashing.isEmpty()) {
        for (LabelledFormula<P> c : clashing) {
            clashes.add(literal, c);
        }
    }
}

From source file:org.aksw.resparql.IMyHandler.java

public static String getJenaFormatByQueryString(URI uri) {
    // Check if a particular format was requested via the query string
    // As this excludes some of the content types that may be used
    String query = uri.getQuery();
    MultiMap<String, String> params = URIUtil.getQueryMap(query);

    String rawFormat = getFirst(params.get("format"));
    rawFormat = rawFormat == null ? null : rawFormat.trim().toLowerCase();
    String requestFormat = formatToJenaFormat.get(rawFormat);
    if (rawFormat != null && requestFormat == null) {
        // FIXME Respect the accept header when returning an error
        //return new SimpleResponse(400, "text/plain", "Unsupported format");
        return null;
    }//  ww w.j a v  a  2s .  c o m

    return requestFormat;
}

From source file:org.openanzo.glitter.query.SPARQLAlgebra.java

/**
 * Evaluates and binds the expressions in the given assignments to the corresponding variables.
 * //  w w  w  .ja  va  2  s. c o  m
 * @param answers
 * @param assignments
 * @return SolutionSet with assignments assigned
 * @throws ExpressionEvaluationException
 */
static public SolutionSet processAssignments(SolutionSet answers, MultiMap<Variable, Expression> assignments)
        throws ExpressionEvaluationException {
    SolutionList newAnswers = new SolutionList();
    ListIterator<PatternSolution> it = answers.listIterator();
    while (it.hasNext()) {
        // TODO there are evaluation optimizations here if the LET expressions don't depend on variables in
        // the answers _and_ if the expressions are pure functional (no side effects/outside state involved)
        PatternSolution solution = it.next();
        PatternSolutionImpl newSolution = new PatternSolutionImpl(solution);
        boolean keepSolution = true;
        for (Variable v : assignments.keySet()) {
            for (Expression e : assignments.get(v)) {
                // allow aggregates to operate over the given solutions
                // evaluate in the context of the solution going in, but compare with the new solution
                // we're building up (extending the original solution)
                Value assignedVal = e.evaluate(solution, answers);
                if (assignedVal == null)
                    continue;
                Value currentVal = newSolution.getBinding(v);
                if (currentVal != null && !currentVal.equals(assignedVal)) {
                    // joining this assignment to this solution results fails, so the solution
                    // is removed from the results
                    keepSolution = false;
                    break;
                } else if (currentVal == null) {
                    newSolution.setBinding(v, assignedVal);
                }
            }
            if (!keepSolution)
                break;
        }
        if (keepSolution)
            newAnswers.add(newSolution);
    }
    return newAnswers;
}

From source file:org.openanzo.rdf.MemQuadStore.java

/**
 * Use the predicate/Subject index to find statements
 * //from   ww  w.j  av a 2  s .co  m
 * @param subj
 *            Subject value to match
 * @param pred
 *            predicate value to match
 * @param namedGraphUris
 *            namedGraphURI value to match, or wildcard if null
 * @return Collection<Statement> of matching statements
 */
private Collection<Statement> findPS(Resource subj, URI pred, URI... namedGraphUris) {
    //Lookup predicate/subject index for predicate value provided
    MultiMap<Resource, Statement> psIndexMap = psIndex.get(pred);
    if (psIndexMap != null) {
        //Find subject index for subject provided
        Collection<Statement> map = psIndexMap.get(subj);
        if (map != null && map.size() > 0) {
            //If only 1 namedGraphURI is provided, then get index for that namedGraphURI
            if (namedGraphUris == null || namedGraphUris.length == 0) {
                return org.openanzo.rdf.utils.Collections.copyCollection(map);
            } else if (namedGraphUris.length == 1) {
                Collection<Statement> gSet = namedGraphMap.get(namedGraphUris[0]);
                if (gSet == null) {
                    return Collections.<Statement>emptyList();
                }
                Collection<Statement> matches = new ArrayList<Statement>();
                //Use the smallest set of statements, the ones from the index or set from namedGraphURI index
                if (gSet.size() < map.size()) {
                    for (Statement stmt : gSet) {
                        if (stmt.getPredicate().equals(pred) && stmt.getSubject().equals(subj)) {
                            matches.add(stmt);
                        }
                    }
                } else {
                    for (Statement stmt : map) {
                        if (stmt.getNamedGraphUri().equals(namedGraphUris[0])) {
                            matches.add(stmt);
                        }
                    }
                }
                return matches;
            } else { //If more than one namedGraphURI compare the namedGraphURI of statement from p/s index to set of namedGraphUris
                Collection<Statement> matches = new ArrayList<Statement>();
                HashSet<Resource> namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                Collections.addAll(namedGraphUriset, namedGraphUris);
                for (Statement stmt : map) {
                    if (namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                        matches.add(stmt);
                    }
                }
                return matches;
            }
        }
    }
    return Collections.<Statement>emptyList();
}

From source file:org.openanzo.rdf.MemQuadStore.java

/**
 * Use the predicate/Object index to find statements
 * //from   w w w .ja va 2s  .  co  m
 * @param pred
 *            predicate value to match
 * @param obj
 *            Object value to match
 * @param namedGraphUris
 *            namedGraphURI value to match, or wildcard if null
 * @return Collection<Statement> of matching statements
 */
private Collection<Statement> findPO(URI pred, Value obj, Resource... namedGraphUris) {
    MultiMap<Value, Statement> poIndexMap = poIndex.get(pred);
    if (poIndexMap != null) {
        Collection<Statement> map = poIndexMap.get(obj);
        if (map != null && map.size() > 0) {
            if (namedGraphUris == null || namedGraphUris.length == 0) {
                return org.openanzo.rdf.utils.Collections.copyCollection(map);
            } else if (namedGraphUris.length == 1) {
                Collection<Statement> matches = new ArrayList<Statement>();
                Collection<Statement> gSet = namedGraphMap.get(namedGraphUris[0]);
                if (gSet == null) {
                    return Collections.<Statement>emptyList();
                }
                if (gSet.size() < map.size()) {
                    for (Statement stmt : gSet) {
                        if (stmt.getPredicate().equals(pred) && stmt.getObject().equals(obj)) {
                            matches.add(stmt);
                        }
                    }
                } else {
                    for (Statement stmt : map) {
                        if (stmt.getNamedGraphUri().equals(namedGraphUris[0])) {
                            matches.add(stmt);
                        }
                    }
                }
                return matches;
            } else {
                Collection<Statement> matches = new ArrayList<Statement>();
                HashSet<Resource> namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                Collections.addAll(namedGraphUriset, namedGraphUris);
                for (Statement stmt : map) {
                    if (namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                        matches.add(stmt);
                    }
                }
                return matches;
            }
        }
    }
    return Collections.<Statement>emptyList();
}

From source file:org.openanzo.rdf.MemQuadStore.java

/**
 * Tests if a statement is contained in store that match provided parameters
 * //from w  w w  . ja  v  a2s  .co m
 * @param subj
 *            Subject value to match, or wildcard if null
 * @param pred
 *            predicate value to match, or wildcard if null
 * @param obj
 *            Object value to match, or wildcard if null
 * @param namedGraphUris
 *            namedGraphURI value to match, or wildcard if null
 * @return boolean result to indicate if the statement was contained in store
 */
@SuppressWarnings({ "unchecked" })
public boolean contains(Resource subj, URI pred, Value obj, URI... namedGraphUris) {
    if ((subj == null && pred == null && obj == null && namedGraphUris == null) || statements.size() == 0) {
        return (statements.size() > 0);
    }
    synchronized (this.statements) {
        if (subj != null && pred != null && obj != null && namedGraphUris != null
                && namedGraphUris.length == 1) {
            Statement stmt = new Statement(subj, pred, obj, namedGraphUris[0]);
            return statements.contains(stmt);
        }
        if (subj == null && pred != null && obj != null) {
            MultiMap<Value, Statement> poIndexMap = poIndex.get(pred);
            if (poIndexMap != null) {
                Collection<Statement> map = poIndexMap.get(obj);
                if (map != null && map.size() > 0) {
                    if (namedGraphUris != null && namedGraphUris.length > 0) {
                        Collection<Statement> gSet = null;
                        HashSet<Resource> namedGraphUriset = null;
                        if (namedGraphUris.length == 1) {
                            gSet = namedGraphMap.get(namedGraphUris[0]);
                        } else {
                            namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                            Collections.addAll(namedGraphUriset, namedGraphUris);
                        }
                        if (gSet != null) {
                            Collection<Statement>[] set = new Collection[2];
                            set[0] = (gSet.size() < map.size()) ? gSet : map;
                            set[1] = (gSet.size() < map.size()) ? map : gSet;
                            for (Statement stmt : set[0]) {
                                if (set[1].contains(stmt)) {
                                    return true;
                                }
                            }
                        } else if (namedGraphUriset != null) {
                            for (Statement stmt : map) {
                                if (namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                                    return true;
                                }
                            }
                        }
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            }
        }
        if (subj != null && pred != null && obj == null) {
            MultiMap<Resource, Statement> psIndexMap = psIndex.get(pred);
            if (psIndexMap != null) {
                Collection<Statement> map = psIndexMap.get(subj);
                if (map != null && map.size() > 0) {
                    if (namedGraphUris != null && namedGraphUris.length > 0) {
                        Collection<Statement> gSet = null;
                        HashSet<Resource> namedGraphUriset = null;
                        if (namedGraphUris.length == 1) {
                            gSet = namedGraphMap.get(namedGraphUris[0]);
                        } else {
                            namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                            Collections.addAll(namedGraphUriset, namedGraphUris);
                        }
                        if (gSet != null) {
                            Collection<Statement>[] set = new Collection[2];
                            set[0] = (gSet.size() < map.size()) ? gSet : map;
                            set[1] = (gSet.size() < map.size()) ? map : gSet;
                            for (Statement stmt : set[0]) {
                                if (set[1].contains(stmt)) {
                                    return true;
                                }
                            }
                        } else if (namedGraphUriset != null) {
                            for (Statement stmt : map) {
                                if (namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                                    return true;
                                }
                            }
                        }
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            }
        }
        Collection<Statement> sSet = (subj != null) ? subjectMap.get(subj) : statements;
        Collection<Statement> pSet = (pred != null) ? predMap.get(pred) : statements;
        Collection<Statement> oSet = (obj != null) ? objMap.get(obj) : statements;
        Collection<Statement> gSet = null;
        if (namedGraphUris != null) {
            if (namedGraphUris.length == 1) {
                gSet = namedGraphMap.get(namedGraphUris[0]);
                if (gSet == null) {
                    return false;
                }
            }
        } else {
            gSet = statements;
        }
        if (sSet != null && pSet != null && oSet != null && gSet != null) {
            Collection<Statement>[] set = new Collection[4];
            set[0] = sSet;
            set[1] = pSet;
            set[2] = oSet;
            set[3] = gSet;
            Arrays.sort(set, comparator);
            if (set[0].size() == statements.size()) {
                return true;
            } else {
                for (Statement stmt : set[0]) {
                    if ((set[1] == statements || set[1].contains(stmt))
                            && (set[2] == statements || set[2].contains(stmt))
                            && (set[3] == statements || set[3].contains(stmt))) {
                        return true;
                    }
                }
            }
        } else if (sSet != null && pSet != null && oSet != null && gSet == null) {
            if (namedGraphUris != null) {
                HashSet<Resource> namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                Collections.addAll(namedGraphUriset, namedGraphUris);
                Collection<Statement>[] set = new Collection[3];
                set[0] = sSet;
                set[1] = pSet;
                set[2] = oSet;
                Arrays.sort(set, comparator);
                if (set[0].size() == statements.size()) {
                    return true;
                } else {
                    for (Statement stmt : set[0]) {
                        if ((set[1] == statements || set[1].contains(stmt))
                                && (set[2] == statements || set[2].contains(stmt))
                                && namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

From source file:org.openanzo.rdf.MemQuadStore.java

/**
 * Tests if a statement is contained in store that match provided parameters
 * //from w ww. j  av a  2 s.co m
 * @param subj
 *            Subject value to match, or wildcard if null
 * @param pred
 *            predicate value to match, or wildcard if null
 * @param obj
 *            Object value to match, or wildcard if null
 * @param namedGraphUris
 *            namedGraphURI value to match, or wildcard if null
 * @return boolean result to indicate if the statement was contained in store
 */
@SuppressWarnings({ "unchecked" })
public long count(Resource subj, URI pred, Value obj, URI... namedGraphUris) {
    if ((subj == null && pred == null && obj == null && namedGraphUris == null) || statements.size() == 0) {
        return statements.size();
    }
    synchronized (this.statements) {
        if (subj != null && pred != null && obj != null && namedGraphUris != null
                && namedGraphUris.length == 1) {
            Statement stmt = new Statement(subj, pred, obj, namedGraphUris[0]);
            return statements.contains(stmt) ? 1 : 0;
        }
        long count = 0;
        if (subj == null && pred != null && obj != null) {
            MultiMap<Value, Statement> poIndexMap = poIndex.get(pred);
            if (poIndexMap != null) {
                Collection<Statement> map = poIndexMap.get(obj);
                if (map != null && map.size() > 0) {
                    if (namedGraphUris != null && namedGraphUris.length > 0) {
                        Collection<Statement> gSet = null;
                        HashSet<Resource> namedGraphUriset = null;
                        if (namedGraphUris.length == 1) {
                            gSet = namedGraphMap.get(namedGraphUris[0]);
                        } else {
                            namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                            Collections.addAll(namedGraphUriset, namedGraphUris);
                        }
                        if (gSet != null) {
                            Collection<Statement>[] set = new Collection[2];
                            set[0] = (gSet.size() < map.size()) ? gSet : map;
                            set[1] = (gSet.size() < map.size()) ? map : gSet;
                            for (Statement stmt : set[0]) {
                                if (set[1].contains(stmt)) {
                                    count++;
                                }
                            }
                        } else if (namedGraphUriset != null) {
                            for (Statement stmt : map) {
                                if (namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                                    count++;
                                }
                            }
                        }
                    } else {
                        return map.size();
                    }
                } else {
                    return 0;
                }
            }
        }
        if (subj != null && pred != null && obj == null) {
            MultiMap<Resource, Statement> psIndexMap = psIndex.get(pred);
            if (psIndexMap != null) {
                Collection<Statement> map = psIndexMap.get(subj);
                if (map != null && map.size() > 0) {
                    if (namedGraphUris != null && namedGraphUris.length > 0) {
                        Collection<Statement> gSet = null;
                        HashSet<Resource> namedGraphUriset = null;
                        if (namedGraphUris.length == 1) {
                            gSet = namedGraphMap.get(namedGraphUris[0]);
                        } else {
                            namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                            Collections.addAll(namedGraphUriset, namedGraphUris);
                        }
                        if (gSet != null) {
                            Collection<Statement>[] set = new Collection[2];
                            set[0] = (gSet.size() < map.size()) ? gSet : map;
                            set[1] = (gSet.size() < map.size()) ? map : gSet;
                            for (Statement stmt : set[0]) {
                                if (set[1].contains(stmt)) {
                                    return count++;
                                }
                            }
                        } else if (namedGraphUriset != null) {
                            for (Statement stmt : map) {
                                if (namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                                    return count++;
                                }
                            }
                        }
                    } else {
                        return map.size();
                    }
                } else {
                    return 0;
                }
            }
        }
        Collection<Statement> sSet = (subj != null) ? subjectMap.get(subj) : statements;
        Collection<Statement> pSet = (pred != null) ? predMap.get(pred) : statements;
        Collection<Statement> oSet = (obj != null) ? objMap.get(obj) : statements;
        Collection<Statement> gSet = null;
        if (namedGraphUris != null) {
            if (namedGraphUris.length == 1) {
                gSet = namedGraphMap.get(namedGraphUris[0]);
                if (gSet == null) {
                    return 0;
                }
            }
        } else {
            gSet = statements;
        }
        if (sSet != null && pSet != null && oSet != null && gSet != null) {
            Collection<Statement>[] set = new Collection[4];
            set[0] = sSet;
            set[1] = pSet;
            set[2] = oSet;
            set[3] = gSet;
            Arrays.sort(set, comparator);
            if (set[0].size() == statements.size()) {
                return statements.size();
            } else {
                for (Statement stmt : set[0]) {
                    if ((set[1] == statements || set[1].contains(stmt))
                            && (set[2] == statements || set[2].contains(stmt))
                            && (set[3] == statements || set[3].contains(stmt))) {
                        return count++;
                    }
                }
            }
        } else if (sSet != null && pSet != null && oSet != null && gSet == null) {
            if (namedGraphUris != null) {
                HashSet<Resource> namedGraphUriset = new HashSet<Resource>(namedGraphUris.length);
                Collections.addAll(namedGraphUriset, namedGraphUris);
                Collection<Statement>[] set = new Collection[3];
                set[0] = sSet;
                set[1] = pSet;
                set[2] = oSet;
                Arrays.sort(set, comparator);
                if (set[0].size() == statements.size()) {
                    return statements.size();
                } else {
                    for (Statement stmt : set[0]) {
                        if ((set[1] == statements || set[1].contains(stmt))
                                && (set[2] == statements || set[2].contains(stmt))
                                && namedGraphUriset.contains(stmt.getNamedGraphUri())) {
                            return count++;
                        }
                    }
                }
            }
        }
        return count;
    }

}