Java Code Examples for org.apache.commons.collections.CollectionUtils#intersection()

The following examples show how to use org.apache.commons.collections.CollectionUtils#intersection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompositeMap.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * Add an additional Map to the composite.
 *
 * @param map  the Map to be added to the composite
 * @throws IllegalArgumentException if there is a key collision and there is no
 *         MapMutator set to handle it.
 */
public synchronized void addComposited(Map map) throws IllegalArgumentException {
    for (int i = composite.length - 1; i >= 0; --i) {
        Collection intersect = CollectionUtils.intersection(this.composite[i].keySet(), map.keySet());
        if (intersect.size() != 0) {
            if (this.mutator == null) {
                throw new IllegalArgumentException("Key collision adding Map to CompositeMap");
            }
            else {
                this.mutator.resolveCollision(this, this.composite[i], map, intersect);
            }
        }
    }
    Map[] temp = new Map[this.composite.length + 1];
    System.arraycopy(this.composite, 0, temp, 0, this.composite.length);
    temp[temp.length - 1] = map;
    this.composite = temp;
}
 
Example 2
Source File: MapComparator.java    From eagle with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void compare() {
    Set<K> keys1 = map1.keySet();
    Set<K> keys2 = map2.keySet();
    Collection<K> addedKeys = CollectionUtils.subtract(keys1, keys2);
    Collection<K> removedKeys = CollectionUtils.subtract(keys2, keys1);
    Collection<K> modifiedKeys = CollectionUtils.intersection(keys1, keys2);

    addedKeys.forEach(k -> added.add(map1.get(k)));
    removedKeys.forEach(k -> removed.add(map2.get(k)));
    modifiedKeys.forEach(k -> {
        if (!map1.get(k).equals(map2.get(k))) {
            modified.add(map1.get(k));
        }
    });
}
 
Example 3
Source File: CompositeSet.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Add a Set to this composite
 *
 * @param c Must implement Set
 * @throws IllegalArgumentException if c does not implement java.util.Set
 *         or if a SetMutator is set, but fails to resolve a collision
 * @throws UnsupportedOperationException if there is no SetMutator set, or
 *         a CollectionMutator is set instead of a SetMutator
 * @see org.apache.commons.collections.collection.CompositeCollection.CollectionMutator
 * @see SetMutator
 */
public synchronized void addComposited(Collection c) {
    if (!(c instanceof Set)) {
        throw new IllegalArgumentException("Collections added must implement java.util.Set");
    }
    
    for (Iterator i = this.getCollections().iterator(); i.hasNext();) {
        Set set = (Set) i.next();
        Collection intersects = CollectionUtils.intersection(set, c);
        if (intersects.size() > 0) {
            if (this.mutator == null) {
                throw new UnsupportedOperationException(
                    "Collision adding composited collection with no SetMutator set");
            }
            else if (!(this.mutator instanceof SetMutator)) {
                throw new UnsupportedOperationException(
                    "Collision adding composited collection to a CompositeSet with a CollectionMutator instead of a SetMutator");
            }
            ((SetMutator) this.mutator).resolveCollision(this, set, (Set) c, intersects);
            if (CollectionUtils.intersection(set, c).size() > 0) {
                throw new IllegalArgumentException(
                    "Attempt to add illegal entry unresolved by SetMutator.resolveCollision()");
            }
        }
    }
    super.addComposited(new Collection[]{c});
}
 
Example 4
Source File: IntersectionOfTwoSets.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void intersection_of_sets_apache_commons () {
	
	@SuppressWarnings("rawtypes")
	Collection mutalFriends = CollectionUtils.intersection(yourFriends, myFriends);
	
	assertEquals(4, mutalFriends.size());
}
 
Example 5
Source File: CheckClassLoading.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static Collection<DiffItem> intersection(final Classes cl1, final Classes cl2) {
    final List<DiffItem> diff = new ArrayList<>();
    for (final Map.Entry<String, Collection<String>> entry1 : cl1.fileByArchive.entrySet()) {
        for (final Map.Entry<String, Collection<String>> entry2 : cl2.fileByArchive.entrySet()) {
            final Collection<String> v1 = entry1.getValue();
            final Collection<String> v2 = entry2.getValue();
            final Collection<String> inter = CollectionUtils.intersection(v1, v2);

            if (inter.size() == 0) {
                continue;
            }

            if (inter.size() == v1.size() && v1.size() == v2.size()) {
                diff.add(new SameItem(inter, entry1.getKey(), entry2.getKey()));
            } else if (inter.size() == v1.size()) {
                diff.add(new IncludedItem(inter, entry1.getKey(), entry2.getKey()));
            } else if (inter.size() == v2.size()) {
                diff.add(new ContainingItem(inter, entry1.getKey(), entry2.getKey()));
            } else {
                diff.add(new DiffItem(inter, entry1.getKey(), entry2.getKey()));
            }
        }
    }

    diff.sort(DiffItemComparator.getInstance());
    return diff;
}
 
