Example usage for org.apache.commons.collections15 CollectionUtils find

List of usage examples for org.apache.commons.collections15 CollectionUtils find

Introduction

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

Prototype

public static <E> E find(Iterable<E> iterable, Predicate<? super E> predicate) 

Source Link

Document

Finds the first element in the given iterable which matches the given predicate.

Usage

From source file:gov.nih.nci.cabig.caaers.service.synchronizer.report.ConcomitantMedicationSynchronizer.java

public void migrate(ExpeditedAdverseEventReport src, ExpeditedAdverseEventReport dest,
        DomainObjectImportOutcome<ExpeditedAdverseEventReport> outcome) {
    List<ConcomitantMedication> newConmeds = new ArrayList<ConcomitantMedication>();
    List<ConcomitantMedication> existingConmeds = new ArrayList<ConcomitantMedication>();
    if (dest.getConcomitantMedications() != null)
        existingConmeds.addAll(dest.getConcomitantMedications());

    if (src.getConcomitantMedications() != null) {
        for (ConcomitantMedication conMed : src.getConcomitantMedications()) {
            final ConcomitantMedication xmlConmed = conMed;
            ConcomitantMedication found = CollectionUtils.find(existingConmeds,
                    new Predicate<ConcomitantMedication>() {
                        public boolean evaluate(ConcomitantMedication concomitantMedication) {
                            return StringUtils.equals(concomitantMedication.getAgentName(),
                                    xmlConmed.getAgentName());
                        }/* w w  w  .  j  av a2  s . co  m*/
                    });
            if (found != null) {
                found.setEndDate(xmlConmed.getEndDate());
                found.setStartDate(xmlConmed.getStartDate());
                found.setStillTakingMedications(xmlConmed.getStillTakingMedications());
                //remove the pre-existing condition found
                existingConmeds.remove(found);
            } else {
                newConmeds.add(xmlConmed);
            }
        }
    }

    //remove unwanted
    for (ConcomitantMedication unwanted : existingConmeds) {
        dest.getConcomitantMedications().remove(unwanted);
    }

    //add newly added
    for (ConcomitantMedication newPc : newConmeds) {
        dest.addConcomitantMedication(newPc);
    }

}

From source file:ch.algotrader.entity.security.CombinationImpl.java

@Override
public Component getComponentBySecurity(final Security security) {

    // find the component to the specified security
    return CollectionUtils.find(getComponents(), new Predicate<Component>() {
        @Override/*from ww w.  jav a 2s.c om*/
        public boolean evaluate(Component component) {
            return security.equals(component.getSecurity());
        }
    });
}

From source file:gov.nih.nci.cabig.caaers.service.synchronizer.report.PreExistingConditionSynchronizer.java

public void migrate(ExpeditedAdverseEventReport src, ExpeditedAdverseEventReport dest,
        DomainObjectImportOutcome<ExpeditedAdverseEventReport> outcome) {
    List<SAEReportPreExistingCondition> newPreConditions = new ArrayList<SAEReportPreExistingCondition>();
    List<SAEReportPreExistingCondition> existingConditions = new ArrayList<SAEReportPreExistingCondition>();
    if (dest.getSaeReportPreExistingConditions() != null)
        existingConditions.addAll(dest.getSaeReportPreExistingConditions());

    if (src.getSaeReportPreExistingConditions() != null) {
        for (SAEReportPreExistingCondition pc : src.getSaeReportPreExistingConditions()) {
            final SAEReportPreExistingCondition xmlPreCondition = pc;
            SAEReportPreExistingCondition found = CollectionUtils.find(existingConditions,
                    new Predicate<SAEReportPreExistingCondition>() {
                        public boolean evaluate(SAEReportPreExistingCondition saeReportPreExistingCondition) {
                            if (saeReportPreExistingCondition.getPreExistingCondition() == null
                                    && xmlPreCondition.getPreExistingCondition() == null) {
                                return StringUtils.equals(saeReportPreExistingCondition.getOther(),
                                        xmlPreCondition.getOther());
                            }// w  ww  .  j ava 2s . c  om
                            return xmlPreCondition.getPreExistingCondition().getId()
                                    .equals(saeReportPreExistingCondition.getPreExistingCondition().getId());
                        }
                    });
            if (found != null) {
                //nothing to sync, just remove the pre-existing condition found
                existingConditions.remove(found);
            } else {
                newPreConditions.add(xmlPreCondition);
            }
        }
    }

    //remove unwanted
    for (SAEReportPreExistingCondition unwanted : existingConditions) {
        dest.getSaeReportPreExistingConditions().remove(unwanted);
    }

    //add newly added
    for (SAEReportPreExistingCondition newPc : newPreConditions) {
        dest.addSaeReportPreExistingCondition(newPc);
    }

}

