Example usage for java.util Set retainAll

List of usage examples for java.util Set retainAll

Introduction

In this page you can find the example usage for java.util Set retainAll.

Prototype

boolean retainAll(Collection<?> c);

Source Link

Document

Retains only the elements in this set that are contained in the specified collection (optional operation).

Usage

From source file:org.apache.lucene.store.FSDirectory.java

@Override
public void sync(Collection<String> names) throws IOException {
    ensureOpen();/*w  w  w .j a  v a  2s .  c  o m*/
    Set<String> toSync = new HashSet<String>(names);
    toSync.retainAll(staleFiles);

    for (String name : toSync)
        fsync(name);

    staleFiles.removeAll(toSync);
}

From source file:io.anserini.rerank.lib.AxiomReranker.java

private double computeMutualInformation(Set<Integer> docidsX, Set<Integer> docidsY, int totalDocCount) {
    int x1 = docidsX.size(), y1 = docidsY.size(); //document that x occurres
    int x0 = totalDocCount - x1, y0 = totalDocCount - y1; //document num that x doesn't occurres

    if (x1 == 0 || x0 == 0 || y1 == 0 || y0 == 0) {
        return 0;
    }//from  w  ww  .j a  va  2  s .  c  o  m

    float pX0 = 1.0f * x0 / totalDocCount;
    float pX1 = 1.0f * x1 / totalDocCount;
    float pY0 = 1.0f * y0 / totalDocCount;
    float pY1 = 1.0f * y1 / totalDocCount;

    //get the intersection of docIds
    Set<Integer> docidsXClone = new HashSet<>(docidsX); // directly operate on docidsX will change it permanently
    docidsXClone.retainAll(docidsY);
    int numXY11 = docidsXClone.size();
    int numXY10 = numXY10 = x1 - numXY11; //doc num that x occurs but y doesn't
    int numXY01 = y1 - numXY11; // doc num that y occurs but x doesn't
    int numXY00 = totalDocCount - numXY11 - numXY10 - numXY01; //doc num that neither x nor y occurs

    float pXY11 = 1.0f * numXY11 / totalDocCount;
    float pXY10 = 1.0f * numXY10 / totalDocCount;
    float pXY01 = 1.0f * numXY01 / totalDocCount;
    float pXY00 = 1.0f * numXY00 / totalDocCount;

    double m00 = 0, m01 = 0, m10 = 0, m11 = 0;
    if (pXY00 != 0)
        m00 = pXY00 * Math.log(pXY00 / (pX0 * pY0));
    if (pXY01 != 0)
        m01 = pXY01 * Math.log(pXY01 / (pX0 * pY1));
    if (pXY10 != 0)
        m10 = pXY10 * Math.log(pXY10 / (pX1 * pY0));
    if (pXY11 != 0)
        m11 = pXY11 * Math.log(pXY11 / (pX1 * pY1));
    return m00 + m10 + m01 + m11;
}

From source file:com.vmware.identity.openidconnect.server.UserInfoRetriever.java

private Set<String> computeGroupMembershipFiltered(List<String> groupMembership,
        Set<ResourceServerInfo> resourceServerInfos) {
    // 1. result = {union of all filters}
    Set<String> result = new HashSet<String>();
    for (ResourceServerInfo rsInfo : resourceServerInfos) {
        assert !rsInfo.getGroupFilter().isEmpty();
        Set<String> groupFilterLowerCase = toLowerCase(rsInfo.getGroupFilter());
        result.addAll(groupFilterLowerCase);
    }//from www  .  j  ava2  s .c o  m

    // 2. result = intersection of {union of all filters} with groupMembership
    Set<String> groupMembershipLowerCase = toLowerCase(groupMembership);
    result.retainAll(groupMembershipLowerCase);
    return result;
}

From source file:org.phenotips.data.similarity.internal.DefaultPatientSimilarityView.java

/**
 * Get the phenotypic similarity score for this patient match.
 *
 * @return the similarity score, between 0 (a poor match) and 1 (a good match)
 *//*from w w w.  j  a v a2  s  .  c om*/
public double getPhenotypeScore() {
    if (this.match == null || this.reference == null) {
        return 0.0;
    } else {
        // Get ancestors for both patients
        Set<VocabularyTerm> refAncestors = getAncestors(getPresentPatientTerms(this.reference));
        Set<VocabularyTerm> matchAncestors = getAncestors(getPresentPatientTerms(this.match));

        if (refAncestors.isEmpty() || matchAncestors.isEmpty()) {
            return 0.0;
        } else {
            // Score overlapping ancestors
            Set<VocabularyTerm> commonAncestors = new HashSet<VocabularyTerm>();
            commonAncestors.addAll(refAncestors);
            commonAncestors.retainAll(matchAncestors);

            Set<VocabularyTerm> allAncestors = new HashSet<VocabularyTerm>();
            allAncestors.addAll(refAncestors);
            allAncestors.addAll(matchAncestors);

            return getTermICs(commonAncestors) / getTermICs(allAncestors);
        }
    }
}

From source file:org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.EnforceVariablesVisitor.java

private ILogicalOperator visitsInputs(ILogicalOperator op, Collection<LogicalVariable> varsToRecover)
        throws AlgebricksException {
    if (op.getInputs().size() == 0 || varsToRecover.isEmpty()) {
        return op;
    }//w w w.  j  ava  2 s  .  c  o m
    Set<LogicalVariable> producedVars = new HashSet<>();
    VariableUtilities.getProducedVariables(op, producedVars);
    varsToRecover.removeAll(producedVars);
    if (!varsToRecover.isEmpty()) {
        if (op.getInputs().size() == 1) {
            // Deals with single input operators.
            ILogicalOperator newOp = op.getInputs().get(0).getValue().accept(this, varsToRecover);
            op.getInputs().get(0).setValue(newOp);
        } else {
            // Deals with multi-input operators.
            for (Mutable<ILogicalOperator> childRef : op.getInputs()) {
                ILogicalOperator child = childRef.getValue();
                Set<LogicalVariable> varsToRecoverInChild = new HashSet<>();
                VariableUtilities.getProducedVariablesInDescendantsAndSelf(child, varsToRecoverInChild);
                // Obtains the variables that this particular child should propagate.
                varsToRecoverInChild.retainAll(varsToRecover);
                ILogicalOperator newChild = child.accept(this, varsToRecoverInChild);
                childRef.setValue(newChild);
            }
        }
    }
    return op;
}

From source file:ubic.BAMSandAllen.MatrixPairs.ConnectivityAndAllenDataPair.java

/**
 * removes all or retains only the connections to and from the Bed nuclei of the stria terminalis
 * /*w  w w. jav  a2  s  . co m*/
 * @param inverse true to retain the connections, false to remove
 * @param leaveCols true to leave the columns of the matrix untouched
 */
public void removeBedNucleiStria(boolean inverse, boolean leaveCols) {
    BAMSDataLoader BAMSData = new BAMSDataLoader();
    boolean indirect = true;
    Set<String> bedNuclei = BAMSData.getChildren("Bed nuclei of the stria terminalis", indirect);
    bedNuclei.add("Bed nuclei of the stria terminalis");

    log.info("Bed nuclei:" + bedNuclei);
    log.info("Bed nuclei terms:" + bedNuclei.size());

    List<String> connectionRows = new LinkedList<String>(matrixA.getRowNames());
    if (inverse) {
        connectionRows.removeAll(bedNuclei);
    } else {
        connectionRows.retainAll(bedNuclei);
    }
    log.info("Bed nuclei rows removed:" + connectionRows.size());
    matrixA = matrixA.removeRows(connectionRows);

    if (!leaveCols) {
        Set<String> connectionCols = new HashSet<String>(matrixA.getColNames());
        if (inverse) {
            // do nothing
        } else {
            connectionCols.retainAll(bedNuclei);
            log.info("Bed nuclei cols removed:" + connectionCols.size());
            matrixA = matrixA.removeColumns(connectionCols);
            if (isInSameSpace) {
                // remove from matrix B too
                log.info("MatrixB before:" + matrixB.columns());
                matrixB = matrixB.removeColumns(connectionCols);
                log.info("MatrixB after:" + matrixB.columns());
            } else {
                // if it's not in the same space then it will fail to map it to a BAMS region, and will be removed
            }
        }
    }
}

From source file:au.edu.ausstage.networks.LookupManager.java

