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:ubic.pubmedgate.mallet.SimpleMalletRunner.java

public void compareFeatures() throws Exception {
    float sum = 0;
    float sumTotal = 0;
    for (FoldRunner foldRunner : foldRunners) {
        // feature count on training
        FeatureCounter counter = new FeatureCounter(this, false);
        counter.run(foldRunner.training);
        Set<String> trainFeatures = counter.getMap().keySet();
        int trainSize = trainFeatures.size();

        // feature count on test
        counter = new FeatureCounter(this, false);
        counter.run(foldRunner.testing);
        CountingMap<String> testMap = counter.getMap();
        Set<String> testFeatures = testMap.keySet();
        int testSize = testFeatures.size();
        long testSizeTotal = counter.getMap().summation();

        // stupid shallow copy error here
        Set<String> intersect = new HashSet<String>(trainFeatures);
        intersect.retainAll(testFeatures);
        int intersectSize = intersect.size();
        for (String key : intersect) {
            testMap.remove(key);/*from   w w  w .j av a 2s  .com*/
        }
        long intersectTotal = testMap.summation();

        log.info("Train Features=" + trainSize + " Test Features=" + testSize + " Intersect:" + intersectSize
                + " %unseen" + (float) intersect.size() / (float) testFeatures.size());
        sum += (float) intersectSize / (float) testSize;
        log.info("test summation:" + testSizeTotal + " intersect summation:" + intersectTotal + " %unseen"
                + intersectTotal / (float) testSizeTotal);
        sumTotal += intersectTotal / (float) testSizeTotal;

        // for ( int i = 0; i < 20; i++ ) {
        // log.info( testMap.keySet().toArray()[i] );
        // }
    }

    log.info("Percent of features not seen in training set: " + (1 - sum / (float) numFolds));
    log.info("Percent of words not seen in training set: " + sumTotal / (float) numFolds);
    otherResults.put("%NewFeatures", (1 - sum / (float) numFolds) + "");
    otherResults.put("%NewWords", (sumTotal / (float) numFolds) + "");
}

From source file:ubic.BAMSandAllen.visualization.GraphFromABAMS.java

public void intersectEdges(ConnectivityAndAllenExpressionMatrixPair pair, boolean symetric,
        DoubleMatrix<String, String> matrixAPlain, DoubleMatrix<String, String> matrixBPlain) throws Exception {
    // make new matrix with names joined

    DoubleMatrix<String, String> matrixHadaProd;
    Set<String> colNames = new HashSet<String>();
    colNames.addAll(matrixAPlain.getColNames());
    colNames.retainAll(matrixBPlain.getColNames());
    matrixHadaProd = new DenseDoubleMatrix<String, String>(colNames.size(), colNames.size());
    matrixHadaProd.setRowNames(new LinkedList<String>(colNames));
    matrixHadaProd.setColumnNames(new LinkedList<String>(colNames));

    log.info(/*  ww w  . j a  va2s  .  c  o m*/
            colNames.size() + " colnames kept of " + matrixAPlain.columns() + " and " + matrixBPlain.columns());
    int edgeCount = 0;
    for (String rowName : colNames) {
        for (String colName : colNames) {
            double value = matrixBPlain.getByKeys(rowName, colName) * matrixAPlain.getByKeys(rowName, colName);
            matrixHadaProd.setByKeys(rowName, colName, value);
            if (value != 0)
                edgeCount++;
        }
    }
    log.info("Intersection edges added:" + edgeCount + " (includes doubles)");

    // DoubleMatrix<String, String> matrixHadaProd = matrixBPlain.copy();
    // if ( !matrixBPlain.getColNames().equals( matrixAPlain.getColNames() ) )
    // throw new Exception( "Error col names don't match" );
    // int edgeCount = 0;
    // for ( int i = 0; i < matrixBPlain.rows(); i++ ) {
    // for ( int j = 0; j < matrixBPlain.columns(); j++ ) {
    // double value = matrixBPlain.get( i, j ) * matrixAPlain.get( i, j );
    // matrixHadaProd.set( i, j, value );
    // if ( value != 0 ) edgeCount++;
    // }
    // }
    // log.info( "Intersection edges added:" + edgeCount + " (includes doubles)" );
    addBAMSMatrix(matrixHadaProd, pair, "intersection", symetric);
}

From source file:ubic.GEOMMTx.ParentFinder.java

/**
 * Given a set of URI's return only the leaves, that is remove all of the parent terms.
 * //from   w w w . j av a2s . co m
 * @param inputURIs
 * @return
 */
public Set<String> onlyLeaves(Set<String> inputURIs) {
    Set<String> result = new HashSet<String>(inputURIs);
    for (String URI : inputURIs) {
        Set<String> intersection = new HashSet<String>(inputURIs);
        OntologyTerm t = getTerm(URI);
        if (t == null) {
            nullTerms++;
            // log.warn( "got null term " + URI );
            continue;
        }

        // convert the parents to string
        Set<String> parents = new HashSet<String>();
        for (OntologyTerm p : t.getParents(false)) {
            parents.add(p.getUri());
        }

        intersection.retainAll(parents);

        // remove those that are in both lists from the final list
        result.removeAll(intersection);
    }
    return result;
}

From source file:cc.kave.commons.pointsto.evaluation.events.MRREvaluation.java

private Set<ICoReTypeName> getStoreTypes() {
    Set<ICoReTypeName> storeTypes = new HashSet<>();
    for (UsageStore store : usageStores) {
        Set<ICoReTypeName> types = store.getAllTypes().stream().filter(usageFilter::test)
                .collect(Collectors.toSet());
        if (storeTypes.isEmpty()) {
            storeTypes.addAll(types);/*from w w w.jav  a  2 s.  c  o  m*/
        } else {
            storeTypes.retainAll(types);
        }
    }
    return storeTypes;
}

From source file:gov.nih.nci.caintegrator.application.lists.UserListBeanHelper.java

public void intersectLists(List<String> listNames, String newListName, ListType listType) {
    List<String> items = new ArrayList<String>();
    List<UserList> lists = new ArrayList<UserList>();
    for (String listName : listNames) {
        UserList list = userListBean.getList(listName);
        lists.add(list);// w  w w  .  j a  v a2 s .com
        if (!list.getList().isEmpty()) {
            items.addAll(list.getList());
        }
    }
    Set<String> intersectedList = new HashSet<String>(items);
    for (UserList ul : lists) {
        intersectedList.retainAll(ul.getList());
    }
    items.clear();
    items.addAll(intersectedList);
    Collections.sort(items, String.CASE_INSENSITIVE_ORDER);
    UserList newList = new UserList(newListName, listType, items, new ArrayList<String>(), new Date());
    newList.setListOrigin(ListOrigin.Custom);
    newList.setItemCount(items.size());
    userListBean.addList(newList);
}

From source file:org.apache.oodt.cas.pge.metadata.PgeMetadata.java

/**
 * Use to commit marked LOCAL keys to DYNAMIC keys. Specify a list of keys
 * only if you want to limit the keys which get committed, otherwise all
 * marked keys will be moved into DYNAMIC metadata.
 * //from w ww.  j  a  va 2 s.com
 * @param keys
 *           The list of marked LOCAL metadata keys which should be moved
 *           into DYNAMIC metadata. If no keys are specified then all marked
 *           keys are moved.
 */
public void commitMarkedDynamicMetadataKeys(String... keys) {
    Set<String> commitKeys = Sets.newHashSet(keys);
    if (commitKeys.isEmpty()) {
        commitKeys.addAll(markedAsDynamicMetKeys);
    } else {
        commitKeys.retainAll(markedAsDynamicMetKeys);
    }
    for (String key : commitKeys) {
        dynamicMetadata.replaceMetadata(key, localMetadata.getAllMetadata(resolveKey(key)));
        localMetadata.removeMetadata(key);
        markedAsDynamicMetKeys.remove(key);
    }
}

