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

io.helidon.build.util.Requirements Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020 Oracle and/or its affiliates.
 *
 * 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 io.helidon.build.util;

import java.util.Optional;

/**
 * Assertions with message strings formatted via {@link StyleRenderer#render(String, Object...)}.
 */
public class Requirements {
    /**
     * Throws a {@code RequirementFailure} with a message formatted via {@link StyleRenderer#render(String, Object...)} if the
     * given instance is {@code null}.
     *
     * @param instance The instance.
     * @param message The message.
     * @param args The message args.
     * @param  The instance type.
     * @return The instance.
     * @throws RequirementFailure if the condition returns {@code false}.
     */
    public static  T requireNonNull(T instance, String message, Object... args) {
        require(instance != null, message, args);
        return instance;
    }

    /**
     * Conditionally throws a {@code RequirementFailure} with a message formatted via {@link StyleRenderer#render(String, Object...)}.
     *
     * @param condition The condition.
     * @param message The message.
     * @param args The message args.
     * @throws RequirementFailure if the condition is {@code false}.
     */
    public static void require(boolean condition, String message, Object... args) {
        if (!condition) {
            failed(message, args);
        }
    }

    /**
     * Throws a {@code RequirementFailure} with a message formatted via {@link StyleRenderer#render(String, Object...)}.
     *
     * @param message The message.
     * @param args The message args.
     * @throws RequirementFailure always.
     */
    public static void failed(String message, Object... args) {
        throw new RequirementFailure(message, args);
    }

    /**
     * Convert the given error to a {@link RequirementFailure} if it is or was caused by this type.
     *
     * @param error The error.
     * @return The optional {@link RequirementFailure}.
     */
    public static Optional toFailure(Throwable error) {
        if (error == null) {
            return Optional.empty();
        } else if (error instanceof RequirementFailure) {
            return Optional.of((RequirementFailure) error);
        }
        // Recurse
        return toFailure(error.getCause()).or(Optional::empty);
    }

    private Requirements() {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy