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

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

Introduction

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

Prototype

public static Collection collect(Iterator inputIterator, Transformer transformer) 

Source Link

Document

Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection.

Usage

From source file:sk.gymdb.thinis.gcm.web.SendAllMessagesServlet.java

private void asyncSend(List<Registration> partialDevices) {
    // make a copy
    final List<Registration> devices = new ArrayList<Registration>(partialDevices);
    threadPool.execute(new Runnable() {

        public void run() {
            Message message = new Message.Builder().build();
            MulticastResult multicastResult;
            try {
                multicastResult = sender.send(message,
                        (List<String>) CollectionUtils.collect(devices, new Transformer() {
                            @Override
                            public Object transform(Object o) {
                                return ((Registration) o).getId();
                            }/*from  w  ww  .j  a v  a2  s .  co  m*/
                        }), 5);
            } catch (IOException e) {
                logger.log(Level.SEVERE, "Error posting messages", e);
                return;
            }
            List<Result> results = multicastResult.getResults();
            // analyze the results
            for (int i = 0; i < devices.size(); i++) {
                //          String regId = devices.get(i);
                Registration registration = devices.get(i);
                Result result = results.get(i);
                String messageId = result.getMessageId();
                if (messageId != null) {
                    logger.fine("Succesfully sent message to device: " + registration.getId() + "; messageId = "
                            + messageId);
                    Registration canonicalRegId = new Registration(result.getCanonicalRegistrationId(),
                            registration.getClazz());
                    if (canonicalRegId != null) {
                        // same device has more than on registration id: update it
                        logger.info("canonicalRegId " + canonicalRegId);
                        Datastore.updateRegistration(registration, canonicalRegId);
                    }
                } else {
                    String error = result.getErrorCodeName();
                    if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                        // application has been removed from device - unregister it
                        logger.info("Unregistered device: " + registration.getId());
                        Datastore.unregister(registration);
                    } else {
                        logger.severe("Error sending message to " + registration.getId() + ": " + error);
                    }
                }
            }
        }
    });
}

From source file:tds.itemrenderer.webcontrols.PageLayout.java

@SuppressWarnings("unchecked")
protected void bindResponseControl_MC(IItemRender itemRender, ITSContent itsContent, UIComponent control) {
    final char delimiter = ',';

    // logic for figuring our answer template
    List<ItemRenderMCOption> renderOptions = new ArrayList<ItemRenderMCOption>();

    // check if there are any MC options
    if (itsContent.getOptions() != null) {
        for (ITSOption option : itsContent.getOptions()) {
            ItemRenderMCOption renderMCOption = new ItemRenderMCOption(
                    "Response_MC_" + itemRender.getPosition(), option.getKey());
            renderMCOption.setText(option.getValue());
            renderMCOption.setFeedback(option.getFeedback());
            renderMCOption.setSound(option.getSound());
            renderMCOption.setDisabled(itemRender.getDisabled());

            // get the answer key
            String answerString = itemRender.getItem().getAnswerKey();

            // set the correct answer on the option
            if (!StringUtils.isEmpty(answerString)) {
                String[] answerKeys = StringUtils.split(answerString, delimiter);

                for (String answerKey : answerKeys) {
                    // check if matching answer key
                    if (StringUtils.equals(option.getKey(), answerKey)) {
                        renderMCOption.setAnswer(true);
                        break;
                    }//from   w  w w. j av a  2s . c  o  m
                }
            }

            // check for a response
            if (!StringUtils.isEmpty(itemRender.getResponse())) {
                // check if multiple response
                if (StringUtils.equalsIgnoreCase(itemRender.getItem().getFormat(), "MS")) {
                    String[] responseKeys = StringUtils.split(itemRender.getResponse(), delimiter);

                    // go through all the response keys to see if they match option key
                    for (String responseKey : responseKeys) {
                        // check if matching response key
                        if (StringUtils.equals(option.getKey(), responseKey)) {
                            renderMCOption.setSelected(true);
                            break;
                        }
                    }
                } else {
                    // check if response key matches option key
                    renderMCOption.setSelected(StringUtils.equals(option.getKey(), itemRender.getResponse()));
                }
            }
            renderOptions.add(renderMCOption);
        }
    }

    // perform data binding
    if (control instanceof ITemplateAnswer) {
        ITemplateAnswer templateAnswer = (ITemplateAnswer) control;
        templateAnswer.setOptions(renderOptions);
    }
    // TODO Shiva: Finish the loops below
    /*
     * else if (control instanceof ITemplateAnswerMC) { ITemplateAnswerMC
     * templateAnswer = control as ITemplateAnswerMC;
     * templateAnswer.Options.DataSource = renderOptions;
     * templateAnswer.Options.DataBind(); }
     */
    else if (control instanceof ITemplateAnswerTable) {

        ITemplateAnswerTable templateAnswer = (ITemplateAnswerTable) control;
        templateAnswer.setOptions((List<Object>) CollectionUtils.collect(renderOptions, new Transformer() {
            @Override
            public Object transform(Object input) {
                return input;
            }
        }));
    }
    /*
     * else if (control is ITemplateAnswerRepeater) { ITemplateAnswerRepeater
     * templateAnswer = control as ITemplateAnswerRepeater;
     * templateAnswer.Options.DataSource = renderOptions;
     * templateAnswer.Options.DataBind(); }
     */
    else {
        addError(ErrorCategories.Template,
                "Cannot bind data to the answer template '{0}' because it inherits from unknown interface. Failed to load item {1}.",
                control.getClientId(), itemRender.getItem().getItemKey());
    }
}

From source file:uk.ac.diamond.scisoft.analysis.rcp.inspector.AxisSelection.java

/**
 * @return maximum order/*w w w  .ja  v a  2 s  .c  o  m*/
 */
@SuppressWarnings({ "unchecked" })
public int getMaxOrder() {
    List<Integer> orders = (List<Integer>) CollectionUtils.collect(asData, orderTransformer);
    return orders.size() > 0 ? (Integer) Collections.max(orders) : 0;
}