Example usage for org.apache.commons.collections Closure Closure

List of usage examples for org.apache.commons.collections Closure Closure

Introduction

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

Prototype

Closure

Source Link

Usage

From source file:edu.harvard.med.screensaver.util.CollectionUtils.java

/**
 * Indexes a collection by creating a map that allows each element of the
 * specified collection to be looked up by its key. The key of each element is
 * determined by calling the <code>makeKey</code> Transformer on that
 * element. I sure miss Lisp./*from  ww  w  . ja  v  a  2s  .  c o m*/
 *
 * @return a Map
 */
public static <K, E> Map<K, E> indexCollection(Collection c, final Transformer getKey, Class<K> keyClass,
        Class<E> elementClass) {
    final Map<K, E> map = new HashMap<K, E>(c.size());
    org.apache.commons.collections.CollectionUtils.forAllDo(c, new Closure() {
        @SuppressWarnings("unchecked")
        public void execute(Object e) {
            map.put((K) getKey.transform(e), (E) e);
        }
    });
    return map;
}

From source file:com.nwea.samples.apachecommons.collections.SampleCollectionUtilsTest.java

/**
 * @param maleStudents//from   w w  w  . j  a  v  a2s.  c o m
 */
private void printStudents(Collection<Student> students) {
    CollectionUtils.forAllDo(students, new Closure() {
        public void execute(Object arg0) {
            out.println(arg0.toString());
        }
    });
}

From source file:com.prodyna.bmw.server.aircraft.service.AircraftBusinessServiceBean.java

@Override
public List<Aircraft> readAircraftsForPilot(String pilotId) {
    List<PilotLicense> licnesesForPilot = pilotLicenseService.readLicensesForPilot(pilotId);

    final List<Aircraft> aircrafts = new ArrayList<Aircraft>();

    CollectionUtils.forAllDo(licnesesForPilot, new Closure() {
        @Override//from   w w  w. j av a  2 s .  c  o m
        public void execute(Object arg0) {
            PilotLicense license = (PilotLicense) arg0;
            List<Aircraft> aircraftsForType = aircraftService
                    .getAircraftsForType(license.getAircraftType().getId());
            aircrafts.addAll(aircraftsForType);
        }
    });

    return aircrafts;
}

From source file:com.switchfly.inputvalidation.CobrandNameValidationStrategyTest.java

@Test
public void testValidateStrategy() throws Exception {
    assertEquals("", ValidationStrategy.COBRAND_NAME.validateStrategy().validate(""));
    assertEquals(" ", ValidationStrategy.COBRAND_NAME.validateStrategy().validate(" "));
    assertEquals(null, ValidationStrategy.COBRAND_NAME.validateStrategy().validate(null));
    assertEquals("foo-Bar_123", ValidationStrategy.COBRAND_NAME.validateStrategy().validate("foo-Bar_123"));

    assertThrowException(ValidatorException.class, new Closure() {
        @Override/*from w w w .ja v  a  2 s .c  o  m*/
        public void execute(Object input) {
            ValidationStrategy.COBRAND_NAME.validateStrategy().validate("../../../../../etc/passwd%00");
        }
    });

    assertThrowException(ValidatorException.class, new Closure() {
        @Override
        public void execute(Object input) {
            ValidationStrategy.COBRAND_NAME.validateStrategy().validate("../../../../../etc/passwd%00");
        }
    });

    assertThrowException(ValidatorException.class, new Closure() {
        @Override
        public void execute(Object input) {
            ValidationStrategy.COBRAND_NAME.validateStrategy().validate("../../../../../etc/passwd%00foo");
        }
    });

    assertThrowException(ValidatorException.class, new Closure() {
        @Override
        public void execute(Object input) {
            ValidationStrategy.COBRAND_NAME.validateStrategy()
                    .validate("default%00%<script>alert('hacked')</script>");
        }
    });
}

From source file:com.stratumsoft.xmlgen.SchemaUtilTest.java

@Test
public void testGetNames() throws Exception {
    final InputStream is = getClass().getResourceAsStream(personalXsd);
    assertNotNull(is);//  ww w.j a  v  a 2 s. c  om

    final StreamSource source = new StreamSource(is);
    final XmlSchema schema = new XmlSchemaCollection().read(source);
    assertNotNull(schema);

    final Collection<QName> elements = SchemaUtil.getElements(schema);
    assertNotNull(elements);
    assertFalse(elements.isEmpty());

    System.out.println("Got the following elements from schema:");
    CollectionUtils.forAllDo(elements, new Closure() {
        @Override
        public void execute(Object input) {
            System.out.println(input);
        }
    });

}

From source file:com.newlandframework.avatarmq.broker.ProducerMessageHook.java

private void filterByTopic(String topic) {
    Predicate focusAllPredicate = new Predicate() {
        public boolean evaluate(Object object) {
            ConsumerClusters clusters = (ConsumerClusters) object;
            return clusters.findSubscriptionData(topic) != null;
        }/*from  w  w w  .  j  a  v a 2s. c  om*/
    };

    AnyPredicate any = new AnyPredicate(new Predicate[] { focusAllPredicate });

    Closure joinClosure = new Closure() {
        public void execute(Object input) {
            if (input instanceof ConsumerClusters) {
                ConsumerClusters clusters = (ConsumerClusters) input;
                clustersSet.add(clusters);
            }
        }
    };

    Closure ignoreClosure = new Closure() {
        public void execute(Object input) {
        }
    };

    Closure ifClosure = ClosureUtils.ifClosure(any, joinClosure, ignoreClosure);

    CollectionUtils.forAllDo(focusTopicGroup, ifClosure);
}

