All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bson.assertions.Assertions Maven / Gradle / Ivy

There is a newer version: 5.3.0-beta0
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 * Copyright (c) 2008-2014 Atlassian Pty Ltd
 *
 * 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 org.bson.assertions;

import javax.annotation.Nullable;

/**
 * 

Design by contract assertions.

This class is not part of the public API and may be removed or changed at any time.

*/ public final class Assertions { /** * Throw IllegalArgumentException if the value is null. * * @param name the parameter name * @param value the value that should not be null * @param the value type * @return the value * @throws IllegalArgumentException if value is null */ public static T notNull(final String name, final T value) { if (value == null) { throw new IllegalArgumentException(name + " can not be null"); } return value; } /** * Throw IllegalStateException if the condition if false. * * @param name the name of the state that is being checked * @param condition the condition about the parameter to check * @throws IllegalStateException if the condition is false */ public static void isTrue(final String name, final boolean condition) { if (!condition) { throw new IllegalStateException("state should be: " + name); } } /** * Throw IllegalArgumentException if the condition if false. * * @param name the name of the state that is being checked * @param condition the condition about the parameter to check * @throws IllegalArgumentException if the condition is false */ public static void isTrueArgument(final String name, final boolean condition) { if (!condition) { throw new IllegalArgumentException("state should be: " + name); } } /** * Throw IllegalArgumentException if the condition if false, otherwise return the value. This is useful when arguments must be checked * within an expression, as when using {@code this} to call another constructor, which must be the first line of the calling * constructor. * * @param the value type * @param name the name of the state that is being checked * @param value the value of the argument * @param condition the condition about the parameter to check * @return the value * @throws java.lang.IllegalArgumentException if the condition is false */ public static T isTrueArgument(final String name, final T value, final boolean condition) { if (!condition) { throw new IllegalArgumentException("state should be: " + name); } return value; } /** * @return Never completes normally. The return type is {@link AssertionError} to allow writing {@code throw fail()}. * This may be helpful in non-{@code void} methods. * @throws AssertionError Always */ public static AssertionError fail() throws AssertionError { throw new AssertionError(); } /** * @param msg The failure message. * @return Never completes normally. The return type is {@link AssertionError} to allow writing {@code throw fail("failure message")}. * This may be helpful in non-{@code void} methods. * @throws AssertionError Always */ public static AssertionError fail(final String msg) throws AssertionError { throw new AssertionError(assertNotNull(msg)); } /** * @param value A value to check. * @param The type of {@code value}. * @return {@code value} * @throws AssertionError If {@code value} is {@code null}. */ public static T assertNotNull(@Nullable final T value) throws AssertionError { if (value == null) { throw new AssertionError(); } return value; } /** * Cast an object to the given class and return it, or throw IllegalArgumentException if it's not assignable to that class. * * @param clazz the class to cast to * @param value the value to cast * @param errorMessage the error message to include in the exception * @param the Class type * @return value cast to clazz * @throws IllegalArgumentException if value is not assignable to clazz */ @SuppressWarnings("unchecked") public static T convertToType(final Class clazz, final Object value, final String errorMessage) { if (!clazz.isAssignableFrom(value.getClass())) { throw new IllegalArgumentException(errorMessage); } return (T) value; } private Assertions() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy