IntUnaryOperator.java revision e053d5ee5eea301559822c98e69874344c971502
1/*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package java.util.function;
26
27import java.util.Objects;
28
29/**
30 * Represents an operation on a single {@code int}-valued operand that produces
31 * an {@code int}-valued result.  This is the primitive type specialization of
32 * {@link UnaryOperator} for {@code int}.
33 *
34 * <p>This is a <a href="package-summary.html">functional interface</a>
35 * whose functional method is {@link #applyAsInt(int)}.
36 *
37 * @see UnaryOperator
38 * @since 1.8
39 * @hide 1.8
40 */
41@FunctionalInterface
42public interface IntUnaryOperator {
43
44    /**
45     * Applies this operator to the given operand.
46     *
47     * @param operand the operand
48     * @return the operator result
49     */
50    int applyAsInt(int operand);
51
52    /**
53     * Returns a composed operator that first applies the {@code before}
54     * operator to its input, and then applies this operator to the result.
55     * If evaluation of either operator throws an exception, it is relayed to
56     * the caller of the composed operator.
57     *
58     * @param before the operator to apply before this operator is applied
59     * @return a composed operator that first applies the {@code before}
60     * operator and then applies this operator
61     * @throws NullPointerException if before is null
62     *
63     * @see #andThen(IntUnaryOperator)
64     */
65    default IntUnaryOperator compose(IntUnaryOperator before) {
66        Objects.requireNonNull(before);
67        return (int v) -> applyAsInt(before.applyAsInt(v));
68    }
69
70    /**
71     * Returns a composed operator that first applies this operator to
72     * its input, and then applies the {@code after} operator to the result.
73     * If evaluation of either operator throws an exception, it is relayed to
74     * the caller of the composed operator.
75     *
76     * @param after the operator to apply after this operator is applied
77     * @return a composed operator that first applies this operator and then
78     * applies the {@code after} operator
79     * @throws NullPointerException if after is null
80     *
81     * @see #compose(IntUnaryOperator)
82     */
83    default IntUnaryOperator andThen(IntUnaryOperator after) {
84        Objects.requireNonNull(after);
85        return (int t) -> after.applyAsInt(applyAsInt(t));
86    }
87
88    /**
89     * Returns a unary operator that always returns its input argument.
90     *
91     * @return a unary operator that always returns its input argument
92     */
93    static IntUnaryOperator identity() {
94        return t -> t;
95    }
96}
97