From source file:org.eclipse.jubula.client.core.commands.CAPRecordedCommand.java

/**
 * @param messageCap MessageCap//w w w  . j a  v a2  s. c  o  m
 * @param compSystem Component
 * @param componentType String
 * @return the highest component that supports the current action (e.g.
 *         gdSelect on TreeTable will return Tree)
 */
@SuppressWarnings("unchecked")
private Component getComponentToUse(MessageCap messageCap, CompSystem compSystem, String componentType) {
    List<Component> supportedComponents = compSystem.findComponents(componentType);

    Set<Component> compsWithAction = new HashSet<Component>();
    for (Component c : supportedComponents) {
        List<Action> actionList = new LinkedList<Action>(c.getActions());

        if (actionList.contains(messageCap.getAction())) {
            compsWithAction.add(c);
        }
    }

    Component theComponentToUse = null;
    for (Component c : compsWithAction) {
        Set<Component> realizedIntersection = new HashSet<Component>(c.getAllRealized());
        realizedIntersection.retainAll(compsWithAction);
        if (realizedIntersection.isEmpty()) {
            theComponentToUse = c;
            break;
        }
    }

    return theComponentToUse;
}

From source file:com.github.lindenb.jvarkit.tools.vcfcmp.VcfCompareCallers.java

@Override
public Collection<Throwable> call() throws Exception {
    htsjdk.samtools.util.IntervalTreeMap<Boolean> capture = null;
    PrintWriter exampleWriter = null;
    XMLStreamWriter exampleOut = null;
    PrintStream pw = null;//  w  w w  .  j a  v  a 2  s . c  om
    VcfIterator vcfInputs[] = new VcfIterator[] { null, null };
    VCFHeader headers[] = new VCFHeader[] { null, null };
    final List<String> args = getInputFiles();
    try {
        if (args.size() == 1) {
            LOG.info("Reading from stdin and " + args.get(0));
            vcfInputs[0] = VCFUtils.createVcfIteratorStdin();
            vcfInputs[1] = VCFUtils.createVcfIterator(args.get(0));
        } else if (args.size() == 2) {
            LOG.info("Reading from stdin and " + args.get(0) + " and " + args.get(1));
            vcfInputs[0] = VCFUtils.createVcfIterator(args.get(0));
            vcfInputs[1] = VCFUtils.createVcfIterator(args.get(1));
        } else {
            return wrapException(getMessageBundle("illegal.number.of.arguments"));
        }

        if (super.captureFile != null) {
            LOG.info("Reading " + super.captureFile);
            capture = super.readBedFileAsBooleanIntervalTreeMap(super.captureFile);
        }

        for (int i = 0; i < vcfInputs.length; ++i) {
            headers[i] = vcfInputs[i].getHeader();
        }
        /* dicts */
        final SAMSequenceDictionary dict0 = headers[0].getSequenceDictionary();
        final SAMSequenceDictionary dict1 = headers[1].getSequenceDictionary();
        final Comparator<VariantContext> ctxComparator;
        if (dict0 == null && dict1 == null) {
            ctxComparator = VCFUtils.createChromPosRefComparator();
        } else if (dict0 != null && dict1 != null) {
            if (!SequenceUtil.areSequenceDictionariesEqual(dict0, dict1)) {
                return wrapException(getMessageBundle("not.the.same.sequence.dictionaries"));
            }
            ctxComparator = VCFUtils.createTidPosRefComparator(dict0);
        } else {
            return wrapException(getMessageBundle("not.the.same.sequence.dictionaries"));
        }
        /* samples */
        Set<String> samples0 = new HashSet<>(headers[0].getSampleNamesInOrder());
        Set<String> samples1 = new HashSet<>(headers[1].getSampleNamesInOrder());
        Set<String> samples = new TreeSet<>(samples0);
        samples.retainAll(samples1);

        if (samples.size() != samples0.size() || samples.size() != samples1.size()) {
            LOG.warn("Warning: Not the same samples set. Using intersection of both lists.");
        }
        if (samples.isEmpty()) {
            return wrapException("No common samples");
        }

        Map<String, Counter<Category>> sample2info = new HashMap<String, Counter<Category>>(samples.size());
        for (String sampleName : samples) {
            sample2info.put(sampleName, new Counter<Category>());
        }

        if (super.exampleFile != null) {
            exampleWriter = new PrintWriter(exampleFile, "UTF-8");
            XMLOutputFactory xof = XMLOutputFactory.newFactory();
            exampleOut = xof.createXMLStreamWriter(exampleWriter);
            exampleOut.writeStartDocument("UTF-8", "1.0");
            exampleOut.writeStartElement("compare-callers");
        }

        SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(dict0);
        VariantContext buffer[] = new VariantContext[vcfInputs.length];
        VariantContext prev[] = new VariantContext[vcfInputs.length];
        for (;;) {
            VariantContext smallest = null;
            //refill buffer
            for (int i = 0; i < vcfInputs.length; ++i) {
                if (buffer[i] == null && vcfInputs[i] != null) {
                    if (vcfInputs[i].hasNext()) {
                        buffer[i] = vcfInputs[i].peek();
                        /* check data are sorted */
                        if (prev[i] != null && ctxComparator.compare(prev[i], buffer[i]) > 0) {
                            return wrapException("Input " + (i + 1) + "/2 is not sorted"
                                    + (((i == 0 && dict0 == null) || (i == 1 && dict1 == null))
                                            ? "on chrom/pos/ref"
                                            : "on sequence dictionary")
                                    + ". got\n" + buffer[i] + "\nafter\n" + prev[i]);
                        }
                    } else {
                        vcfInputs[i].close();
                        vcfInputs[i] = null;
                    }
                }

                if (buffer[i] != null) {
                    if (smallest == null || ctxComparator.compare(buffer[i], smallest) < 0) {
                        smallest = buffer[i];
                    }
                }
            }

            if (smallest == null)
                break;

            VariantContext ctx0 = null;
            VariantContext ctx1 = null;
            Interval interval = null;

            if (buffer[0] != null && ctxComparator.compare(buffer[0], smallest) == 0) {
                prev[0] = progress.watch(vcfInputs[0].next());
                ctx0 = prev[0];
                buffer[0] = null;
                interval = new Interval(ctx0.getContig(), ctx0.getStart(), ctx0.getEnd());
            }
            if (buffer[1] != null && ctxComparator.compare(buffer[1], smallest) == 0) {
                prev[1] = progress.watch(vcfInputs[1].next());
                ctx1 = prev[1];
                buffer[1] = null;
                interval = new Interval(ctx1.getContig(), ctx1.getStart(), ctx1.getEnd());
            }
            boolean in_capture = true;
            if (capture != null && interval != null) {
                in_capture = capture.containsOverlapping(interval);
            }

            for (final String sampleName : sample2info.keySet()) {
                final Counter<Category> sampleInfo = sample2info.get(sampleName);
                Genotype g0 = (ctx0 == null ? null : ctx0.getGenotype(sampleName));
                Genotype g1 = (ctx1 == null ? null : ctx1.getGenotype(sampleName));
                if (g0 != null && (g0.isNoCall() || !g0.isAvailable()))
                    g0 = null;
                if (g1 != null && (g1.isNoCall() || !g1.isAvailable()))
                    g1 = null;

                if (g0 == null && g1 == null) {
                    watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.both_missing);
                    continue;
                } else if (g0 != null && g1 == null) {
                    if (!in_capture) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.off_target_only_1);
                        continue;
                    }
                    watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_1);

                    if (ctx0.isIndel()) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.unique_to_file_1_indel);
                    } else if (ctx0.isSNP()) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.unique_to_file_1_snp);
                    }
                    continue;
                } else if (g0 == null && g1 != null) {
                    if (!in_capture) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.off_target_only_2);
                        continue;
                    }
                    watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.unique_to_file_2);
                    if (ctx1.isIndel()) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.unique_to_file_2_indel);
                    } else if (ctx1.isSNP()) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.unique_to_file_2_snp);
                    }
                    continue;
                } else {
                    if (!in_capture) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.off_target_both);
                        continue;
                    }
                    watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.common_context);
                    if (ctx0.isIndel() && ctx1.isIndel()) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.common_context_indel);
                    } else if (ctx0.isSNP() && ctx1.isSNP()) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.common_context_snp);
                    }

                    if ((ctx0.hasID() && !ctx1.hasID()) || (!ctx0.hasID() && ctx1.hasID())
                            || (ctx0.hasID() && ctx1.hasID() && !ctx0.getID().equals(ctx1.getID()))) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.common_context_discordant_id);
                    }

                    if (g0.sameGenotype(g1)) {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo, Category.called_and_same);

                        if (g0.isHomRef()) {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_and_same_hom_ref);
                        }
                        if (g0.isHomVar()) {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_and_same_hom_var);
                        } else if (g0.isHet()) {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_and_same_het);
                        }
                    } else {
                        watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                Category.called_but_discordant);

                        if (g0.isHom() && g1.isHet()) {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_but_discordant_hom1_het2);
                        } else if (g0.isHet() && g1.isHom()) {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_but_discordant_het1_hom2);
                        } else if (g0.isHom() && g1.isHom()) {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_but_discordant_hom1_hom2);
                        } else if (g0.isHet() && g1.isHet()) {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_but_discordant_het1_het2);
                        } else {
                            watch(exampleOut, ctx0, ctx1, g0, g1, sampleName, sampleInfo,
                                    Category.called_but_discordant_others);
                        }
                    }

                }
            }
        }
        progress.finish();

        pw = openFileOrStdoutAsPrintStream();
        pw.print("#Sample");
        for (Category c : Category.values()) {
            pw.print('\t');
            pw.print(c.name());
        }
        pw.println();
        for (String sample : sample2info.keySet()) {
            Counter<Category> count = sample2info.get(sample);
            pw.print(sample);
            for (Category c : Category.values()) {
                pw.print('\t');
                pw.print(count.count(c));
            }
            pw.println();
            if (pw.checkError())
                break;
        }
        pw.flush();

        if (exampleOut != null) {
            exampleOut.writeEndElement();
            exampleOut.writeEndDocument();
            exampleOut.flush();
            exampleOut.close();
        }
        return RETURN_OK;
    } catch (Exception err) {
        return wrapException(err);
    } finally {
        if (getOutputFile() != null)
            CloserUtil.close(pw);
        CloserUtil.close(exampleWriter);
    }

}

