Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-515.v2.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions preconditions-legacy/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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.
*
* <p>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.
*
* <p>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.
*
* <p>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.
*
* <p>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.
*
* <p>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> 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> 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.
*
* <p>See {@link #checkNotNull(Object, String, Arg...)} for details.
*/
@Nonnull
@CanIgnoreReturnValue
public static <T> 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.
*
* <p>See {@link #checkNotNull(Object, String, Arg...)} for details.
*/
@Nonnull
@CanIgnoreReturnValue
public static <T> 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.
*
* <p>See {@link #checkNotNull(Object, String, Arg...)} for details.
*/
@Nonnull
@CanIgnoreReturnValue
public static <T> 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> T checkNotNull(@Nullable T reference, @CompileTimeConstant String message, Arg<?>... args) {
if (reference == null) {
throw new SafeNullPointerException(message, args);
}
return reference;
}
}
5 changes: 2 additions & 3 deletions preconditions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions safe-logging-exceptions/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -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'