From source file:ar.com.zauber.garfio.modules.jira.model.actions.FixInVersionAction.java

/** 
 * {@link AbstractIssueAction}{@link #preExecutionValidation(List)}
 * @throws MustNotExecuteException on error 
 *///www  . j a va 2  s  .c  o  m
public final void preExecutionValidation(final List<Action> allActions) throws MustNotExecuteException {
    if (!issue.isFixed()) {
        Action resolveIssueAction = CollectionUtils.find(allActions, new Predicate<Action>() {
            public boolean evaluate(final Action action) {
                return action instanceof ResolveIssueAction;
            }
        });
        if (resolveIssueAction == null) {
            throw new MustNotExecuteException("version specified to an issue that is not fixed. You "
                    + "must also use the resolved action");
        }
    }

    try {
        issue.getAvailableVersion(version);
    } catch (final NoSuchEntityException e) {
        throw new MustNotExecuteException("unknown version " + e.getEntity());
    }
}

From source file:ar.com.zauber.garfio.modules.mantis.model.actions.FixInVersionAction.java

/** 
 * {@link AbstractIssueAction}{@link #preExecutionValidation(List)}
 * @throws MustNotExecuteException on error 
 *//*from  w  w w.j av a  2 s  .c om*/
@Override
public final void preExecutionValidation(final List<Action> allActions) throws MustNotExecuteException {
    if (mantisIssue.isFixed()) {
        Action resolveIssueAction = CollectionUtils.find(allActions, new Predicate<Action>() {
            public boolean evaluate(final Action action) {
                return action instanceof ResolveIssueAction;
            }
        });
        if (resolveIssueAction == null) {
            throw new MustNotExecuteException("version specified to an issue that is not fixed. You "
                    + "must also use the resolved action");
        }
    }
}

From source file:ch.algotrader.service.CombinationServiceImpl.java

/**
 * {@inheritDoc}// ww w.  j a  v a 2s  .  c  o m
 */
@Override
@Transactional(propagation = Propagation.REQUIRED)
public Combination removeComponent(final long combinationId, final long securityId) {

    Combination combination = this.combinationDao.get(combinationId);

    if (combination == null) {
        throw new IllegalArgumentException("combination does not exist: " + combinationId);
    }

    String combinationString = combination.toString();

    if (this.securityDao.load(securityId) == null) {
        throw new IllegalArgumentException("security does not exist: " + securityId);
    }

    // find the component to the specified security
    Component component = CollectionUtils.find(combination.getComponents(), new Predicate<Component>() {
        @Override
        public boolean evaluate(Component component) {
            return component.getSecurity().getId() == securityId;
        }
    });

    if (component != null) {

        // update the combination
        combination.getComponents().remove(component);

        // delete the component
        this.componentDao.delete(component);

        // remove the component from the ComponentWindow
        removeFromComponentWindow(component);

        // update the ComponentWindow
        insertIntoComponentWindow(combination);

    } else {

        throw new IllegalArgumentException("component on securityId " + securityId + " does not exist");
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("removed component {} from combination {}", component, combinationString);
    }

    return combination;

}

From source file:ch.algotrader.service.CombinationServiceImpl.java

