Search in sources :

Example 1 with TreeSortedMap

use of org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap in project robozonky by RoboZonky.

the class Util method rankRatingsByDemand.

public static Stream<Rating> rankRatingsByDemand(final ParsedStrategy strategy, final Map<Rating, BigDecimal> currentShare) {
    final SortedMap<BigDecimal, EnumSet<Rating>> mostWantedRatings = new TreeSortedMap<>(Comparator.reverseOrder());
    // put the ratings into buckets based on how much we're missing them
    currentShare.forEach((r, currentRatingShare) -> {
        final BigDecimal maximumAllowedShare = toDecimalShare(strategy.getMaximumShare(r));
        final BigDecimal undershare = maximumAllowedShare.subtract(currentRatingShare);
        if (undershare.signum() < 1) {
            // we over-invested into this rating; do not include
            final BigDecimal pp = undershare.multiply(ONE_HUNDRED).negate();
            Decisions.report(logger -> logger.debug("Rating {} over-invested by {} percentage points.", r, pp));
            return;
        }
        mostWantedRatings.compute(undershare, (k, v) -> {
            // rank the rating
            if (v == null) {
                return EnumSet.of(r);
            }
            v.add(r);
            return v;
        });
        final BigDecimal minimumNeededShare = toDecimalShare(strategy.getMinimumShare(r));
        if (currentRatingShare.compareTo(minimumNeededShare) < 0) {
            // inform that the rating is under-invested
            final BigDecimal pp = minimumNeededShare.subtract(currentRatingShare).multiply(ONE_HUNDRED);
            Decisions.report(logger -> logger.debug("Rating {} under-invested by {} percentage points.", r, pp));
        }
    });
    return mostWantedRatings.values().stream().flatMap(Collection::stream);
}
Also used : EnumSet(java.util.EnumSet) TreeSortedMap(org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap) Collection(java.util.Collection) BigDecimal(java.math.BigDecimal)

Example 2 with TreeSortedMap

use of org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap in project eclipse-collections by eclipse.

the class ImmutableTreeMapTest method entrySet.

@Override
public void entrySet() {
    super.entrySet();
    Interval interval = Interval.oneTo(100);
    LazyIterable<Pair<String, Integer>> pairs = interval.collect(Object::toString).zip(interval);
    MutableSortedMap<String, Integer> mutableSortedMap = new TreeSortedMap<>(pairs.toArray(new Pair[] {}));
    ImmutableSortedMap<String, Integer> immutableSortedMap = mutableSortedMap.toImmutable();
    MutableList<Map.Entry<String, Integer>> entries = FastList.newList(immutableSortedMap.castToSortedMap().entrySet());
    MutableList<Map.Entry<String, Integer>> sortedEntries = entries.toSortedListBy(Map.Entry::getKey);
    Assert.assertEquals(sortedEntries, entries);
}
Also used : TreeSortedMap(org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap) Interval(org.eclipse.collections.impl.list.Interval) Pair(org.eclipse.collections.api.tuple.Pair)

Aggregations

TreeSortedMap (org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap)2 BigDecimal (java.math.BigDecimal)1 Collection (java.util.Collection)1 EnumSet (java.util.EnumSet)1 Pair (org.eclipse.collections.api.tuple.Pair)1 Interval (org.eclipse.collections.impl.list.Interval)1