@SuppressWarnings("unchecked")
public String getCollaboration(int id1, int id2) {

    Set<Integer> evtSet_1 = new HashSet<Integer>();
    Set<Integer> evtSet_2 = new HashSet<Integer>();

    evtSet_1 = getAssociatedEvents(id1);
    evtSet_2 = getAssociatedEvents(id2);

    Event evt = null;/*from   w  w w  .  j  a  va2 s .  c om*/
    JSONArray evt_jsonArr = new JSONArray();
    //first_date comparator used to sort Event nodes
    EvtComparator evtComp = new EvtComparator();

    if (evtSet_1 != null && evtSet_2 != null) {
        Set<Integer> intersection = new HashSet<Integer>(evtSet_1);
        intersection.retainAll(evtSet_2);

        if (intersection != null) {
            List<Event> evtList = db.selectBatchingEventDetails(intersection);
            // Sorting Event List on the basis of Event first Date by passing Comparator
            Collections.sort(evtList, evtComp);

            if (evtList != null)
                for (int i = 0; i < evtList.size(); i++) {
                    evt = evtList.get(i);
                    evt_jsonArr.add(evt.toJSONObj(i));
                }
        }
    }

    return evt_jsonArr.toString();
}

From source file:ubic.BAMSandAllen.MatrixPairs.ConnectivityAndAllenDataPair.java

/**
 * Converts allen name to connectivity region name
 *//*from   w ww  .jav  a 2s  .c o  m*/
public Set<String> convertBNametoA(String allenRegion, boolean useVirtual) {
    if (!allenCatalog.getLeafs().contains(allenRegion))
        return null;

    // the region has been modified to create a virtual region
    if (useVirtual && virtualRegions.contains(allenRegion)) {
        Set<String> result = new HashSet<String>();
        result.add(allenRegion);
        return result;
    }
    Set<String> result = allenCatalog.getBAMSMappedRegions(allenRegion);
    result.retainAll(matrixA.getColNames());
    return result;
}

From source file:org.chromium.chrome.browser.physicalweb.UrlManager.java

/**
 * Get the list of URLs which are both nearby and resolved through PWS.
 * @param allowUnresolved If true, include unresolved URLs only if the
 * resolved URL list is empty.// w  w w  . ja v  a  2s. c  o  m
 * @return A set of nearby URLs, sorted by distance.
 */
@VisibleForTesting
public List<UrlInfo> getUrls(boolean allowUnresolved) {
    Set<String> resolvedUrls = mPwsResultMap.keySet();
    Set<String> intersection = new HashSet<>(mNearbyUrls);
    intersection.retainAll(resolvedUrls);
    Log.d(TAG, "Get URLs With: %d nearby, %d resolved, and %d in intersection.", mNearbyUrls.size(),
            resolvedUrls.size(), intersection.size());

    List<UrlInfo> urlInfos = null;
    if (allowUnresolved && resolvedUrls.isEmpty()) {
        urlInfos = getUrlInfoList(mNearbyUrls);
    } else {
        urlInfos = getUrlInfoList(intersection);
    }
    Collections.sort(urlInfos, new Comparator<UrlInfo>() {
        @Override
        public int compare(UrlInfo urlInfo1, UrlInfo urlInfo2) {
            Double distance1 = Double.valueOf(urlInfo1.getDistance());
            Double distance2 = Double.valueOf(urlInfo2.getDistance());
            return distance1.compareTo(distance2);
        }
    });
    return urlInfos;
}

From source file:org.onosproject.imr.IntentMonitorAndRerouteManager.java

/**
 * Returns a list of links starting from a list of devices.
 * @param deviceList List of devices.// w w  w . j a  v  a  2  s  .  com
 * @return A path in terms of list of links.
 */
private List<Link> createPathFromDeviceList(List<ElementId> deviceList) {
    List<Link> path = new ArrayList<>();
    if (deviceList.size() == 1) {
        return path;
    }

    // Left element represents the input and right the output
    List<Pair<DeviceId, DeviceId>> devicePairs = IntStream.range(0, deviceList.size() - 1)
            .mapToObj(i -> Pair.of((DeviceId) deviceList.get(i), (DeviceId) deviceList.get(i + 1)))
            .collect(Collectors.toList());

    devicePairs.forEach(pair -> {
        //TODO use GetPath pair by pair?
        // The common Link between DevEgress and DevIngress is the intersection of their links
        Set<Link> commonLinks = new HashSet<>(linkService.getDeviceEgressLinks(pair.getLeft()));
        commonLinks.retainAll(linkService.getDeviceIngressLinks(pair.getRight()));
        if (commonLinks.size() == 0) {
            log.error("No link found between node {} and node {}!", pair.getLeft(), pair.getRight());
        } else if (commonLinks.size() == 1) {
            path.add(commonLinks.iterator().next());
        } else {
            //TODO select the one with more bandwidth?
            log.warn("{} links found between node {} and node {}: taking the first one!", commonLinks.size(),
                    pair.getLeft(), pair.getRight());
            path.add(commonLinks.iterator().next());
        }
    });

    return path;
}