Example usage for org.apache.commons.collections CollectionUtils intersection

List of usage examples for org.apache.commons.collections CollectionUtils intersection

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils intersection.

Prototype

public static Collection intersection(final Collection a, final Collection b) 

Source Link

Document

Returns a Collection containing the intersection of the given Collection s.

Usage

From source file:com.github.NearestNeighbors.java

/**
 * Calculates accuracy between actual and predicted watchers.
 * /*from w  w  w .ja  v a2 s.  com*/
 * @param actual
 * @param predicted
 * @return
 */
public static float accuracy(final Watcher actual, final Watcher predicted) {
    if ((actual == null) || (predicted == null)) {
        return 0.0f;
    }

    if ((actual.repositories.isEmpty()) && (predicted.repositories.isEmpty())) {
        return 1.0f;
    }

    if ((actual.repositories.isEmpty()) && (!predicted.repositories.isEmpty())) {
        return 0.0f;
    }

    if ((actual.repositories.isEmpty()) || (predicted.repositories.isEmpty())) {
        return 0.0f;
    }

    int number_correct = CollectionUtils.intersection(actual.repositories, predicted.repositories).size();
    int number_incorrect = CollectionUtils.subtract(predicted.repositories, actual.repositories).size();

    // Rate the accuracy of the predictions, with a bias towards positive results.
    return ((float) number_correct) / actual.repositories.size(); // - ((float) (number_incorrect) / predicted.repositories.size();
}

From source file:com.atlassian.jira.bc.group.DefaultGroupService.java

boolean validateUserIsNotInSelectedGroups(final JiraServiceContext jiraServiceContext,
        final Collection<String> selectedGroupsNames, final User user) {
    final ErrorCollection errorCollection = new SimpleErrorCollection();
    final I18nHelper i18n = jiraServiceContext.getI18nBean();

    // get the selected groups that the user is already in
    final Collection<String> groupsUserIsIn = CollectionUtils.intersection(getUserGroups(user),
            selectedGroupsNames);//from w w  w  . j  a v a  2 s  . co m

    if ((selectedGroupsNames.size() == 1) && (groupsUserIsIn.size() == 1)) {
        final String groupName = groupsUserIsIn.iterator().next();
        errorCollection.addErrorMessage(i18n.getText("admin.bulkeditgroups.error.user.already.member.of.group",
                user.getName(), groupName));
    } else if (selectedGroupsNames.size() == groupsUserIsIn.size()) {
        errorCollection.addErrorMessage(
                i18n.getText("admin.bulkeditgroups.error.user.already.member.of.all", user.getName()));
    }

    if (errorCollection.hasAnyErrors()) {
        jiraServiceContext.getErrorCollection().addErrorCollection(errorCollection);
        return false;
    }
    return true;
}

From source file:com.atlassian.jira.bc.group.DefaultGroupService.java

boolean validateGroupIsNotInSelectedGroups(final JiraServiceContext jiraServiceContext,
        final Collection<String> selectedGroupsNames, final Group group) {
    final ErrorCollection errorCollection = new SimpleErrorCollection();
    final I18nHelper i18n = jiraServiceContext.getI18nBean();

    // get the selected groups that the user is already in
    final Collection groupsUserIsIn = CollectionUtils.intersection(getParentGroupNames(group),
            selectedGroupsNames);/*from w  w  w.j a v a 2 s. c  om*/

    if ((selectedGroupsNames.size() == 1) && (groupsUserIsIn.size() == 1)) {
        final String groupName = (String) groupsUserIsIn.iterator().next();
        errorCollection.addErrorMessage(i18n.getText(
                "admin.editnestedgroups.error.group.already.member.of.group", group.getName(), groupName));
    } else if (selectedGroupsNames.size() == groupsUserIsIn.size()) {
        errorCollection.addErrorMessage(
                i18n.getText("admin.editnestedgroups.error.group.already.member.of.all", group.getName()));
    }

    if (errorCollection.hasAnyErrors()) {
        jiraServiceContext.getErrorCollection().addErrorCollection(errorCollection);
        return false;
    }
    return true;
}

From source file:com.atlassian.jira.bc.group.DefaultGroupService.java

/**
 * Determines whether the given user is in all the groups with the given names or not.
 *
 * @param user       the user./*  w w  w. j a  v  a  2  s.co m*/
 * @param groupNames the names of the groups to check.
 * @return true only if the user is a member of all the groups.
 */
boolean isUserInGroups(final User user, final Set<String> groupNames) {
    final Set<String> usersGroupNames = new HashSet<String>(getUserGroups(user));
    // if the subset of the given groupnames that are also groups the user is in is the same set as the
    // given groups, then the user is in all the given groups.
    return CollectionUtils.intersection(usersGroupNames, groupNames).size() == groupNames.size();
}

From source file:com.atlassian.jira.bc.group.DefaultGroupService.java

/**
 * Determines whether the given chilg group is in all the groups with the given names or not.
 *
 * @param child      the child group./*from  w  w w  .  j  a  v  a2 s.  com*/
 * @param groupNames the names of the groups to check.
 * @return true only if the group is a member of all the groups.
 */
boolean isGroupInGroups(final Group child, final Set<String> groupNames) {
    final Collection usersGroupNames = getParentGroupNames(child);
    // if the subset of the given groupnames that are also groups the user is in is the same set as the
    // given groups, then the user is in all the given groups.
    return CollectionUtils.intersection(usersGroupNames, groupNames).size() == groupNames.size();
}

From source file:edu.jhuapl.openessence.datasource.jdbc.entry.JdbcOeDataEntrySource.java

/**
 * Takes collection of db-key/value pairings from the original and replacement records. Returns a map of
 * sqlOperation/list of db-key/value pairings. The 3 sqlOperations are "INSERT" "UPDATE" and "DELETE." Basic logic is
 * this:/*w  w w .j  ava2s  .  com*/
 *
 * UPDATE - pk/val exists in original AND replacement record INSERT - pk/val exists in replacement record but NOT in
 * original record DELETE - pk/val exists in origianal record but NOT in replacement record
 *
 * @param originalPks    original record's set of (dbkeys<->values)
 * @param replacementPks replacement record's set of (dbkeys<->values)
 * @return categorization of the original and replacement records' (dbkeys<->values) into INSERT, UPDATE, DELETE
 *         operations
 */
public Map<String, List<Map<String, Object>>> categorizePKs(Set<Map<String, Object>> originalPks,
        Set<Map<String, Object>> replacementPks) {
    Set<Map<String, Object>> ORIG = originalPks;
    Set<Map<String, Object>> REPL = replacementPks;

    List<Map<String, Object>> UPDATE = new ArrayList<Map<String, Object>>();
    List<Map<String, Object>> INSERT = new ArrayList<Map<String, Object>>();
    List<Map<String, Object>> DELETE = new ArrayList<Map<String, Object>>();

    UPDATE.addAll(CollectionUtils.intersection(ORIG, REPL));
    INSERT.addAll(CollectionUtils.subtract(REPL, ORIG));
    DELETE.addAll(CollectionUtils.subtract(ORIG, REPL));

    Map<String, List<Map<String, Object>>> categorizedPKs = new HashMap<String, List<Map<String, Object>>>();
    categorizedPKs.put("UPDATE", UPDATE);
    categorizedPKs.put("INSERT", INSERT);
    categorizedPKs.put("DELETE", DELETE);

    log.debug("pks for update: " + UPDATE);
    log.debug("pks for insertion: " + INSERT);
    log.debug("pks for deletion: " + DELETE);
    return categorizedPKs;
}

From source file:nl.strohalm.cyclos.controls.groups.customizedFiles.GetAvailableFilesForGroupAjaxAction.java

@Override
@SuppressWarnings("unchecked")
protected void renderContent(final ActionContext context) throws Exception {
    final GetAvailableFilesForGroupAjaxForm form = context.getForm();
    final CustomizedFileQuery query = getDataBinder().readFromString(form);
    Type type = query.getType();//from w w w  . j  ava  2 s  .c o m
    if (type == null) {
        type = CustomizedFile.Type.STATIC_FILE;
        query.setType(type);
    }
    if (query.getGroup() == null) {
        throw new ValidationException();
    }
    final List<CustomizedFile> customizedFiles = customizedFileService.search(query);
    final List<String> notAlreadyCustomized = customizationHelper.onlyNotAlreadyCustomized(type,
            customizedFiles);

    // Ensure only possible per type
    final Map<Type, Collection<String>> allPossible = context.isAdmin() ? POSSIBLE_CUSTOMIZED_FILES
            : POSSIBLE_CUSTOMIZED_FILES_OPERATOR;
    final Collection<String> possible = CollectionUtils.intersection(notAlreadyCustomized,
            allPossible.get(type));
    responseHelper.writeJSON(context.getResponse(), getCollectionBinder().readAsString(possible));
}

