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

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

Introduction

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

Prototype

public static boolean isEqualCollection(final Collection a, final Collection b) 

Source Link

Document

Returns true iff the given Collection s contain exactly the same elements with exactly the same cardinalities.

Usage

From source file:org.orbeon.oxf.processor.pipeline.ast.ASTNodeContainer.java

public IdInfo getIdInfo() {
    class IdInfoASTHandler extends ASTSimpleHandler {

        final IdInfo idInfo = new IdInfo();

        /**//w ww . j a v a 2 s. com
         * Returns the id collected after this walked went through an AST.
         */
        public IdInfo getIdInfo() {
            return idInfo;
        }

        public void hrefId(ASTHrefId hrefId) {
            idInfo.getInputRefs().add(hrefId.getId());
        }

        public void output(ASTOutput output) {
            if (output.getId() != null)
                idInfo.getOutputIds().add(output.getId());
            if (output.getRef() != null) {
                if (idInfo.getOutputRefs().contains(output.getRef()))
                    throw new ValidationException(
                            "Output id '" + output.getRef() + "' can be referenced only once",
                            output.getLocationData());
                idInfo.getOutputRefs().add(output.getRef());
            }
        }

        public boolean startChoose(ASTChoose choose) {
            choose.getHref().walk(this);

            // Get idInfo for each branch
            final IdInfo[] whenIdInfos;
            {
                List whens = choose.getWhen();
                whenIdInfos = new IdInfo[whens.size()];
                int count = 0;
                for (Iterator i = whens.iterator(); i.hasNext();) {
                    ASTWhen astWhen = (ASTWhen) i.next();
                    IdInfoASTHandler idInfoASTHandler = new IdInfoASTHandler();
                    astWhen.walk(idInfoASTHandler);
                    whenIdInfos[count] = idInfoASTHandler.getIdInfo();
                    Collection intersection = CollectionUtils.intersection(whenIdInfos[count].getInputRefs(),
                            whenIdInfos[count].getOutputIds());
                    whenIdInfos[count].getInputRefs().removeAll(intersection);
                    whenIdInfos[count].getOutputIds().removeAll(intersection);
                    count++;
                }
            }

            // Make sure the output ids and output refs are the same for every branch
            if (whenIdInfos.length > 1) {
                for (int i = 1; i < whenIdInfos.length; i++) {
                    if (!CollectionUtils.isEqualCollection(whenIdInfos[0].getOutputIds(),
                            whenIdInfos[i].getOutputIds()))
                        throw new ValidationException("ASTChoose branch number " + (i + 1)
                                + " does not declare the same ids " + whenIdInfos[0].getOutputIds().toString()
                                + " as the previous branches " + whenIdInfos[i].getOutputIds().toString(),
                                choose.getLocationData());
                    if (!CollectionUtils.isEqualCollection(whenIdInfos[0].getOutputRefs(),
                            whenIdInfos[i].getOutputRefs()))
                        throw new ValidationException("ASTChoose branch number " + (i + 1)
                                + " does not declare the same ids " + whenIdInfos[0].getOutputRefs().toString()
                                + " as the previous branches " + whenIdInfos[i].getOutputRefs().toString(),
                                choose.getLocationData());
                }
            }

            // Add ids from all the branches
            for (int i = 0; i < whenIdInfos.length; i++)
                idInfo.getInputRefs().addAll(whenIdInfos[i].getInputRefs());

            // Add output ids and output refs from first branch (they are the same for each branch)
            idInfo.getOutputIds().addAll(whenIdInfos[0].getOutputIds());
            idInfo.getOutputRefs().addAll(whenIdInfos[0].getOutputRefs());

            return false;
        }

        public boolean startForEach(ASTForEach forEach) {
            // Add contribution from <p:for-each> attributes
            forEach.getHref().walk(this);
            if (forEach.getRef() != null)
                idInfo.getOutputRefs().add(forEach.getRef());
            if (forEach.getId() != null)
                idInfo.getOutputIds().add(forEach.getId());
            forEach.getHref().walk(this);

            // Collect idInfo for all the statements
            final IdInfo statementsIdInfo;
            {
                IdInfoASTHandler statementsIdInfoASTHandler = new IdInfoASTHandler();
                statementsIdInfoASTHandler.getIdInfo().getOutputIds()
                        .add(AbstractForEachProcessor.FOR_EACH_CURRENT_INPUT);
                for (Iterator i = forEach.getStatements().iterator(); i.hasNext();) {
                    ASTStatement statement = (ASTStatement) i.next();
                    statement.walk(statementsIdInfoASTHandler);
                }
                statementsIdInfo = statementsIdInfoASTHandler.getIdInfo();
                Collection intersection = CollectionUtils.intersection(statementsIdInfo.getInputRefs(),
                        statementsIdInfo.getOutputIds());
                statementsIdInfo.getInputRefs().removeAll(intersection);
                statementsIdInfo.getOutputIds().removeAll(intersection);
            }

            // Check the refs
            if (forEach.getId() == null && forEach.getRef() == null) {
                if (statementsIdInfo.getOutputRefs().size() != 0)
                    throw new ValidationException(
                            "The statements in a <for-each> cannot have output ref; they reference "
                                    + statementsIdInfo.getOutputRefs().toString(),
                            forEach.getLocationData());
            } else if (statementsIdInfo.getOutputRefs().size() != 1) {
                throw new ValidationException(
                        "The statements in a <for-each> must have exactly one output ref; "
                                + (statementsIdInfo.getOutputRefs().isEmpty() ? "this <for-each> has none"
                                        : "this <for-each> defined: "
                                                + statementsIdInfo.getOutputRefs().toString()),
                        forEach.getLocationData());
            } else {
                String statementsRef = (String) statementsIdInfo.getOutputRefs().iterator().next();
                if (forEach.getId() != null) {
                    if (!forEach.getId().equals(statementsRef))
                        throw new ValidationException("The statements in a <for-each> referenced the id '"
                                + statementsRef + "' but the id declared on the <for-each> is '"
                                + forEach.getId() + "'", forEach.getLocationData());
                } else if (!forEach.getRef().equals(statementsRef)) {
                    throw new ValidationException(
                            "The statements in a <for-each> referenced the id '" + statementsRef
                                    + "' but the ref declared on the <for-each> is '" + forEach.getRef() + "'",
                            forEach.getLocationData());
                }
            }

            // Add ids referenced inside <for-each>
            idInfo.getInputRefs().addAll(statementsIdInfo.getInputRefs());

            return false;
        }
    }

    // Run the handler declared above on this node
    IdInfoASTHandler idInfoASTHandler = new IdInfoASTHandler();
    walk(idInfoASTHandler);
    return idInfoASTHandler.getIdInfo();
}

From source file:org.orbeon.oxf.processor.pipeline.choose.AbstractChooseProcessor.java

public Processor createInstance() {

    // We store here the "refs with no id" and "ids with no ref" for each branch.
    // Those are list of collections (one collection for each branch).
    final List refsWithNoId = new ArrayList();
    final List idsWithNoRef = new ArrayList();
    final List paramRefs = new ArrayList();

    for (Iterator astIterator = chooseAST.getWhen().iterator(); astIterator.hasNext();) {

        // Get info about id used in this branch
        final ASTWhen when = (ASTWhen) astIterator.next();
        IdInfo idInfo = when.getIdInfo();
        paramRefs.add(idInfo.getOutputRefs());

        // Determine all <p:input ref="..."> with no <p:output id="...">.
        // Those are the inputs of this processor.
        final Set branchRefsWithNoId = new HashSet(idInfo.getInputRefs());
        branchRefsWithNoId.removeAll(idInfo.getOutputIds());
        refsWithNoId.add(branchRefsWithNoId);

        // Determine all <p:output id="..."> with no <p:input ref="...">.
        // Those are the outputs of this processor.
        final Set branchIdsWithNoRef = new HashSet(idInfo.getOutputIds());
        branchIdsWithNoRef.removeAll(idInfo.getInputRefs());
        idsWithNoRef.add(branchIdsWithNoRef);
    }/* w w  w.  jav a  2 s  .c  o m*/

    // Make sure that the "ids with no ref" are the same for each branch
    if (idsWithNoRef.size() > 1) {
        final Collection firstBranchIdsWithNoRef = (Collection) idsWithNoRef.get(0);
        int branchId = 0;
        for (Iterator i = idsWithNoRef.iterator(); i.hasNext();) {
            branchId++;
            final Collection branchIdsWithNoRef = (Collection) i.next();
            if (branchIdsWithNoRef != firstBranchIdsWithNoRef
                    && !CollectionUtils.isEqualCollection(branchIdsWithNoRef, firstBranchIdsWithNoRef))
                throw new ValidationException("ASTChoose branch number " + branchId
                        + " does not declare the same ids " + branchIdsWithNoRef.toString()
                        + " as the previous branches " + firstBranchIdsWithNoRef.toString(), getLocationData());
        }
    }

    // Make sure that the "param ref" are the same for each branch
    if (paramRefs.size() > 1) {
        final Collection firstBranchParamRefs = (Collection) paramRefs.get(0);
        int branchId = 0;
        for (Iterator i = paramRefs.iterator(); i.hasNext();) {
            branchId++;
            final Collection branchParamRefs = (Collection) i.next();
            if (branchParamRefs != firstBranchParamRefs
                    && !CollectionUtils.isEqualCollection(branchParamRefs, firstBranchParamRefs))
                throw new ValidationException("ASTChoose branch number " + branchId
                        + " does not declare the same refs " + branchParamRefs.toString()
                        + " as the previous branches " + firstBranchParamRefs.toString(), getLocationData());
        }
    }

    // Compute the union of "refs with no id" for all the branches
    final Set allRefsWithNoId = new HashSet();
    for (Iterator i = refsWithNoId.iterator(); i.hasNext();)
        allRefsWithNoId.addAll((Set) i.next());

    // Create the list of inputs based on allRefsWithNoId
    final List astParams = new ArrayList();
    for (int i = 0; i < 2; i++) {
        final Set parameters;
        if (i == 0) {
            parameters = allRefsWithNoId;
        } else {
            parameters = new HashSet();
            parameters.addAll((Set) idsWithNoRef.get(0));
            parameters.addAll((Set) paramRefs.get(0));
        }

        for (Iterator j = parameters.iterator(); j.hasNext();) {
            final String paramName = (String) j.next();
            ASTParam astParam = new ASTParam();
            astParam.setType(i == 0 ? ASTParam.INPUT : ASTParam.OUTPUT);
            astParam.setName(paramName);
            astParams.add(astParam);
        }
    }

    // For each branch, create a new pipeline processor
    final List<Processor> branchProcessors = new ArrayList();
    final List branchConditions = new ArrayList();
    final List<NamespaceMapping> branchNamespaces = new ArrayList<NamespaceMapping>();

    for (Iterator astIterator = chooseAST.getWhen().iterator(); astIterator.hasNext();) {
        final ASTWhen astWhen = (ASTWhen) astIterator.next();

        // Save condition
        branchConditions.add(astWhen.getTest());
        // Get namespaces declared at this point in the pipeline
        if (astWhen.getNode() != null && astWhen.getNamespaces().mapping.size() != 0) {
            throw new ValidationException("ASTWhen cannot have both a node and namespaces defined",
                    astWhen.getLocationData());
        }
        branchNamespaces.add(astWhen.getNode() != null
                ? new NamespaceMapping(Dom4jUtils.getNamespaceContextNoDefault((Element) astWhen.getNode()))
                : astWhen.getNamespaces());

        // Add an identity processor to connect the output of the branch to
        // the <param type="output"> of the pipeline
        final Set idsToConvert = (Set) idsWithNoRef.get(0);
        for (Iterator i = idsToConvert.iterator(); i.hasNext();) {
            final String id = (String) i.next();
            final ASTProcessorCall identityConnector = new ASTProcessorCall(
                    XMLConstants.IDENTITY_PROCESSOR_QNAME);
            {
                identityConnector.addInput(new ASTInput("data", new ASTHrefId(new ASTOutput(null, id))));
                final ASTParam outParam = new ASTParam(ASTParam.OUTPUT, id);
                final LocationData locDat = Dom4jUtils.getLocationData();
                final ASTOutput astOut = new ASTOutput("data", outParam);
                astOut.setLocationData(locDat);
                identityConnector.addOutput(astOut);
            }
            astWhen.addStatement(identityConnector);
        }

        final ASTPipeline astPipeline = new ASTPipeline();
        astPipeline.setValidity(validity);
        astPipeline.getParams().addAll(astParams);
        astPipeline.getStatements().addAll(astWhen.getStatements());
        astPipeline.setNode(astWhen.getNode());
        final Processor pipelineProcessor = new PipelineProcessor(astPipeline);
        if (getId() != null)
            pipelineProcessor.setId(getId() + "-branch" + branchProcessors.size());
        branchProcessors.add(pipelineProcessor);
    }

    return new ConcreteChooseProcessor(getId(), getLocationData(), branchConditions, branchNamespaces,
            branchProcessors, allRefsWithNoId, (Set) idsWithNoRef.get(0), (Set) paramRefs.get(0));
}