Example 6
Source File: AggregateFileSource.java    From mr4c with Apache License 2.0 5 votes vote down vote up
public List<String> getAllFileNames() throws IOException {
	List<String> names = new ArrayList<String>();
	for ( FileSource src : m_sources ) {
		List<String> newNames = src.getAllFileNames();
		Collection<String> dups= CollectionUtils.intersection(names, newNames);
		if ( !dups.isEmpty() ) {
			throw new IllegalStateException("Found the following repeated file names: " + dups);
		}
		names.addAll(newNames);
	}
	return names;
}
 
Example 7
Source File: SequentialCachingAlgorithmTest.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void calculateDatasets(String dataset, String fromAssociation, String filter) throws Exception {

	Map<String, String> datasetAssociation = new HashMap<String, String>(datasets.get(dataset));
	// no need to iterate over the incoming association
	datasetAssociation.remove(fromAssociation);
	// iterate over all the associations
	for (String association : datasetAssociation.keySet()) {
		String columnName = datasetAssociation.get(association);
		String query = "SELECT DISTINCT " + columnName + " FROM " + dataset + " WHERE " + filter;
		Set<String> distinctValues = new HashSet<String>();
		ResultSet rs = stmt.executeQuery(query);
		while (rs.next()) {
			distinctValues.add(rs.getString(columnName));
		}
		Set<String> baseSet = associationValues.get(association);
		Set<String> intersection = new HashSet<String>(CollectionUtils.intersection(baseSet, distinctValues));
		if (!intersection.equals(baseSet)) {
			associationValues.put(association, intersection);
			String inClauseValues = "'" + StringUtils.join(associationValues.get(association).iterator(), "','") + "'";
			String f = columnName + " IN (" + inClauseValues + ")";
			for (String datasetInvolved : associations.get(association)) {
				if (!datasetInvolved.equals(dataset)) {
					// it will skip the current dataset, from which the filter is fired
					calculateDatasets(datasetInvolved, association, f);
				}
			}
		}
	}

}
 
Example 8
Source File: PackageManager.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void checkPackageItems(NodeRef packageRef)
{
    List<NodeRef> currentitems = getCurrentItems(packageRef);
    Collection<NodeRef> intersection = CollectionUtils.intersection(addItems, removeItems);
    addItems.removeAll(intersection);
    removeItems.removeAll(intersection);
    for (NodeRef node : intersection)
    {
        if (logger.isDebugEnabled())
            logger.debug("Item was added and removed from package! Ignoring item: "+ node);
    }
    checkAddedItems(currentitems);
    checkRemovedItems(currentitems);
}
 
Example 9
Source File: DrugsApiController.java    From oncokb with GNU Affero General Public License v3.0 4 votes vote down vote up
public ResponseEntity<List<Drug>> drugsLookupGet(
    @ApiParam(value = "Drug Name") @RequestParam(value = "name", required = false) String name
    , @ApiParam(value = "NCI Thesaurus Code") @RequestParam(value = "ncitCode", required = false) String ncitCode
    , @ApiParam(value = "Drug Synonyms") @RequestParam(value = "synonym", required = false) String synonym
    , @ApiParam(value = "Exactly Match", required = true) @RequestParam(value = "exactMatch", required = true, defaultValue = "true") Boolean exactMatch
) {
    DrugBo drugBo = ApplicationContextSingleton.getDrugBo();
    Set<Drug> drugs = null;

    if (exactMatch == null) {
        exactMatch = true;
    }

    if (name != null) {
        drugs = DrugUtils.getDrugsByNames(Collections.singleton(name), !exactMatch);
    }

    if (ncitCode != null) {
        Drug drug = DrugUtils.getDrugByNcitCode(ncitCode);
        if (drug != null) {
            drugs = new HashSet<>(Collections.singleton(drug));
        }
    }

    if (synonym != null) {
        Set<Drug> result = DrugUtils.getDrugsBySynonyms(Collections.singleton(synonym), !exactMatch);
        if (result != null) {
            if (drugs == null) {
                drugs = result;
            } else {
                drugs = new HashSet<>(CollectionUtils.intersection(drugs, result));
            }
        }
    }

    List<Drug> drugList = new ArrayList();
    if (drugs != null) {
        drugList.addAll(drugs);
    }
    return new ResponseEntity<>(drugList, HttpStatus.OK);
}
 
Example 10
Source File: LevelUtils.java    From oncokb with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Set<LevelOfEvidence> getSensitiveLevels() {
    return new HashSet<>(CollectionUtils.intersection(PUBLIC_LEVELS, THERAPEUTIC_SENSITIVE_LEVELS));
}
 
Example 11
Source File: LevelUtils.java    From oncokb with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Set<LevelOfEvidence> getResistanceLevels() {
    return new HashSet<>(CollectionUtils.intersection(PUBLIC_LEVELS, THERAPEUTIC_RESISTANCE_LEVELS));
}
 
Example 12
Source File: LevelUtils.java    From oncokb with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Set<LevelOfEvidence> getPrognosticLevels() {
    return new HashSet<>(CollectionUtils.intersection(PUBLIC_LEVELS, PROGNOSTIC_LEVELS));
}
 
Example 13
Source File: LevelUtils.java    From oncokb with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Set<LevelOfEvidence> getDiagnosticLevels() {
    return new HashSet<>(CollectionUtils.intersection(PUBLIC_LEVELS, DIAGNOSTIC_LEVELS));
}
 
Example 14
Source File: ABE.java    From SPADE with GNU General Public License v3.0 4 votes vote down vote up
private void transformEdges(Set<AbstractEdge> edgeSet)
{
	List<String> highAnnotations;
	List<String> mediumAnnotations;
	List<String> lowAnnotations;
	List<String> commonAnnotations;
	highAnnotations = highMap.get(EDGE);
	mediumAnnotations = mediumMap.get(EDGE);
	lowAnnotations = lowMap.get(EDGE);
	commonAnnotations = (List<String>) CollectionUtils.intersection(highAnnotations, mediumAnnotations);
	commonAnnotations = (List<String>) CollectionUtils.intersection(commonAnnotations, lowAnnotations);
	highAnnotations = (List<String>) CollectionUtils.disjunction(commonAnnotations, highAnnotations);
	mediumAnnotations = (List<String>) CollectionUtils.disjunction(commonAnnotations, mediumAnnotations);
	lowAnnotations = (List<String>) CollectionUtils.disjunction(commonAnnotations, lowAnnotations);

	for(AbstractEdge edge : edgeSet)
	{
		// encrypt non-common annotations here
		switch(encryptionLevel)
		{
			case HIGH:
				encryptAnnotations(edge, highAnnotations, this.cipher.high);
			case MEDIUM:
				encryptAnnotations(edge, mediumAnnotations, this.cipher.medium);
			case LOW:
				encryptAnnotations(edge, lowAnnotations, this.cipher.low);
		}

		// encrypt common annotations here
		for(String annotation : commonAnnotations)
		{
			if(annotation.equals(OPMConstants.EDGE_TIME))
			{
				// extract time details from unix time
				String time = edge.getAnnotation(OPMConstants.EDGE_TIME);
				Date date = new Date(Double.valueOf(Double.parseDouble(time) * 1000).longValue());
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				String year = String.valueOf(calendar.get(Calendar.YEAR));
				String month = String.valueOf(calendar.get(Calendar.MONTH) + 1); // zero-based indexing
				String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
				String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
				String minute = String.valueOf(calendar.get(Calendar.MINUTE));
				String second = String.valueOf(calendar.get(Calendar.SECOND));
				String millisecond = String.valueOf(calendar.get(Calendar.MILLISECOND));

				switch(encryptionLevel)
				{
					case HIGH:
						// encrypt time
						day = encryptAnnotation(day, this.cipher.high);

					case MEDIUM:
						// encrypt time
						hour = encryptAnnotation(hour, this.cipher.medium);

					case LOW:
						// encrypt time
						minute = encryptAnnotation(minute, this.cipher.low);
						second = encryptAnnotation(second, this.cipher.low);
						millisecond = encryptAnnotation(millisecond, this.cipher.low);

						// stitch time with format is 'yyyy-MM-dd HH:mm:ss.SSS'
						String timestamp = year + "-" + month + "-" + day + " " + hour + ":" +
								minute + ":" + second + "." + millisecond;
						edge.addAnnotation(OPMConstants.EDGE_TIME, timestamp);
				}
			}
		}
	}
}
 