From source file:de.hybris.platform.b2bacceleratorfacades.order.populators.B2BUserGroupEditPermissionsPopulator.java

protected CustomerData populateUserGroupsEditPermissions(final CustomerData customerData) {
    final List<B2BUserGroupData> customerPermissionGroups = customerData.getPermissionGroups();

    if (!customerPermissionGroups.isEmpty()) {
        final SearchPageData<B2BUserGroupModel> currentUserPermissionGroups = getB2bCommerceB2bUserGroupService()
                .getPagedB2BUserGroups(createPageableData());
        final List<B2BUserGroupModel> results = currentUserPermissionGroups.getResults();

        CollectionUtils.forAllDo(customerPermissionGroups, new Closure() {
            @Override/*from ww w .ja va2 s  .  co  m*/
            public void execute(final Object input) {
                final B2BUserGroupData b2bUserGroupData = (B2BUserGroupData) input;
                b2bUserGroupData.setEditable(isUserGroupAllowedToEditByCurrentUser(b2bUserGroupData, results));
            }
        });
    }
    return customerData;
}

From source file:com.architexa.diagrams.jdt.builder.ReloASTExtractor.java

public void removeAnnotations(List<Resource> srcResources) {
    CollectionUtils.forAllDo(srcResources, new Closure() {
        public void execute(Object arg0) {
            removeAnnotations(projectModel, (URI) arg0);
        }/*from   w ww . j  a  va2  s  . co  m*/
    });
}

From source file:com.stratumsoft.xmlgen.SchemaUtilTest.java

@Test
public void testGetNamesFromSchemaWithImport() throws Exception {
    final InputStream is = getClass().getResourceAsStream(elementFormXsd);
    assertNotNull(is);/*from w ww.j av a 2 s  .  c  om*/

    final StreamSource source = new StreamSource(is);
    final XmlSchemaCollection schemaColl = new XmlSchemaCollection();
    final URL baseUrl = SchemaUtilTest.class.getResource(elementFormXsd);
    final String baseUri = baseUrl.toURI().toString();

    schemaColl.setBaseUri(baseUri);

    final XmlSchema schema = schemaColl.read(source);
    assertNotNull(schema);

    System.out.println("There are " + schemaColl.getXmlSchemas().length + " schemas present");

    final Collection<QName> elements = SchemaUtil.getElements(schema);
    assertNotNull(elements);
    assertFalse(elements.isEmpty());

    System.out.println("Got the following elements from schema:");
    CollectionUtils.forAllDo(elements, new Closure() {
        @Override
        public void execute(Object input) {
            System.out.println(input);
        }
    });

}

From source file:net.sf.wickedshell.domain.command.CommandDescriptorDao.java

/**
 * Persist the <code>List</code> of <code>ICommandDescriptor</code>.
 * //from  w ww .  ja v  a  2 s  .co m
 * @param commandDescriptors
 *            the <code>List</code> of <code>ICommandDescriptor</code>
 */
public void writeCommandHistory(List<ICommandDescriptor> commandDescriptors) throws IOException {
    String stateLocation = DomainPlugin.getDefault().getStateLocation().toOSString();
    File commandHistoryListDocumentFile = new File(stateLocation, DomainID.COMMAND_HISTORY_FILE);
    if (!commandHistoryListDocumentFile.exists()) {
        commandHistoryListDocumentFile.delete();
    }
    final CommandHistoryListDocument commandHistoryListDocument = CommandHistoryListDocument.Factory
            .newInstance();
    commandHistoryListDocument.addNewCommandHistoryList();
    CollectionUtils.forAllDo(commandDescriptors, new Closure() {
        /**
         * @see org.apache.commons.collections.Closure#execute(java.lang.Object)
         */
        public void execute(Object object) {
            ICommandDescriptor commandFileDescriptor = (ICommandDescriptor) object;
            CommandHistoryList commandHistoryList = commandHistoryListDocument.getCommandHistoryList();
            CommandDescriptor staticCommandDescriptor = commandHistoryList.addNewCommandDescriptor();
            // The command is stored in XML, so we need to remove ALL
            // invalid XML characters
            char[] commandCharacters = commandFileDescriptor.getCommand().toCharArray();
            StringBuffer validXMLCharacters = new StringBuffer();
            for (int charIndex = 0; charIndex < commandCharacters.length; charIndex++) {
                if (XMLChar.isValid(commandCharacters[charIndex])) {
                    validXMLCharacters.append(commandCharacters[charIndex]);
                }
            }
            staticCommandDescriptor.setCommand(validXMLCharacters.toString());
            staticCommandDescriptor.setShellDescriptorId(commandFileDescriptor.getShellDescriptorId());
        }
    });
    commandHistoryListDocument.save(commandHistoryListDocumentFile);
}