From source file:org.pentaho.test.platform.repository.subscription.SubscriptionRepositoryHelperTest.java

/**
 * verify that the expected items are in the schedule's content list, delete the last schedule
 * and verify that the content list is clean up.
 */// www. j  a va2s.  c  o m
public void verifyContentList() {
    ISubscriptionRepository subscriptionRepository = PentahoSystem.get(ISubscriptionRepository.class, session);
    List<ISubscribeContent> endContentList = subscriptionRepository.getAllContent();
    PentahoSystem.systemExitPoint();
    // lists are both detached, and modification of lists should not effect persistent store

    // create a set of the actionRefs that were added to the final schedule (for fast lookup)
    Set<String> actionRefEditSet = new HashSet<String>();
    for (String actionRef : actionRefsEdit) {
        actionRefEditSet.add(actionRef);
    }
    Set<String> actionRefStartSet = new HashSet<String>();
    for (ISubscribeContent content : startContentList) {
        actionRefStartSet.add(content.getActionReference());
    }
    Set<String> actionRefEndSet = new HashSet<String>();
    for (ISubscribeContent content : endContentList) {
        actionRefEndSet.add(content.getActionReference());
    }

    assertTrue("Content list does not contain expected content.", CollectionUtils
            .isEqualCollection(CollectionUtils.union(actionRefEditSet, actionRefStartSet), actionRefEndSet));
    //    assertTrue( "", CollectionUtils.xx() );
    //    assertTrue( "", CollectionUtils.xx() );
    //    assertTrue( "", CollectionUtils.xx() );

    // do the final delete

    int ii = scheduleNames.length - 1;
    String name = scheduleNames[ii] + "-cron"; //$NON-NLS-1$
    System.out.println("deleting: " + name); //$NON-NLS-1$
    try {
        subscriptionRepository = PentahoSystem.get(ISubscriptionRepository.class, session);
        ISchedule schedule = subscriptionRepository.getScheduleByScheduleReference(name); //$NON-NLS-1$
        if (null != schedule) {
            SubscriptionRepositoryHelper.deleteScheduleContentAndSubscription(subscriptionRepository, schedule);
        }
        PentahoSystem.systemExitPoint();
    } catch (Exception e) {
        assertTrue("Failed in call to deleteScheduleContentAndSubscription: " //$NON-NLS-1$
                + name + " " + e.getMessage(), false); //$NON-NLS-1$
    }

    // we have now removed our last schedule, so associated content should be clean up. Let's see...

    subscriptionRepository = PentahoSystem.get(ISubscriptionRepository.class, session);
    endContentList = subscriptionRepository.getAllContent();
    PentahoSystem.systemExitPoint();
    actionRefEndSet = new HashSet<String>();
    for (ISubscribeContent content : endContentList) {
        actionRefEndSet.add(content.getActionReference());
    }

    assertTrue("Content list does not contain expected content.",
            CollectionUtils.isEqualCollection(actionRefStartSet, actionRefEndSet));

}

From source file:org.restcomm.connect.commons.configuration.MgAsrConfigurationTest.java

@Test
public void checkMgAsrSection() throws ConfigurationException, MalformedURLException {
    MgAsrConfigurationSet conf = createCfg("/restcomm.xml").getMgAsr();

    assertTrue(CollectionUtils.isEqualCollection(expectedDrivers, conf.getDrivers()));
    assertEquals("driver1", conf.getDefaultDriver());
}

From source file:org.restcomm.connect.commons.configuration.MgAsrConfigurationTest.java

@Test
public void checkNoMgAsrSection() throws ConfigurationException, MalformedURLException {
    MgAsrConfigurationSet conf = createCfg("/restcomm-no-mg-asr.xml").getMgAsr();

    assertTrue(CollectionUtils.isEqualCollection(Collections.emptyList(), conf.getDrivers()));
    assertNull(conf.getDefaultDriver());
}

From source file:org.restcomm.connect.commons.configuration.MgAsrConfigurationTest.java

@Test
public void checkAsrLanguagesSection() throws ConfigurationException, MalformedURLException {
    MgAsrConfigurationSet conf = createCfg("/restcomm.xml").getMgAsr();

    assertTrue(CollectionUtils.isEqualCollection(expectedLanguages, conf.getLanguages()));
    assertEquals("en-US", conf.getDefaultLanguage());
}