Example 15
Source File: StreamRouterBolt.java    From eagle with Apache License 2.0 4 votes vote down vote up
/**
 * Compare with metadata snapshot cache to generate diff like added, removed and modified between different versions.
 *
 * @param spec
 */
@SuppressWarnings("unchecked")
@Override
public synchronized void onStreamRouteBoltSpecChange(RouterSpec spec, Map<String, StreamDefinition> sds) {
    sanityCheck(spec);

    // figure out added, removed, modified StreamSortSpec
    Map<StreamPartition, StreamSortSpec> newSSS = spec.makeSSS();

    Set<StreamPartition> newStreamIds = newSSS.keySet();
    Set<StreamPartition> cachedStreamIds = cachedSSS.keySet();
    Collection<StreamPartition> addedStreamIds = CollectionUtils.subtract(newStreamIds, cachedStreamIds);
    Collection<StreamPartition> removedStreamIds = CollectionUtils.subtract(cachedStreamIds, newStreamIds);
    Collection<StreamPartition> modifiedStreamIds = CollectionUtils.intersection(newStreamIds, cachedStreamIds);

    Map<StreamPartition, StreamSortSpec> added = new HashMap<>();
    Map<StreamPartition, StreamSortSpec> removed = new HashMap<>();
    Map<StreamPartition, StreamSortSpec> modified = new HashMap<>();
    addedStreamIds.forEach(s -> added.put(s, newSSS.get(s)));
    removedStreamIds.forEach(s -> removed.put(s, cachedSSS.get(s)));
    modifiedStreamIds.forEach(s -> {
        if (!newSSS.get(s).equals(cachedSSS.get(s))) { // this means StreamSortSpec is changed for one specific streamId
            modified.put(s, newSSS.get(s));
        }
    });
    if (LOG.isDebugEnabled()) {
        LOG.debug("added StreamSortSpec " + added);
        LOG.debug("removed StreamSortSpec " + removed);
        LOG.debug("modified StreamSortSpec " + modified);
    }
    router.onStreamSortSpecChange(added, removed, modified);
    // switch cache
    cachedSSS = newSSS;

    // figure out added, removed, modified StreamRouterSpec
    Map<StreamPartition, List<StreamRouterSpec>> newSRS = spec.makeSRS();

    Set<StreamPartition> newStreamPartitions = newSRS.keySet();
    Set<StreamPartition> cachedStreamPartitions = cachedSRS.keySet();

    Collection<StreamPartition> addedStreamPartitions = CollectionUtils.subtract(newStreamPartitions, cachedStreamPartitions);
    Collection<StreamPartition> removedStreamPartitions = CollectionUtils.subtract(cachedStreamPartitions, newStreamPartitions);
    Collection<StreamPartition> modifiedStreamPartitions = CollectionUtils.intersection(newStreamPartitions, cachedStreamPartitions);

    Collection<StreamRouterSpec> addedRouterSpecs = new ArrayList<>();
    Collection<StreamRouterSpec> removedRouterSpecs = new ArrayList<>();
    Collection<StreamRouterSpec> modifiedRouterSpecs = new ArrayList<>();
    addedStreamPartitions.forEach(s -> addedRouterSpecs.addAll(newSRS.get(s)));
    removedStreamPartitions.forEach(s -> removedRouterSpecs.addAll(cachedSRS.get(s)));
    modifiedStreamPartitions.forEach(s -> {
        if (!CollectionUtils.isEqualCollection(newSRS.get(s), cachedSRS.get(s))) { // this means StreamRouterSpec is changed for one specific StreamPartition
            modifiedRouterSpecs.addAll(newSRS.get(s));
        }
    });

    if (LOG.isDebugEnabled()) {
        LOG.debug("added StreamRouterSpec " + addedRouterSpecs);
        LOG.debug("removed StreamRouterSpec " + removedRouterSpecs);
        LOG.debug("modified StreamRouterSpec " + modifiedRouterSpecs);
    }

    routeCollector.onStreamRouterSpecChange(addedRouterSpecs, removedRouterSpecs, modifiedRouterSpecs, sds);
    // switch cache
    cachedSRS = newSRS;
    sdf = sds;
    specVersion = spec.getVersion();
}
 
Example 16
Source File: AlertBolt.java    From eagle with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public synchronized void onAlertBoltSpecChange(AlertBoltSpec spec, Map<String, StreamDefinition> sds) {
    List<PolicyDefinition> newPolicies = spec.getBoltPoliciesMap().get(boltId);
    if (newPolicies == null) {
        LOG.info("no new policy with AlertBoltSpec {} for this bolt {}", spec, boltId);
        return;
    }

    Map<String, PolicyDefinition> newPoliciesMap = new HashMap<>();
    newPolicies.forEach(p -> newPoliciesMap.put(p.getName(), p));
    MapComparator<String, PolicyDefinition> comparator = new MapComparator<>(newPoliciesMap, cachedPolicies);
    comparator.compare();

    MapComparator<String, StreamDefinition> streamComparator = new MapComparator<>(sds, sdf);
    streamComparator.compare();

    List<StreamDefinition> addOrUpdatedStreams = streamComparator.getAdded();
    addOrUpdatedStreams.addAll(streamComparator.getModified());
    List<PolicyDefinition> cachedPoliciesTemp = new ArrayList<>(cachedPolicies.values());
    addOrUpdatedStreams.forEach(s -> {
        cachedPoliciesTemp.stream().filter(p -> p.getInputStreams().contains(s.getStreamId())
            || p.getOutputStreams().contains(s.getStreamId())).forEach(p -> {
                if (comparator.getModified().stream().filter(x -> x.getName().equals(p.getName())).count() <= 0
                    && comparator.getAdded().stream().filter(x -> x.getName().equals(p.getName())).count() <= 0) {
                    comparator.getModified().add(p);
                }
            });
        ;
    });

    policyGroupEvaluator.onPolicyChange(spec.getVersion(), comparator.getAdded(), comparator.getRemoved(), comparator.getModified(), sds);

    // update alert output collector
    Set<PublishPartition> newPublishPartitions = new HashSet<>();
    spec.getPublishPartitions().forEach(p -> {
        if (newPolicies.stream().filter(o -> o.getName().equals(p.getPolicyId())).count() > 0) {
            newPublishPartitions.add(p);
        }
    });

    Collection<PublishPartition> addedPublishPartitions = CollectionUtils.subtract(newPublishPartitions, cachedPublishPartitions);
    Collection<PublishPartition> removedPublishPartitions = CollectionUtils.subtract(cachedPublishPartitions, newPublishPartitions);
    Collection<PublishPartition> modifiedPublishPartitions = CollectionUtils.intersection(newPublishPartitions, cachedPublishPartitions);

    LOG.debug("added PublishPartition " + addedPublishPartitions);
    LOG.debug("removed PublishPartition " + removedPublishPartitions);
    LOG.debug("modified PublishPartition " + modifiedPublishPartitions);

    alertOutputCollector.onAlertBoltSpecChange(addedPublishPartitions, removedPublishPartitions, modifiedPublishPartitions);

    // switch
    cachedPolicies = newPoliciesMap;
    cachedPublishPartitions = newPublishPartitions;
    sdf = sds;
    specVersion = spec.getVersion();
    this.spec = spec;
}
 
