Search in sources :

Example 1 with ReverseComparator

use of org.apache.commons.collections.comparators.ReverseComparator in project vaadin-jsf-integration by alejandro-du.

the class BasePage method sort.

/**
 * Sort list according to which column has been clicked on.
 * @param list the java.util.List to sort
 * @return ordered list
 */
@SuppressWarnings("unchecked")
protected List sort(List list) {
    Comparator comparator = new BeanComparator(sortColumn, new NullComparator(nullsAreHigh));
    if (!ascending) {
        comparator = new ReverseComparator(comparator);
    }
    Collections.sort(list, comparator);
    return list;
}
Also used : NullComparator(org.apache.commons.collections.comparators.NullComparator) BeanComparator(org.apache.commons.beanutils.BeanComparator) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) BeanComparator(org.apache.commons.beanutils.BeanComparator) NullComparator(org.apache.commons.collections.comparators.NullComparator) Comparator(java.util.Comparator)

Example 2 with ReverseComparator

use of org.apache.commons.collections.comparators.ReverseComparator in project mondrian by pentaho.

the class PartialSortTest method doStablePartialSort.

// just glue
private Item[] doStablePartialSort(Item[] vec, boolean desc, int limit) {
    Comparator<Item> comp = Item.byKey;
    if (desc) {
        // noinspection unchecked
        comp = new ReverseComparator(comp);
    }
    List<Item> sorted = FunUtil.stablePartialSort(Arrays.asList(vec), comp, limit);
    return sorted.toArray(new Item[sorted.size()]);
}
Also used : ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator)

Example 3 with ReverseComparator

use of org.apache.commons.collections.comparators.ReverseComparator in project motech by motech.

the class InMemoryQueryFilter method order.

/**
 * Orders the provided collection using the provided ordering information.
 * @param collection the collection to order
 * @param orderList list of orders that should be applied to the collection
 * @param <T> the type of the collection to order
 * @return a new list with ordered objects from the provided collection
 */
private static <T> List<T> order(Collection<T> collection, List<Order> orderList) {
    List<Comparator<T>> comparatorList = new ArrayList<>();
    for (Order order : orderList) {
        Comparator<T> comparator = new BeanComparator<>(order.getField(), new NullComparator());
        // reverse it if order is descending
        if (order.getDirection() == Order.Direction.DESC) {
            comparator = new ReverseComparator(comparator);
        }
        comparatorList.add(comparator);
    }
    // we use a compound comparator to chain comparators for each provided order
    CompoundComparator<T> compoundComparator = new CompoundComparator<>(comparatorList.toArray(new Comparator[comparatorList.size()]));
    // convert to a list and sort it
    List<T> result = new ArrayList<>(collection);
    Collections.sort(result, compoundComparator);
    return result;
}
Also used : Order(org.motechproject.mds.util.Order) NullComparator(org.apache.commons.collections.comparators.NullComparator) ArrayList(java.util.ArrayList) BeanComparator(org.apache.commons.beanutils.BeanComparator) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) CompoundComparator(org.springframework.util.comparator.CompoundComparator) NullComparator(org.apache.commons.collections.comparators.NullComparator) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) CompoundComparator(org.springframework.util.comparator.CompoundComparator) BeanComparator(org.apache.commons.beanutils.BeanComparator) Comparator(java.util.Comparator)

Example 4 with ReverseComparator

use of org.apache.commons.collections.comparators.ReverseComparator in project BroadleafCommerce by BroadleafCommerce.

the class FulfillmentGroupOfferProcessorImpl method applyAllFulfillmentGroupOffers.

