diff --git a/changelog/@unreleased/pr-515.v2.yml b/changelog/@unreleased/pr-515.v2.yml new file mode 100644 index 00000000..932667d5 --- /dev/null +++ b/changelog/@unreleased/pr-515.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Migrate the Preconditions utility to its own `com.palantir.logsafe.preconditions` package + links: + - https://github.com/palantir/safe-logging/pull/515 diff --git a/preconditions-legacy/build.gradle b/preconditions-legacy/build.gradle new file mode 100644 index 00000000..06ab8ded --- /dev/null +++ b/preconditions-legacy/build.gradle @@ -0,0 +1,9 @@ +apply from: "${rootDir}/gradle/publish-jar.gradle" +apply plugin: 'com.palantir.revapi' + +dependencies { + api project(':safe-logging') + api project(':safe-logging-exceptions') + compileOnly 'com.google.code.findbugs:jsr305' + api 'com.google.errorprone:error_prone_annotations' +} diff --git a/preconditions-legacy/src/main/java/com/palantir/logsafe/Preconditions.java b/preconditions-legacy/src/main/java/com/palantir/logsafe/Preconditions.java new file mode 100755 index 00000000..b5f751ed --- /dev/null +++ b/preconditions-legacy/src/main/java/com/palantir/logsafe/Preconditions.java @@ -0,0 +1,295 @@ +/* + * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Copyright (C) 2007 The Guava Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.palantir.logsafe; + +import com.google.errorprone.annotations.CanIgnoreReturnValue; +import com.google.errorprone.annotations.CompileTimeConstant; +import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; +import com.palantir.logsafe.exceptions.SafeIllegalStateException; +import com.palantir.logsafe.exceptions.SafeNullPointerException; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +/** + * Deprecated in favor of {@code com.palantir.logsafe.preconditions.Preconditions} in a separate package. + * This class has been migrated to support the java platform module system, which does not allow packages + * to be reused from different jars. + * + * @deprecated Prefer Preconditions in the {@code com.palantir.logsafe.preconditions} package. + */ +@Deprecated +public final class Preconditions { + private Preconditions() {} + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + * @param expression a boolean expression + * @throws SafeIllegalArgumentException if {@code expression} is false + */ + public static void checkArgument(boolean expression) { + if (!expression) { + throw new SafeIllegalArgumentException(); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + * @param expression a boolean expression + * @param message the loggable exception message + * @throws SafeIllegalArgumentException if {@code expression} is false + */ + public static void checkArgument(boolean expression, @CompileTimeConstant String message) { + if (!expression) { + throw new SafeIllegalArgumentException(message); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + *
See {@link #checkArgument(boolean, String, Arg...)} for details. + */ + public static void checkArgument(boolean expression, @CompileTimeConstant String message, Arg> arg) { + if (!expression) { + throw new SafeIllegalArgumentException(message, arg); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + *
See {@link #checkArgument(boolean, String, Arg...)} for details. + */ + public static void checkArgument( + boolean expression, @CompileTimeConstant String message, Arg> arg1, Arg> arg2) { + if (!expression) { + throw new SafeIllegalArgumentException(message, arg1, arg2); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + *
See {@link #checkArgument(boolean, String, Arg...)} for details. + */ + public static void checkArgument( + boolean expression, @CompileTimeConstant String message, Arg> arg1, Arg> arg2, Arg> arg3) { + if (!expression) { + throw new SafeIllegalArgumentException(message, arg1, arg2, arg3); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + * @param expression a boolean expression + * @param message the loggable exception message + * @param args the arguments to include in the {@link SafeIllegalArgumentException} + * @throws SafeIllegalArgumentException if {@code expression} is false + */ + public static void checkArgument(boolean expression, @CompileTimeConstant String message, Arg>... args) { + if (!expression) { + throw new SafeIllegalArgumentException(message, args); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters + * to the calling method. + * + * @param expression a boolean expression + * @throws SafeIllegalStateException if {@code expression} is false + */ + public static void checkState(boolean expression) { + if (!expression) { + throw new SafeIllegalStateException(); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters + * to the calling method. + * + * @param expression a boolean expression + * @param message the loggable exception message + * @throws SafeIllegalStateException if {@code expression} is false + */ + public static void checkState(boolean expression, @CompileTimeConstant String message) { + if (!expression) { + throw new SafeIllegalStateException(message); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + *
See {@link #checkState(boolean, String, Arg...)} for details. + */ + public static void checkState(boolean expression, @CompileTimeConstant String message, Arg> arg) { + if (!expression) { + throw new SafeIllegalStateException(message, arg); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + *
See {@link #checkState(boolean, String, Arg...)} for details. + */ + public static void checkState(boolean expression, @CompileTimeConstant String message, Arg> arg1, Arg> arg2) { + if (!expression) { + throw new SafeIllegalStateException(message, arg1, arg2); + } + } + + /** + * Ensures the truth of an expression involving one or more parameters to the calling method. + * + *
See {@link #checkState(boolean, String, Arg...)} for details.
+ */
+ public static void checkState(
+ boolean expression, @CompileTimeConstant String message, Arg> arg1, Arg> arg2, Arg> arg3) {
+ if (!expression) {
+ throw new SafeIllegalStateException(message, arg1, arg2, arg3);
+ }
+ }
+
+ /**
+ * Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters
+ * to the calling method.
+ *
+ * @param expression a boolean expression
+ * @param message the loggable exception message
+ * @param args the arguments to include in the {@link SafeIllegalStateException}
+ * @throws SafeIllegalStateException if {@code expression} is false
+ */
+ public static void checkState(boolean expression, @CompileTimeConstant String message, Arg>... args) {
+ if (!expression) {
+ throw new SafeIllegalStateException(message, args);
+ }
+ }
+
+ /**
+ * Ensures that an Object reference passed as a parameter to the calling method is not null.
+ *
+ * @param reference an String reference
+ * @return the non-null reference that was validated
+ * @throws SafeNullPointerException if {@code reference} is null
+ */
+ @Nonnull
+ @CanIgnoreReturnValue
+ public static See {@link #checkNotNull(Object, String, Arg...)} for details.
+ */
+ @Nonnull
+ @CanIgnoreReturnValue
+ public static See {@link #checkNotNull(Object, String, Arg...)} for details.
+ */
+ @Nonnull
+ @CanIgnoreReturnValue
+ public static See {@link #checkNotNull(Object, String, Arg...)} for details.
+ */
+ @Nonnull
+ @CanIgnoreReturnValue
+ public static