private Combination addOrRemoveComponentQuantity(long combinationId, final long securityId, long quantity,
        boolean add) {

    Combination combination = this.combinationDao.get(combinationId);

    if (combination == null) {
        throw new IllegalArgumentException("combination does not exist: " + combinationId);
    }/*from w  w w  .j ava 2 s .c o m*/

    final Security security = this.securityDao.load(securityId);

    if (security == null) {
        throw new IllegalArgumentException("security does not exist: " + securityId);
    }

    // find the component to the specified security
    Component component = CollectionUtils.find(combination.getComponents(), new Predicate<Component>() {
        @Override
        public boolean evaluate(Component component) {
            return security.getId() == component.getSecurity().getId();
        }
    });

    if (component != null) {

        // add or set the quantity
        if (add) {
            if (quantity == 0) {
                return combination;
            } else {
                component.setQuantity(component.getQuantity() + quantity);
            }
        } else {
            if (component.getQuantity() == quantity) {
                return combination;
            } else {
                component.setQuantity(quantity);
            }
        }

    } else {

        // create a new component
        component = Component.Factory.newInstance();
        component.setSecurity(security);
        component.setQuantity(quantity);

        // associate combination
        component.setCombination(combination);

        this.componentDao.save(component);

        // reverse associate combination (after component has received an id)
        combination.getComponents().add(component);
    }

    // update the ComponentWindow
    insertIntoComponentWindow(combination);

    if (LOGGER.isDebugEnabled()) {
        if (add) {
            LOGGER.debug("added component quantity {} of {} to combination {}", quantity, component,
                    combination.getId());
        } else {
            LOGGER.debug("set component quantity {} of {} to combination {}", quantity, component,
                    combination.getId());
        }
    }

    return combination;
}

From source file:com.javaid.bolaky.domain.pools.entity.Pool.java

public void addPassengers(final Passenger passenger) {

    if (passenger != null) {
        passenger.setPool(this);
    }// www  .j a  va2 s .co m

    Predicate<Passenger> predicate = new Predicate<Passenger>() {

        public boolean evaluate(Passenger passengerFromList) {

            boolean match = false;
            if (passenger != null && passengerFromList != null
                    && passengerFromList.getUsername().equalsIgnoreCase(passenger.getUsername())) {
                match = true;
            }
            return match;
        }
    };

    Passenger existingPassenger = CollectionUtils.find(passengers, predicate);

    if (existingPassenger == null) {

        this.passengers.add(passenger);
    } else {

        if (passenger != null && !passenger.getPassengerRequestInfos().isEmpty()) {

            for (PassengerRequestInfo passengerRequestInfo : passenger.getPassengerRequestInfos()) {

                existingPassenger.addPassengerRequestInfo(passengerRequestInfo);
            }
        }
    }
}

From source file:ch.algotrader.service.CalendarServiceImpl.java

/**
 * gets the holiday for this date/*ww  w  .  j  a va 2 s  . c  om*/
 */
private Holiday getHoliday(final Exchange exchange, final Date dateTime) {

    Validate.notNull(exchange, "exchange not found");
    Validate.notNull(dateTime, "dateTime is null");

    final Date date = DateUtils.truncate(dateTime, Calendar.DATE);

    return CollectionUtils.find(exchange.getHolidays(), new Predicate<Holiday>() {

        @Override
        public boolean evaluate(Holiday holiday) {
            return DateUtils.isSameDay(holiday.getDate(), date);
        }
    });
}

From source file:org.apache.sshd.server.PublickeyAuthenticatorUtils.java

/**
 * @param entries A {@link Collection} of {@link CryptoKeyEntry}-ies
 * @return A {@link PublickeyAuthenticator} that matches the received encoded
 * public key bytes to one of the authorized keys published by the user
 * @see CryptoKeyEntry#readAuthorizedKeys(File)
 *//*from   ww w  .j ava 2s.com*/
public static final PublickeyAuthenticator authorizedKeysAuthenticator(
        Collection<? extends CryptoKeyEntry> entries) {
    final Map<String, ? extends Collection<CryptoKeyEntry>> keysMap = ExtendedMapUtils.mapCollectionMultiValues(
            CryptoKeyEntry.USERNAME_EXTRACTOR, ExtendedCollectionUtils.<CryptoKeyEntry>linkedListFactory(),
            entries);
    return new PublickeyAuthenticator() {
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            Collection<CryptoKeyEntry> keySet = keysMap.get(username);
            if (ExtendedCollectionUtils.isEmpty(keySet)) {
                return false;
            }

            final byte[] keyBytes = key.getEncoded();
            if (ArrayUtils.isEmpty(keyBytes)) {
                return false; // TODO consider throwing an exception
            }

            CryptoKeyEntry entry = CollectionUtils.find(keySet,
                    new AbstractExtendedPredicate<CryptoKeyEntry>(CryptoKeyEntry.class) {
                        @Override
                        public boolean evaluate(CryptoKeyEntry e) {
                            byte[] entryBytes = e.getKeyData();
                            if (Arrays.equals(keyBytes, entryBytes)) {
                                return true;
                            } else {
                                return false; // debug breakpoint;
                            }
                        }
                    });
            if (entry == null) {
                return false;
            } else {
                return true;
            }
        }
    };
}