Example 17
Source File: SequentialCachingAlgorithmTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void createStartingSets() throws Exception {

	// -------------------------------------------------
	// Fill the set of values for the GENDER associations
	Set<String> gender_d1 = new HashSet<String>();
	rs_d1 = stmt.executeQuery("SELECT DISTINCT gender FROM " + D1);
	while (rs_d1.next()) {
		gender_d1.add(rs_d1.getString("gender"));
	}
	Set<String> gender_d3 = new HashSet<String>();
	rs_d2 = stmt.executeQuery("SELECT DISTINCT gender FROM " + D2);
	while (rs_d2.next()) {
		gender_d3.add(rs_d2.getString("gender"));
	}
	Set<String> gender_intersection = new HashSet<String>(CollectionUtils.intersection(gender_d1, gender_d3));
	associationValues.put("GENDER", gender_intersection);
	// -------------------------------------------------

	// -------------------------------------------------
	// Fill the set of values for the EDUCATION associations
	Set<String> education_d1 = new HashSet<String>();
	rs_d1 = stmt.executeQuery("SELECT DISTINCT education FROM " + D1);
	while (rs_d1.next()) {
		education_d1.add(rs_d1.getString("education"));
	}
	Set<String> education_d3 = new HashSet<String>();
	rs_d3 = stmt.executeQuery("SELECT DISTINCT education FROM " + D3);
	while (rs_d3.next()) {
		education_d3.add(rs_d3.getString("education"));
	}
	Set<String> education_intersection = new HashSet<String>(CollectionUtils.intersection(education_d1, education_d3));
	associationValues.put("EDUCATION", education_intersection);
	// -------------------------------------------------

	// -------------------------------------------------
	// Fill the set of values for the STATE associations
	Set<String> state_d1 = new HashSet<String>();
	rs_d1 = stmt.executeQuery("SELECT DISTINCT state_province FROM " + D1);
	while (rs_d1.next()) {
		state_d1.add(rs_d1.getString("state_province"));
	}
	Set<String> state_d3 = new HashSet<String>();
	rs_d3 = stmt.executeQuery("SELECT DISTINCT state_province FROM " + D3);
	while (rs_d3.next()) {
		state_d3.add(rs_d3.getString("state_province"));
	}
	Set<String> state_intersection = new HashSet<String>(CollectionUtils.intersection(state_d1, state_d3));
	associationValues.put("STATE", state_intersection);
	// -------------------------------------------------
}
 
Example 18
Source File: DynamicPolicyLoader.java    From eagle with Apache License 2.0 4 votes vote down vote up
/**
 * When it is run at the first time, due to cachedPolicies being empty, all existing policies are expected
 * to be addedPolicies.
 */
@SuppressWarnings("unchecked")
@Override
public void run() {
    // we should catch every exception to avoid zombile thread
    try {
        final Stopwatch watch = Stopwatch.createStarted();
        LOG.info("Starting to load policies");
        List<PolicyDefinition> current = client.listPolicies();
        Map<String, PolicyDefinition> currPolicies = new HashMap<>();
        current.forEach(pe -> currPolicies.put(pe.getName(), pe));

        Collection<String> addedPolicies = CollectionUtils.subtract(currPolicies.keySet(), cachedPolicies.keySet());
        Collection<String> removedPolicies = CollectionUtils.subtract(cachedPolicies.keySet(), currPolicies.keySet());
        Collection<String> potentiallyModifiedPolicies = CollectionUtils.intersection(currPolicies.keySet(), cachedPolicies.keySet());

        List<String> reallyModifiedPolicies = new ArrayList<>();
        for (String updatedPolicy : potentiallyModifiedPolicies) {
            if (currPolicies.get(updatedPolicy) != null
                    && !currPolicies.get(updatedPolicy).equals(cachedPolicies.get(updatedPolicy))) {
                reallyModifiedPolicies.add(updatedPolicy);
            }
        }

        boolean policyChanged = false;
        if (addedPolicies.size() != 0
            || removedPolicies.size() != 0
            || reallyModifiedPolicies.size() != 0) {
            policyChanged = true;
        }

        if (!policyChanged) {
            LOG.info("No policy (totally {}) changed since last round", current.size());
            return;
        }

        synchronized (this) {
            for (PolicyChangeListener listener : listeners) {
                listener.onPolicyChange(current, addedPolicies, removedPolicies, reallyModifiedPolicies);
            }
        }

        watch.stop();

        LOG.info("Finished loading {} policies, added: {}, removed: {}, modified: {}, taken: {} ms",
            current.size(), addedPolicies.size(), removedPolicies.size(), potentiallyModifiedPolicies.size(), watch.elapsed(TimeUnit.MILLISECONDS));
        // reset cached policies
        cachedPolicies = currPolicies;
    } catch (Throwable t) {
        LOG.warn("Error loading policy, but continue to run", t);
    }
}
 
Example 19
Source File: IntersectionTest.java    From amodeus with GNU General Public License v2.0 3 votes vote down vote up
@Test
public void test() {

    List<Integer> c1 = Arrays.asList(1, 2, 3, 3);
    List<Integer> c2 = Arrays.asList(3, 4, 5);

    /** function is used in {@link DualSideSearch} */
    Collection<Integer> intersection = CollectionUtils.intersection(c1, c2);

    Assert.assertTrue(intersection.size() == 1);
    Assert.assertTrue(intersection.contains(3));

}