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 T checkNotNull(@Nullable T reference) { + if (reference == null) { + throw new SafeNullPointerException(); + } + return reference; + } + + /** + * Ensures that an Object reference passed as a parameter to the calling method is not null. + * + * @param reference an String reference + * @param message the loggable exception message + * @return the non-null reference that was validated + * @throws SafeNullPointerException if {@code reference} is null + */ + @Nonnull + @CanIgnoreReturnValue + public static T checkNotNull(@Nullable T reference, @CompileTimeConstant String message) { + if (reference == null) { + throw new SafeNullPointerException(message); + } + return reference; + } + + /** + * Ensures that an Object reference passed as a parameter to the calling method is not null. + * + *

See {@link #checkNotNull(Object, String, Arg...)} for details. + */ + @Nonnull + @CanIgnoreReturnValue + public static T checkNotNull(@Nullable T reference, @CompileTimeConstant String message, Arg arg) { + if (reference == null) { + throw new SafeNullPointerException(message, arg); + } + return reference; + } + + /** + * Ensures that an Object reference passed as a parameter to the calling method is not null. + * + *

See {@link #checkNotNull(Object, String, Arg...)} for details. + */ + @Nonnull + @CanIgnoreReturnValue + public static T checkNotNull( + @Nullable T reference, @CompileTimeConstant String message, Arg arg1, Arg arg2) { + if (reference == null) { + throw new SafeNullPointerException(message, arg1, arg2); + } + return reference; + } + + /** + * Ensures that an Object reference passed as a parameter to the calling method is not null. + * + *

See {@link #checkNotNull(Object, String, Arg...)} for details. + */ + @Nonnull + @CanIgnoreReturnValue + public static T checkNotNull( + @Nullable T reference, @CompileTimeConstant String message, Arg arg1, Arg arg2, Arg arg3) { + if (reference == null) { + throw new SafeNullPointerException(message, arg1, arg2, arg3); + } + return reference; + } + + /** + * Ensures that an Object reference passed as a parameter to the calling method is not null. + * + * @param reference an String reference + * @param message the loggable exception message + * @param args the arguments to include in the {@link SafeNullPointerException} + * @return the non-null reference that was validated + * @throws SafeNullPointerException if {@code reference} is null + */ + @Nonnull + @CanIgnoreReturnValue + public static T checkNotNull(@Nullable T reference, @CompileTimeConstant String message, Arg... args) { + if (reference == null) { + throw new SafeNullPointerException(message, args); + } + return reference; + } +} diff --git a/preconditions/build.gradle b/preconditions/build.gradle index 0f4aeb1d..52a8bcb7 100644 --- a/preconditions/build.gradle +++ b/preconditions/build.gradle @@ -3,9 +3,8 @@ apply plugin: 'com.palantir.revapi' dependencies { api project(':safe-logging') + api project(':safe-logging-exceptions') + api project(':preconditions-legacy') compileOnly 'com.google.code.findbugs:jsr305' api 'com.google.errorprone:error_prone_annotations' - - testImplementation 'junit:junit' - testImplementation 'org.assertj:assertj-core' } diff --git a/preconditions/src/main/java/com/palantir/logsafe/Preconditions.java b/preconditions/src/main/java/com/palantir/logsafe/preconditions/Preconditions.java similarity index 99% rename from preconditions/src/main/java/com/palantir/logsafe/Preconditions.java rename to preconditions/src/main/java/com/palantir/logsafe/preconditions/Preconditions.java index 0c6dd290..73c6eede 100755 --- a/preconditions/src/main/java/com/palantir/logsafe/Preconditions.java +++ b/preconditions/src/main/java/com/palantir/logsafe/preconditions/Preconditions.java @@ -27,10 +27,11 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ -package com.palantir.logsafe; +package com.palantir.logsafe.preconditions; import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CompileTimeConstant; +import com.palantir.logsafe.Arg; import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; import com.palantir.logsafe.exceptions.SafeIllegalStateException; import com.palantir.logsafe.exceptions.SafeNullPointerException; diff --git a/safe-logging-exceptions/build.gradle b/safe-logging-exceptions/build.gradle new file mode 100644 index 00000000..0f4aeb1d --- /dev/null +++ b/safe-logging-exceptions/build.gradle @@ -0,0 +1,11 @@ +apply from: "${rootDir}/gradle/publish-jar.gradle" +apply plugin: 'com.palantir.revapi' + +dependencies { + api project(':safe-logging') + compileOnly 'com.google.code.findbugs:jsr305' + api 'com.google.errorprone:error_prone_annotations' + + testImplementation 'junit:junit' + testImplementation 'org.assertj:assertj-core' +} diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptions.java b/safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptions.java similarity index 100% rename from preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptions.java rename to safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptions.java diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java b/safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java similarity index 100% rename from preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java rename to safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalStateException.java b/safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalStateException.java similarity index 100% rename from preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalStateException.java rename to safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalStateException.java diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIoException.java b/safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeIoException.java similarity index 100% rename from preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIoException.java rename to safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeIoException.java diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeNullPointerException.java b/safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeNullPointerException.java similarity index 100% rename from preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeNullPointerException.java rename to safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeNullPointerException.java diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeRuntimeException.java b/safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeRuntimeException.java similarity index 100% rename from preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeRuntimeException.java rename to safe-logging-exceptions/src/main/java/com/palantir/logsafe/exceptions/SafeRuntimeException.java diff --git a/preconditions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionTest.java b/safe-logging-exceptions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionTest.java similarity index 100% rename from preconditions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionTest.java rename to safe-logging-exceptions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionTest.java diff --git a/preconditions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionsTest.java b/safe-logging-exceptions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionsTest.java similarity index 100% rename from preconditions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionsTest.java rename to safe-logging-exceptions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionsTest.java diff --git a/settings.gradle b/settings.gradle index 37098e47..7c23b74f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,7 +1,9 @@ rootProject.name = 'safe-logging-root' include 'safe-logging' +include 'safe-logging-exceptions' include 'safe-logging-refactorings' include 'preconditions' +include 'preconditions-legacy' include 'preconditions-assertj'