@Override
@SuppressWarnings("unchecked")
public boolean applyAllFulfillmentGroupOffers(List<PromotableCandidateFulfillmentGroupOffer> qualifiedFGOffers, PromotableOrder order) {
    Map<FulfillmentGroupOfferPotential, List<PromotableCandidateFulfillmentGroupOffer>> offerMap = new HashMap<FulfillmentGroupOfferPotential, List<PromotableCandidateFulfillmentGroupOffer>>();
    for (PromotableCandidateFulfillmentGroupOffer candidate : qualifiedFGOffers) {
        FulfillmentGroupOfferPotential potential = new FulfillmentGroupOfferPotential();
        potential.setOffer(candidate.getOffer());
        if (offerMap.get(potential) == null) {
            offerMap.put(potential, new ArrayList<PromotableCandidateFulfillmentGroupOffer>());
        }
        offerMap.get(potential).add(candidate);
    }
    List<FulfillmentGroupOfferPotential> potentials = new ArrayList<FulfillmentGroupOfferPotential>();
    for (FulfillmentGroupOfferPotential potential : offerMap.keySet()) {
        List<PromotableCandidateFulfillmentGroupOffer> fgOffers = offerMap.get(potential);
        Collections.sort(fgOffers, new ReverseComparator(new BeanComparator("discountedAmount", new NullComparator())));
        Collections.sort(fgOffers, new BeanComparator("priority", new NullComparator()));
        if (potential.getOffer().isLimitedUsePerOrder() && fgOffers.size() > potential.getOffer().getMaxUsesPerOrder()) {
            for (int j = potential.getOffer().getMaxUsesPerOrder(); j < fgOffers.size(); j++) {
                fgOffers.remove(j);
            }
        }
        filterOffersByQualifyingAndSubtotalRequirements(order, fgOffers);
        for (PromotableCandidateFulfillmentGroupOffer candidate : fgOffers) {
            if (potential.getTotalSavings().getAmount().equals(BankersRounding.zeroAmount())) {
                BroadleafCurrency currency = order.getOrderCurrency();
                if (currency != null) {
                    potential.setTotalSavings(new Money(BigDecimal.ZERO, currency.getCurrencyCode()));
                } else {
                    potential.setTotalSavings(new Money(BigDecimal.ZERO));
                }
            }
            Money priceBeforeAdjustments = candidate.getFulfillmentGroup().calculatePriceWithoutAdjustments();
            Money discountedPrice = candidate.getDiscountedPrice();
            potential.setTotalSavings(potential.getTotalSavings().add(priceBeforeAdjustments.subtract(discountedPrice)));
            potential.setPriority(candidate.getOffer().getPriority());
        }
        potentials.add(potential);
    }
    // Sort fg potentials by priority and discount
    Collections.sort(potentials, new BeanComparator("totalSavings", Collections.reverseOrder()));
    Collections.sort(potentials, new BeanComparator("priority"));
    potentials = removeTrailingNotCombinableFulfillmentGroupOffers(potentials);
    boolean fgOfferApplied = false;
    for (FulfillmentGroupOfferPotential potential : potentials) {
        Offer offer = potential.getOffer();
        boolean alreadyContainsTotalitarianOffer = order.isTotalitarianOfferApplied();
        List<PromotableCandidateFulfillmentGroupOffer> candidates = offerMap.get(potential);
        for (PromotableCandidateFulfillmentGroupOffer candidate : candidates) {
            applyFulfillmentGroupOffer(candidate.getFulfillmentGroup(), candidate);
            fgOfferApplied = true;
        }
        for (PromotableFulfillmentGroup fg : order.getFulfillmentGroups()) {
            fg.chooseSaleOrRetailAdjustments();
        }
        if ((offer.isTotalitarianOffer() != null && offer.isTotalitarianOffer()) || alreadyContainsTotalitarianOffer) {
            fgOfferApplied = compareAndAdjustFulfillmentGroupOffers(order, fgOfferApplied);
            if (fgOfferApplied) {
                break;
            }
        } else if (!offer.isCombinableWithOtherOffers()) {
            break;
        }
    }
    return fgOfferApplied;
}
Also used : HashMap(java.util.HashMap) NullComparator(org.apache.commons.collections.comparators.NullComparator) FulfillmentGroupOfferPotential(org.broadleafcommerce.core.offer.service.discount.FulfillmentGroupOfferPotential) ArrayList(java.util.ArrayList) ReverseComparator(org.apache.commons.collections.comparators.ReverseComparator) BeanComparator(org.apache.commons.beanutils.BeanComparator) Money(org.broadleafcommerce.common.money.Money) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) PromotableCandidateFulfillmentGroupOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer) Offer(org.broadleafcommerce.core.offer.domain.Offer) PromotableFulfillmentGroup(org.broadleafcommerce.core.offer.service.discount.domain.PromotableFulfillmentGroup) ArrayList(java.util.ArrayList) List(java.util.List) PromotableCandidateFulfillmentGroupOffer(org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer)

Aggregations

ReverseComparator (org.apache.commons.collections.comparators.ReverseComparator)4 BeanComparator (org.apache.commons.beanutils.BeanComparator)3 NullComparator (org.apache.commons.collections.comparators.NullComparator)3 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)1 List (java.util.List)1 BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)1 Money (org.broadleafcommerce.common.money.Money)1 Offer (org.broadleafcommerce.core.offer.domain.Offer)1 FulfillmentGroupOfferPotential (org.broadleafcommerce.core.offer.service.discount.FulfillmentGroupOfferPotential)1 PromotableCandidateFulfillmentGroupOffer (org.broadleafcommerce.core.offer.service.discount.domain.PromotableCandidateFulfillmentGroupOffer)1 PromotableFulfillmentGroup (org.broadleafcommerce.core.offer.service.discount.domain.PromotableFulfillmentGroup)1 Order (org.motechproject.mds.util.Order)1 CompoundComparator (org.springframework.util.comparator.CompoundComparator)1