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

com.speedment.common.invariant.IntRangeUtil Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

There is a newer version: 3.1.18
Show newest version
/**
 *
 * Copyright (c) 2006-2018, Speedment, 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.
 */
package com.speedment.common.invariant;

import java.util.function.Function;

/**
 *
 * @author Per Minborg
 */
public final class IntRangeUtil {

    /**
     * Returns the given value if it is positive.
     *
     * @param val to check
     * @return the given value
     * @throws IllegalArgumentException if the given value is not positive
     */
    public static int requirePositive(int val) {
        if (val < 1) {
            throw new IllegalArgumentException(val + " is not positive");
        }
        return val;
    }

    public static int requireNegative(int val) {
        if (val > -1) {
            throw new IllegalArgumentException(val + " is not negative");
        }
        return val;
    }

    public static int requireZero(int val) {
        if (val != 0) {
            throw new IllegalArgumentException(val + " is not zero");
        }
        return val;
    }

    public static int requireNonPositive(int val) {
        if (val > 0) {
            throw new IllegalArgumentException(val + " is positive");
        }
        return val;
    }

    public static int requireNonNegative(int val) {
        if (val < 0) {
            throw new IllegalArgumentException(val + " is negative");
        }
        return val;
    }

    public static int requireNonZero(int val) {
        if (val == 0) {
            throw new IllegalArgumentException(val + " is zero");
        }
        return val;
    }

    public static int requireEquals(int val, int otherVal) {
        if (val != otherVal) {
            throw new IllegalArgumentException(val + " is not equal to " + otherVal);
        }
        return val;
    }

    public static int requireNotEquals(int val, int otherVal) {
        if (val == otherVal) {
            throw new IllegalArgumentException(val + " is equal to " + otherVal);
        }
        return val;
    }

    public static int requireInRange(int val, int first, int lastExclusive) {
        if (val < first || val >= lastExclusive) {
            throw new IllegalArgumentException(val + " is not in the range [" + first + ", " + lastExclusive + ")");
        }
        return val;
    }

    public static int requireInRangeClosed(int val, int first, int lastInclusive) {
        if (val < first || val > lastInclusive) {
            throw new IllegalArgumentException(val + " is not in the range [" + first + ", " + lastInclusive + "]");
        }
        return val;
    }

    /**
     * Returns the given value if it is positive.
     *
     * @param  RuntimeException type
     * @param val to check
     * @param exceptionConstructor to use when throwing exception
     * @return the given value
     * @throws RuntimeException if the given value is not positive
     */
    public static  int requirePositive(int val, Function exceptionConstructor) {
        if (val < 1) {
            throw exceptionConstructor.apply(val + " is not positive");
        }
        return val;
    }

    public static  int requireNegative(int val, Function exceptionConstructor) {
        if (val > -1) {
            throw exceptionConstructor.apply(val + " is not negative");
        }
        return val;
    }

    public static  int requireZero(int val, Function exceptionConstructor) {
        if (val != 0) {
            throw exceptionConstructor.apply(val + " is not zero");
        }
        return val;
    }

    public static  int requireNonPositive(int val, Function exceptionConstructor) {
        if (val > 0) {
            throw exceptionConstructor.apply(val + " is positive");
        }
        return val;
    }

    public static  int requireNonNegative(int val, Function exceptionConstructor) {
        if (val < 0) {
            throw exceptionConstructor.apply(val + " is negative");
        }
        return val;
    }

    public static  int requireNonZero(int val, Function exceptionConstructor) {
        if (val == 0) {
            throw exceptionConstructor.apply(val + " is zero");
        }
        return val;
    }

    public static  int requireEquals(int val, int otherVal, Function exceptionConstructor) {
        if (val != otherVal) {
            throw exceptionConstructor.apply(val + " is not equal to " + otherVal);
        }
        return val;
    }

    public static  int requireNotEquals(int val, int otherVal, Function exceptionConstructor) {
        if (val == otherVal) {
            throw exceptionConstructor.apply(val + " is equal to " + otherVal);
        }
        return val;
    }

    public static  int requireInRange(int val, int first, int lastExclusive, Function exceptionConstructor) {
        if (val < first || val >= lastExclusive) {
            throw exceptionConstructor.apply(val + " is not in the range [" + first + ", " + lastExclusive + ")");
        }
        return val;
    }

    public static  int requireInRangeClosed(int val, int first, int lastInclusive, Function exceptionConstructor) {
        if (val < first || val > lastInclusive) {
            throw exceptionConstructor.apply(val + " is not in the range [" + first + ", " + lastInclusive + "]");
        }
        return val;
    }

    public IntRangeUtil() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy