1 /*
2 * Copyright (c) 2024, 2025, 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
47 * implement checks for {@code instanceof} and pattern matching runtime
48 * implementations. Unconditionally exact testing conversions do not require a
49 * corresponding action at run time and, for this reason, methods corresponding
50 * to these exactness checks are omitted here.
51 * <p>
52 * The run time conversion checks examine whether loss of information would
53 * occur if a testing conversion would be to be applied. In those cases where a
54 * floating-point primitive type is involved, and the value of the testing
55 * conversion is either signed zero, signed infinity or {@code NaN}, these
56 * methods comply with the following:
57 *
58 * <ul>
59 * <li>Converting a floating-point negative zero to an integer type is considered
60 * inexact.</li>
61 * <li>Converting a floating-point {@code NaN} or infinity to an integer type is
62 * considered inexact.</li>
63 * <li>Converting a floating-point {@code NaN} or infinity or signed zero to another
64 * floating-point type is considered exact.</li>
65 * </ul>
66 *
67 * @see <a href="../../../../../specs/primitive-types-in-patterns-instanceof-switch-jls.html#jls-5.7.1">
68 * JLS 5.7.1 Exact Testing Conversions</a>
69 * @see <a href="../../../../../specs/primitive-types-in-patterns-instanceof-switch-jls.html#jls-5.7.2">
70 * JLS 5.7.2 Unconditionally Exact Testing Conversions</a>
71 * @see <a href="../../../../../specs/primitive-types-in-patterns-instanceof-switch-jls.html#jls-15.20.2">
72 * JLS 15.20.2 The instanceof Operator</a>
73 *
74 * @implNote Some exactness checks describe a test which can be redirected
75 * safely through one of the existing methods. Those are omitted too (i.e.,
76 * {@code byte} to {@code char} can be redirected to
77 * {@link ExactConversionsSupport#isIntToCharExact(int)}, {@code short} to
78 * {@code byte} can be redirected to
79 * {@link ExactConversionsSupport#isIntToByteExact(int)} and similarly for
80 * {@code short} to {@code char}, {@code char} to {@code byte} and {@code char}
81 * to {@code short} to the corresponding methods that take an {@code int}).
82 *
83 * @since 23
84 */
85 public final class ExactConversionsSupport {
86
87 private ExactConversionsSupport() { }
88
89 /**
90 * Exactness method from int to byte
91 * @param n value
92 * @return true if and only if the passed value can be converted exactly to the target type
|
1 /*
2 * Copyright (c) 2024, 2026, 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
47 * implement checks for {@code instanceof} and pattern matching runtime
48 * implementations. Unconditionally exact testing conversions do not require a
49 * corresponding action at run time and, for this reason, methods corresponding
50 * to these exactness checks are omitted here.
51 * <p>
52 * The run time conversion checks examine whether loss of information would
53 * occur if a testing conversion would be to be applied. In those cases where a
54 * floating-point primitive type is involved, and the value of the testing
55 * conversion is either signed zero, signed infinity or {@code NaN}, these
56 * methods comply with the following:
57 *
58 * <ul>
59 * <li>Converting a floating-point negative zero to an integer type is considered
60 * inexact.</li>
61 * <li>Converting a floating-point {@code NaN} or infinity to an integer type is
62 * considered inexact.</li>
63 * <li>Converting a floating-point {@code NaN} or infinity or signed zero to another
64 * floating-point type is considered exact.</li>
65 * </ul>
66 *
67 * @jls primitive-types-in-patterns-instanceof-switch-5.7.1 Exact Testing Conversions
68 * @jls primitive-types-in-patterns-instanceof-switch-5.7.2 Unconditionally Exact Testing Conversions
69 * @jls primitive-types-in-patterns-instanceof-switch-15.20.2 The {@code instanceof} Operator
70 *
71 * @implNote Some exactness checks describe a test which can be redirected
72 * safely through one of the existing methods. Those are omitted too (i.e.,
73 * {@code byte} to {@code char} can be redirected to
74 * {@link ExactConversionsSupport#isIntToCharExact(int)}, {@code short} to
75 * {@code byte} can be redirected to
76 * {@link ExactConversionsSupport#isIntToByteExact(int)} and similarly for
77 * {@code short} to {@code char}, {@code char} to {@code byte} and {@code char}
78 * to {@code short} to the corresponding methods that take an {@code int}).
79 *
80 * @since 23
81 */
82 public final class ExactConversionsSupport {
83
84 private ExactConversionsSupport() { }
85
86 /**
87 * Exactness method from int to byte
88 * @param n value
89 * @return true if and only if the passed value can be converted exactly to the target type
|