java.util.function.DoubleToIntFunction

Here are the examples of the java api class java.util.function.DoubleToIntFunction taken from open source projects.

1. CheckedFunctionTest#testCheckedDoubleToIntFunctionWithCustomHandler()

Project: jOOL
File: CheckedFunctionTest.java
@Test
public void testCheckedDoubleToIntFunctionWithCustomHandler() {
    final CheckedDoubleToIntFunction doubleToIntFunction =  d -> {
        throw new Exception("" + d);
    };
    final Consumer<Throwable> handler =  e -> {
        throw new IllegalStateException(e);
    };
    DoubleToIntFunction test = Unchecked.doubleToIntFunction(doubleToIntFunction, handler);
    DoubleToIntFunction alias = CheckedDoubleToIntFunction.unchecked(doubleToIntFunction, handler);
    assertDoubleToIntFunction(test, IllegalStateException.class);
    assertDoubleToIntFunction(alias, IllegalStateException.class);
}

2. CheckedFunctionTest#testCheckedDoubleToIntFunction()

Project: jOOL
File: CheckedFunctionTest.java
@Test
public void testCheckedDoubleToIntFunction() {
    final CheckedDoubleToIntFunction doubleToIntFunction =  d -> {
        throw new Exception("" + d);
    };
    DoubleToIntFunction test = Unchecked.doubleToIntFunction(doubleToIntFunction);
    DoubleToIntFunction alias = CheckedDoubleToIntFunction.unchecked(doubleToIntFunction);
    assertDoubleToIntFunction(test, UncheckedException.class);
    assertDoubleToIntFunction(alias, UncheckedException.class);
}

3. DoubleStreamExTest#testMinMax()

Project: streamex
File: DoubleStreamExTest.java
@Test
public void testMinMax() {
    checkEmpty( s -> s.maxBy(Double::valueOf),  s -> s.maxByInt( x -> (int) x),  s -> s.maxByLong( x -> (long) x),  s -> s.maxByDouble( x -> x),  s -> s.minBy(Double::valueOf),  s -> s.minByInt( x -> (int) x),  s -> s.minByLong( x -> (long) x),  s -> s.minByDouble( x -> x));
    assertEquals(9, IntStreamEx.range(5, 12).asDoubleStream().max(( a,  b) -> String.valueOf(a).compareTo(String.valueOf(b))).getAsDouble(), 0.0);
    assertEquals(10, IntStreamEx.range(5, 12).asDoubleStream().min(( a,  b) -> String.valueOf(a).compareTo(String.valueOf(b))).getAsDouble(), 0.0);
    assertEquals(9, IntStreamEx.range(5, 12).asDoubleStream().maxBy(String::valueOf).getAsDouble(), 0.0);
    assertEquals(10, IntStreamEx.range(5, 12).asDoubleStream().minBy(String::valueOf).getAsDouble(), 0.0);
    assertEquals(5, IntStreamEx.range(5, 12).asDoubleStream().maxByDouble( x -> 1.0 / x).getAsDouble(), 0.0);
    assertEquals(11, IntStreamEx.range(5, 12).asDoubleStream().minByDouble( x -> 1.0 / x).getAsDouble(), 0.0);
    assertEquals(29.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).maxByInt( x -> (int) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    assertEquals(31.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).minByInt( x -> (int) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    assertEquals(29.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).maxByLong( x -> (long) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    assertEquals(31.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).minByLong( x -> (long) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    Supplier<DoubleStreamEx> s = () -> DoubleStreamEx.of(1, 50, 120, 35, 130, 12, 0);
    DoubleToIntFunction intKey =  x -> String.valueOf(x).length();
    DoubleToLongFunction longKey =  x -> String.valueOf(x).length();
    DoubleUnaryOperator doubleKey =  x -> String.valueOf(x).length();
    DoubleFunction<Integer> objKey =  x -> String.valueOf(x).length();
    List<Function<DoubleStreamEx, OptionalDouble>> minFns = Arrays.asList( is -> is.minByInt(intKey),  is -> is.minByLong(longKey),  is -> is.minByDouble(doubleKey),  is -> is.minBy(objKey));
    List<Function<DoubleStreamEx, OptionalDouble>> maxFns = Arrays.asList( is -> is.maxByInt(intKey),  is -> is.maxByLong(longKey),  is -> is.maxByDouble(doubleKey),  is -> is.maxBy(objKey));
    minFns.forEach( fn -> assertEquals(1, fn.apply(s.get()).getAsDouble(), 0.0));
    minFns.forEach( fn -> assertEquals(1, fn.apply(s.get().parallel()).getAsDouble(), 0.0));
    maxFns.forEach( fn -> assertEquals(120, fn.apply(s.get()).getAsDouble(), 0.0));
    maxFns.forEach( fn -> assertEquals(120, fn.apply(s.get().parallel()).getAsDouble(), 0.0));
}