Search in sources :

Example 1 with IntBinaryOperator

use of java.util.function.IntBinaryOperator in project intellij-community by JetBrains.

the class Main method main.

public static void main(String[] args) {
    IntBinaryOperator op = Math::m < caret > in;
    System.out.println(op.applyAsInt(1, 2));
}
Also used : IntBinaryOperator(java.util.function.IntBinaryOperator)

Example 2 with IntBinaryOperator

use of java.util.function.IntBinaryOperator in project jdk8u_jdk by JetBrains.

the class PrimitiveSumMinMaxTest method testIntMethods.

public void testIntMethods() {
    BinaryOperator<Integer> sum1 = Integer::sum;
    IntBinaryOperator sum2 = Integer::sum;
    BinaryOperator<Integer> max1 = Integer::max;
    IntBinaryOperator max2 = Integer::max;
    BinaryOperator<Integer> min1 = Integer::min;
    IntBinaryOperator min2 = Integer::min;
    Comparator<Integer> cmp = Integer::compare;
    int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
    for (int i : numbers) {
        for (int j : numbers) {
            assertEquals(i + j, (int) sum1.apply(i, j));
            assertEquals(i + j, sum2.applyAsInt(i, j));
            assertEquals(Math.max(i, j), (int) max1.apply(i, j));
            assertEquals(Math.max(i, j), max2.applyAsInt(i, j));
            assertEquals(Math.min(i, j), (int) min1.apply(i, j));
            assertEquals(Math.min(i, j), min2.applyAsInt(i, j));
            assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
Also used : IntBinaryOperator(java.util.function.IntBinaryOperator)

Example 3 with IntBinaryOperator

use of java.util.function.IntBinaryOperator in project felix by apache.

the class Posix method toColumn.

private void toColumn(CommandSession session, Process process, PrintStream out, Stream<String> ansi, boolean horizontal) {
    Terminal terminal = Shell.getTerminal(session);
    int width = process.isTty(1) ? terminal.getWidth() : 80;
    List<AttributedString> strings = ansi.map(AttributedString::fromAnsi).collect(Collectors.toList());
    if (!strings.isEmpty()) {
        int max = strings.stream().mapToInt(AttributedString::columnLength).max().getAsInt();
        int c = Math.max(1, width / max);
        while (c > 1 && c * max + (c - 1) >= width) {
            c--;
        }
        int columns = c;
        int lines = (strings.size() + columns - 1) / columns;
        IntBinaryOperator index;
        if (horizontal) {
            index = (i, j) -> i * columns + j;
        } else {
            index = (i, j) -> j * lines + i;
        }
        AttributedStringBuilder sb = new AttributedStringBuilder();
        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < columns; j++) {
                int idx = index.applyAsInt(i, j);
                if (idx < strings.size()) {
                    AttributedString str = strings.get(idx);
                    boolean hasRightItem = j < columns - 1 && index.applyAsInt(i, j + 1) < strings.size();
                    sb.append(str);
                    if (hasRightItem) {
                        for (int k = 0; k <= max - str.length(); k++) {
                            sb.append(' ');
                        }
                    }
                }
            }
            sb.append('\n');
        }
        out.print(sb.toAnsi(terminal));
    }
}
Also used : AttributedString(org.jline.utils.AttributedString) IntBinaryOperator(java.util.function.IntBinaryOperator) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Terminal(org.jline.terminal.Terminal)

Example 4 with IntBinaryOperator

use of java.util.function.IntBinaryOperator in project graal by oracle.

the class MethodMessageTest method functionTest.

@Test
public void functionTest() throws Exception {
    TruffleObject truffleMath = JavaInterop.asTruffleObject(Math.class);
    MathWrap wrap = JavaInterop.asJavaObject(MathWrap.class, truffleMath);
    IntBinaryOperator maxFunction = wrap.max();
    int res = maxFunction.applyAsInt(10, 5);
    assertEquals(10, res);
}
Also used : IntBinaryOperator(java.util.function.IntBinaryOperator) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 5 with IntBinaryOperator

use of java.util.function.IntBinaryOperator in project java-example by 1479005017.

the class AtomicIntegerTest method main.

public static void main(String[] args) {
    AtomicInteger atomicInteger = new AtomicInteger(0);
    /*
         * 获取当前值
         * get()
         * @return 当前值
         */
    // int: 0
    System.out.println("int: " + atomicInteger.get());
    /*
         * 设置值
         * set(value)
         */
    atomicInteger.set(0);
    /*
         * 获取旧值,设置新值
         * getAndSet(newValue)
         * @return 旧值
         */
    // int: 0
    System.out.println("int: " + atomicInteger.getAndSet(1));
    /*
         * 如果旧值为expectedValue时,设置新值newValue
         * compareAndSet(expectedValue, newValue)
         * @return 是否设置成功
         */
    // succeed: true
    System.out.println("succeed: " + atomicInteger.compareAndSet(1, 2));
    /*
         *
         * getAndIncrement()
         * @return
         */
    /*
         *
         * getAndDecrement()
         * @return
         */
    /*
         *
         * getAndAdd(delta)
         * @return
         */
    /*
         *
         * incrementAndGet()
         * @return
         */
    /*
         *
         * decrementAndGet()
         * @return
         */
    /*
         *
         * addAndGet(delta)
         * @return
         */
    /*
         *
         * getAndUpdate(function)
         * @return
         */
    /*
         *
         * updateAndGet(function)
         * @return
         */
    System.out.println("int: " + atomicInteger.updateAndGet(new IntUnaryOperator() {

        @Override
        public int applyAsInt(int value) {
            return value * 2;
        }
    }));
    // int: 4
    /*
         *
         * getAndAccumulate(delta, function)
         * @return
         */
    /*
         *
         * accumulateAndGet(delta, function)
         * @return
         */
    System.out.println("int: " + atomicInteger.accumulateAndGet(1, new IntBinaryOperator() {

        @Override
        public int applyAsInt(int value, int operand) {
            return value + operand;
        }
    }));
// int: 5
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntBinaryOperator(java.util.function.IntBinaryOperator) IntUnaryOperator(java.util.function.IntUnaryOperator)

Aggregations

IntBinaryOperator (java.util.function.IntBinaryOperator)6 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IntUnaryOperator (java.util.function.IntUnaryOperator)1 Terminal (org.jline.terminal.Terminal)1 AttributedString (org.jline.utils.AttributedString)1 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)1 Test (org.junit.Test)1