From source file:org.sonar.java.checks.RedundantAbstractMethodCheck.java

private static boolean differentThrows(JavaSymbol.MethodJavaSymbol method,
        JavaSymbol.MethodJavaSymbol overridee) {
    return !CollectionUtils.isEqualCollection(method.thrownTypes(), overridee.thrownTypes());
}

From source file:org.sonar.server.configuration.MetricsBackupTest.java

@Test
public void shouldExportMetrics() {
    MetricsBackup metricsBackup = new MetricsBackup();
    SonarConfig sonarConfig = new SonarConfig();
    Collection<Metric> metrics = createMetrics();
    metricsBackup.exportXml(sonarConfig, metrics);

    assertTrue(CollectionUtils.isEqualCollection(sonarConfig.getMetrics(), metrics));
}

From source file:org.sonar.server.configuration.PropertiesBackupTest.java

@Test
public void shouldExportProperties() {
    setupData("shouldExportProperties");

    new PropertiesBackup(getSession()).exportXml(sonarConfig);

    Property prop1 = new Property("key1", "value1");
    Property prop2 = new Property("key2", "value2");

    assertTrue(CollectionUtils.isEqualCollection(sonarConfig.getProperties(), Arrays.asList(prop1, prop2)));
}

From source file:org.sonar.server.configuration.PropertiesBackupTest.java

@Test
public void shouldNotExportPropertiesLinkedToResources() {
    setupData("shouldNotExportPropertiesLinkedToResources");

    new PropertiesBackup(getSession()).exportXml(sonarConfig);

    Property prop1 = new Property("key1", "value1");
    Property prop2 = new Property("key2", "value2");

    assertTrue(CollectionUtils.isEqualCollection(sonarConfig.getProperties(), Arrays.asList(prop1, prop2)));
}