Java Code Examples for java.util.Comparator.comparingDouble()

The following are Jave code examples for showing how to use comparingDouble() of the java.util.Comparator class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Biliomi   File: XmlElementPathBeanComparator.java   View Source Code Vote up 6 votes
@Override
public int compare(T o1, T o2) {
  Comparable obj1 = null;
  Comparable obj2 = null;

  try {
    obj1 = (Comparable) PropertyUtils.getProperty(o1, this.propertyPath);
    obj2 = (Comparable) PropertyUtils.getProperty(o2, this.propertyPath);
  } catch (NestedNullException ignored) {
    // Ignored, als het property NULL is, dan vergelijken met NULL
  } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
    throw new IllegalArgumentException("Could not retrieve property " + this.propertyPath, e);
  }

  Comparator<Comparable<Object>> objectComparator = null;
  if ((obj1 != null && obj2 != null) && obj1 instanceof String) {
    obj1 = ((String) obj1).toLowerCase().trim();
    obj2 = ((String) obj2).toLowerCase().trim();

    if (!StringUtils.isEmpty((String) obj1) && !StringUtils.isEmpty((String) obj2)) {
      if (StringUtils.isNumeric((String) obj1) && StringUtils.isNumeric((String) obj2)) {
        objectComparator = Comparator.comparingDouble(o -> new Double(String.valueOf(o)));
      }
    }
  }

  if (objectComparator == null) {
    objectComparator = Comparator.naturalOrder();
  }

  //noinspection unchecked
  return Comparator.nullsLast(objectComparator).compare(obj1, obj2);
}
 
Example 2
Project: freecol   File: EuropeanAIPlayer.java   View Source Code Vote up 6 votes
/**
 * Gets the best goods wish for a carrier unit.
 *
 * @param aiUnit The carrier {@code AIUnit}.
 * @param goodsType The {@code GoodsType} to wish for.
 * @return The best {@code GoodsWish} for the unit.
 */
public GoodsWish getBestGoodsWish(AIUnit aiUnit, GoodsType goodsType) {
    final Unit carrier = aiUnit.getUnit();
    final ToDoubleFunction<GoodsWish> wishValue
        = cacheDouble(gw -> {
                int turns = carrier.getTurnsToReach(carrier.getLocation(),
                                                    gw.getDestination());
                return (turns >= Unit.MANY_TURNS) ? -1.0
                    : (double)gw.getValue() / turns;
            });
    final Comparator<GoodsWish> comp
        = Comparator.comparingDouble(wishValue);

    List<GoodsWish> wishes = goodsWishes.get(goodsType);
    return (wishes == null) ? null
        : maximize(wishes, gw -> wishValue.applyAsDouble(gw) > 0.0, comp);
}
 
Example 3
Project: FreeCol   File: EuropeanAIPlayer.java   View Source Code Vote up 6 votes
/**
 * Gets the best goods wish for a carrier unit.
 *
 * @param aiUnit The carrier {@code AIUnit}.
 * @param goodsType The {@code GoodsType} to wish for.
 * @return The best {@code GoodsWish} for the unit.
 */
public GoodsWish getBestGoodsWish(AIUnit aiUnit, GoodsType goodsType) {
    final Unit carrier = aiUnit.getUnit();
    final ToDoubleFunction<GoodsWish> wishValue
        = cacheDouble(gw -> {
                int turns = carrier.getTurnsToReach(carrier.getLocation(),
                                                    gw.getDestination());
                return (turns >= Unit.MANY_TURNS) ? -1.0
                    : (double)gw.getValue() / turns;
            });
    final Comparator<GoodsWish> comp
        = Comparator.comparingDouble(wishValue);

    List<GoodsWish> wishes = goodsWishes.get(goodsType);
    return (wishes == null) ? null
        : maximize(wishes, gw -> wishValue.applyAsDouble(gw) > 0.0, comp);
}
 
Example 4
Project: jdk8u-jdk   File: BasicTest.java   View Source Code Vote up 5 votes
public void testDoubleComparator() {
    Thing[] things = new Thing[doubleValues.length];
    for (int i=0; i<doubleValues.length; i++)
        things[i] = new Thing(0, 0L, doubleValues[i], null);
    Comparator<Thing> comp = Comparator.comparingDouble(new ToDoubleFunction<Thing>() {
        @Override
        public double applyAsDouble(Thing thing) {
            return thing.getDoubleField();
        }
    });

    assertComparisons(things, comp, comparisons);
}
 
Example 5
Project: openjdk-jdk10   File: BasicTest.java   View Source Code Vote up 5 votes
public void testDoubleComparator() {
    Thing[] things = new Thing[doubleValues.length];
    for (int i=0; i<doubleValues.length; i++)
        things[i] = new Thing(0, 0L, doubleValues[i], null);
    Comparator<Thing> comp = Comparator.comparingDouble(new ToDoubleFunction<Thing>() {
        @Override
        public double applyAsDouble(Thing thing) {
            return thing.getDoubleField();
        }
    });

    assertComparisons(things, comp, comparisons);
}
 
Example 6
Project: Higher-Cloud-Computing-Project   File: ColumnDecisionTreeTrainer.java   View Source Code Vote up 4 votes
/** */
private static Comparator<IgniteBiTuple<Integer, IgniteBiTuple<Integer, Double>>> comparator() {
    return Comparator.comparingDouble(bt -> bt != null && bt.get2() != null ? bt.get2().get2() : Double.NEGATIVE_INFINITY);
}
 
Example 7
Project: freecol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * Helper to create a caching comparator.
 *
 * @param <T> The argument type to be converted to double.
 * @param f The double valued function to use in comparison.
 * @return A caching {@code Comparator}.
 */
public static <T> Comparator<T> cachingDoubleComparator(Function<T, Double> f) {
    return Comparator.comparingDouble(cacheDouble(f));
}
 
Example 8
Project: FreeCol   File: CollectionUtils.java   View Source Code Vote up 2 votes
/**
 * Helper to create a caching comparator.
 *
 * @param <T> The argument type to be converted to double.
 * @param f The double valued function to use in comparison.
 * @return A caching {@code Comparator}.
 */
public static <T> Comparator<T> cachingDoubleComparator(Function<T, Double> f) {
    return Comparator.comparingDouble(cacheDouble(f));
}