From source file:nl.strohalm.cyclos.controls.groups.groupFilters.customizedFiles.GetAvailableFilesForGroupFilterAjaxAction.java

@Override
@SuppressWarnings("unchecked")
protected void renderContent(final ActionContext context) throws Exception {
    final GetAvailableFilesForGroupFilterAjaxForm form = context.getForm();
    final CustomizedFileQuery query = getDataBinder().readFromString(form);
    final Type type = query.getType();
    if (type == null || query.getGroupFilter() == null) {
        throw new ValidationException();
    }/*from w w w  .j  av  a  2  s.  c  o m*/
    final List<CustomizedFile> customizedFiles = customizedFileService.search(query);
    final List<String> notAlreadyCustomized = customizationHelper.onlyNotAlreadyCustomized(type,
            customizedFiles);

    // Ensure only possible per type
    final Collection<String> possible = CollectionUtils.intersection(notAlreadyCustomized,
            POSSIBLE_CUSTOMIZED_FILES.get(type));
    responseHelper.writeJSON(context.getResponse(), getCollectionBinder().readAsString(possible));
}

From source file:nl.strohalm.cyclos.services.elements.ElementServiceImpl.java

@Override
@SuppressWarnings("unchecked")
public List<? extends Element> fullTextSearch(final FullTextElementQuery query) {
    // Since the full text does not index group filters, only groups, disassemble the group filters into groups
    if (query instanceof FullTextMemberQuery) {
        final FullTextMemberQuery memberQuery = (FullTextMemberQuery) query;
        // groupFilters
        final Collection<GroupFilter> groupFilters = fetchService.fetch(memberQuery.getGroupFilters(),
                GroupFilter.Relationships.GROUPS);
        if (CollectionUtils.isNotEmpty(groupFilters)) {
            final boolean hasGroupFilters = CollectionUtils.isNotEmpty(memberQuery.getGroupFilters());
            final boolean hasGroups = CollectionUtils.isNotEmpty(memberQuery.getGroups());
            final Set<Group> groupsFromFilters = new HashSet<Group>();
            if (hasGroupFilters) {
                // Get all groups from selected group filters
                for (final GroupFilter groupFilter : groupFilters) {
                    groupsFromFilters.addAll(groupFilter.getGroups());
                }/* ww  w.  j  a  va 2  s.c o  m*/
                if (hasGroups) {
                    // When there's both groups and group filters, use an intersection between them
                    memberQuery.setGroups(
                            CollectionUtils.intersection(groupsFromFilters, memberQuery.getGroups()));
                } else {
                    // Else, use only the groups from group filters
                    memberQuery.setGroups(groupsFromFilters);
                }
            }
            memberQuery.setGroupFilters(null);
        }
    }
    if (query.getNameDisplay() == null) {
        query.setNameDisplay(settingsService.getLocalSettings().getMemberResultDisplay());
    }
    query.setAnalyzer(settingsService.getLocalSettings().getLanguage().getAnalyzer());
    return elementDao.fullTextSearch(query);
}

From source file:nl.strohalm.cyclos.services.elements.ElementServiceImpl.java

@Override
@SuppressWarnings("unchecked")
public void validateBulkChangeChannels(final FullTextMemberQuery query,
        final Collection<Channel> enableChannels, final Collection<Channel> disableChannels) {
    Collection<Channel> intersection = CollectionUtils.intersection(enableChannels, disableChannels);
    if (CollectionUtils.isNotEmpty(intersection)) {
        throw new ValidationException("changeChannels.invalidChannelsSelection", toString(intersection));
    }// w  w w .ja va  2 s.co  m
}