From source file:org.apache.syncope.core.provisioning.java.propagation.PropagationManagerImpl.java

@Override
public List<PropagationTaskTO> getUserUpdateTasks(final WorkflowResult<Pair<UserPatch, Boolean>> wfResult) {
    UserPatch userPatch = wfResult.getResult().getKey();

    // Propagate password update only to requested resources
    List<PropagationTaskTO> tasks = new ArrayList<>();
    if (userPatch.getPassword() == null) {
        // a. no specific password propagation request: generate propagation tasks for any resource associated
        tasks = getUserUpdateTasks(wfResult, false, null);
    } else {/* www.jav  a2 s .c  o m*/
        // b. generate the propagation task list in two phases: first the ones containing password,
        // the the rest (with no password)
        WorkflowResult<Pair<UserPatch, Boolean>> pwdWFResult = new WorkflowResult<>(wfResult.getResult(),
                new PropagationByResource(), wfResult.getPerformedTasks());

        Set<String> pwdResourceNames = new HashSet<>(userPatch.getPassword().getResources());
        Collection<String> allResourceNames = userDAO.findAllResourceKeys(userPatch.getKey());
        pwdResourceNames.retainAll(allResourceNames);

        pwdWFResult.getPropByRes().addAll(ResourceOperation.UPDATE, pwdResourceNames);
        if (!pwdWFResult.getPropByRes().isEmpty()) {
            Set<String> toBeExcluded = new HashSet<>(allResourceNames);
            toBeExcluded.addAll(userPatch.getResources().stream().map(patchItem -> patchItem.getValue())
                    .collect(Collectors.toList()));
            toBeExcluded.removeAll(pwdResourceNames);

            tasks.addAll(getUserUpdateTasks(pwdWFResult, true, toBeExcluded));
        }

        WorkflowResult<Pair<UserPatch, Boolean>> noPwdWFResult = new WorkflowResult<>(wfResult.getResult(),
                new PropagationByResource(), wfResult.getPerformedTasks());

        noPwdWFResult.getPropByRes().merge(wfResult.getPropByRes());
        noPwdWFResult.getPropByRes().removeAll(pwdResourceNames);
        noPwdWFResult.getPropByRes().purge();
        if (!noPwdWFResult.getPropByRes().isEmpty()) {
            tasks.addAll(getUserUpdateTasks(noPwdWFResult, false, pwdResourceNames));
        }
    }

    return tasks;
}

From source file:com.blazebit.security.impl.interceptor.ChangeInterceptor.java

/**
 * /*from  w w  w. j  a  va 2s .co  m*/
 */
@Override
public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
    if (!EntityFeatures.isInterceptorActive()) {
        super.onCollectionUpdate(collection, key);
        return;
    }
    if (collection instanceof PersistentCollection) {
        PersistentCollection newValuesCollection = (PersistentCollection) collection;
        Object entity = newValuesCollection.getOwner();
        if (AnnotationUtils.findAnnotation(entity.getClass(), EntityResourceType.class) == null) {
            super.onCollectionUpdate(collection, key);
            return;
        }
        // copy new values and old values
        @SuppressWarnings({ "unchecked", "rawtypes" })
        Collection<?> newValues = new HashSet((Collection<?>) newValuesCollection.getValue());
        @SuppressWarnings({ "unchecked", "rawtypes" })
        Set<?> oldValues = new HashSet(((Map<?, ?>) newValuesCollection.getStoredSnapshot()).keySet());

        String fieldName = StringUtils.replace(newValuesCollection.getRole(), entity.getClass().getName() + ".",
                "");
        UserContext userContext = BeanProvider.getContextualReference(UserContext.class);
        ActionFactory actionFactory = BeanProvider.getContextualReference(ActionFactory.class);
        EntityResourceFactory resourceFactory = BeanProvider
                .getContextualReference(EntityResourceFactory.class);
        PermissionService permissionService = BeanProvider.getContextualReference(PermissionService.class);

        // find all objects that were added
        boolean isGrantedToAdd = true;
        boolean isGrantedToRemove = true;

        @SuppressWarnings({ "unchecked", "rawtypes" })
        Set<?> retained = new HashSet(oldValues);
        retained.retainAll(newValues);

        oldValues.removeAll(retained);
        // if there is a difference between oldValues and newValues
        if (!oldValues.isEmpty()) {
            // if something remained
            isGrantedToRemove = permissionService.isGranted(actionFactory.createAction(Action.REMOVE),
                    resourceFactory.createResource(entity, fieldName));
        }
        newValues.removeAll(retained);
        if (!newValues.isEmpty()) {
            isGrantedToAdd = permissionService.isGranted(actionFactory.createAction(Action.ADD),
                    resourceFactory.createResource(entity, fieldName));
        }

        if (!isGrantedToAdd) {
            throw new PermissionActionException("Element cannot be added to entity " + entity + "'s collection "
                    + fieldName + " by " + userContext.getUser());
        } else {
            if (!isGrantedToRemove) {
                throw new PermissionActionException("Element cannot be removed from entity " + entity
                        + "'s collection " + fieldName + " by " + userContext.getUser());
            } else {
                super.onCollectionUpdate(collection, key);
                return;
            }
        }
    } else {
        // not a persistent collection?
    }
}