|
| 1 | +/* |
| 2 | + * Copyright (C) 2007 The Guava Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// This source file is part of the Swift.org open source project |
| 18 | +// |
| 19 | +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors |
| 20 | +// Licensed under Apache License v2.0 |
| 21 | +// |
| 22 | +// See LICENSE.txt for license information |
| 23 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 24 | +// |
| 25 | +// SPDX-License-Identifier: Apache-2.0 |
| 26 | +// |
| 27 | +//===----------------------------------------------------------------------===// |
| 28 | + |
| 29 | +package org.swift.swiftkit.core; |
| 30 | + |
| 31 | +import org.swift.swiftkit.core.annotations.Nullable; |
| 32 | + |
| 33 | +/** |
| 34 | + * Collection of convenience functions to check argument preconditions. |
| 35 | + * <p/> |
| 36 | + * Partially based on {@code com.google.common.base.Preconditions}. |
| 37 | + */ |
| 38 | +public final class Preconditions { |
| 39 | + private Preconditions() { |
| 40 | + } |
| 41 | + |
| 42 | + public static void checkArgument(boolean expression) { |
| 43 | + if (!expression) { |
| 44 | + throw new IllegalArgumentException(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static void checkArgument(boolean expression, @Nullable String format) { |
| 49 | + if (!expression) { |
| 50 | + throw new IllegalArgumentException(format); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void checkArgument(boolean expression, @Nullable String format, |
| 55 | + @Nullable Object arg1) { |
| 56 | + if (!expression) { |
| 57 | + throw new IllegalArgumentException(String.format(format, arg1)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static void checkArgument(boolean expression, @Nullable String format, |
| 62 | + @Nullable Object arg1, |
| 63 | + @Nullable Object arg2) { |
| 64 | + if (!expression) { |
| 65 | + throw new IllegalArgumentException(String.format(format, arg1, arg2)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static <T> T checkNotNull(@Nullable T reference) { |
| 70 | + if (reference == null) { |
| 71 | + throw new NullPointerException(); |
| 72 | + } |
| 73 | + |
| 74 | + return reference; |
| 75 | + } |
| 76 | + |
| 77 | + public static <T> T checkNotNull(@Nullable T reference, @Nullable String message) { |
| 78 | + if (reference == null) { |
| 79 | + throw new NullPointerException(message); |
| 80 | + } |
| 81 | + |
| 82 | + return reference; |
| 83 | + } |
| 84 | + |
| 85 | + /* |
| 86 | + * All recent hotspots (as of 2009) *really* like to have the natural code |
| 87 | + * |
| 88 | + * if (guardExpression) { |
| 89 | + * throw new BadException(messageExpression); |
| 90 | + * } |
| 91 | + * |
| 92 | + * refactored so that messageExpression is moved to a separate String-returning method. |
| 93 | + * |
| 94 | + * if (guardExpression) { |
| 95 | + * throw new BadException(badMsg(...)); |
| 96 | + * } |
| 97 | + * |
| 98 | + * The alternative natural refactorings into void or Exception-returning methods are much slower. |
| 99 | + * This is a big deal - we're talking factors of 2-8 in microbenchmarks, not just 10-20%. (This is |
| 100 | + * a hotspot optimizer bug, which should be fixed, but that's a separate, big project). |
| 101 | + * |
| 102 | + * The coding pattern above is heavily used in java.util, e.g. in ArrayList. There is a |
| 103 | + * RangeCheckMicroBenchmark in the JDK that was used to test this. |
| 104 | + * |
| 105 | + * But the methods in this class want to throw different exceptions, depending on the args, so it |
| 106 | + * appears that this pattern is not directly applicable. But we can use the ridiculous, devious |
| 107 | + * trick of throwing an exception in the middle of the construction of another exception. Hotspot |
| 108 | + * is fine with that. |
| 109 | + */ |
| 110 | + |
| 111 | + /** |
| 112 | + * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size |
| 113 | + * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive. |
| 114 | + * |
| 115 | + * @param index a user-supplied index identifying an element of an array, list or string |
| 116 | + * @param size the size of that array, list or string |
| 117 | + * @return the value of {@code index} |
| 118 | + * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size} |
| 119 | + * @throws IllegalArgumentException if {@code size} is negative |
| 120 | + */ |
| 121 | + public static int checkElementIndex(int index, int size) { |
| 122 | + return checkElementIndex(index, size, "index"); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Ensures that {@code index} specifies a valid <i>element</i> in an array, list or string of size |
| 127 | + * {@code size}. An element index may range from zero, inclusive, to {@code size}, exclusive. |
| 128 | + * |
| 129 | + * @param index a user-supplied index identifying an element of an array, list or string |
| 130 | + * @param size the size of that array, list or string |
| 131 | + * @param desc the text to use to describe this index in an error message |
| 132 | + * @return the value of {@code index} |
| 133 | + * @throws IndexOutOfBoundsException if {@code index} is negative or is not less than {@code size} |
| 134 | + * @throws IllegalArgumentException if {@code size} is negative |
| 135 | + */ |
| 136 | + public static int checkElementIndex(int index, int size, String desc) { |
| 137 | + // Carefully optimized for execution by hotspot (explanatory comment above) |
| 138 | + if (index < 0 || index >= size) { |
| 139 | + throw new IndexOutOfBoundsException( |
| 140 | + String.format("%s, index:%d, size:%d", desc, index, size)); |
| 141 | + } |
| 142 | + return index; |
| 143 | + } |
| 144 | + |
| 145 | + public static void checkPositionIndexes(int start, int end, int size) { |
| 146 | + // Carefully optimized for execution by hotspot (explanatory comment above) |
| 147 | + if (start < 0 || end < start || end > size) { |
| 148 | + throw new IndexOutOfBoundsException( |
| 149 | + String.format("Start index:%d, end index:%d, size: %d", start, end, size)); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments