/* * Copyright (C) 2021 The Android Open Source Project * * 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.android.providers.media.util; import java.util.Locale; public final class Preconditions { /** * Ensures that that the argument numeric value is non-negative (greater than or equal to 0). * * @param value a numeric int value * @return the validated numeric value * @throws IllegalArgumentException if {@code value} was negative */ public static int checkArgumentNonnegative(final int value) { if (value < 0) { throw new IllegalArgumentException(); } return value; } /** * Ensures that the argument int value is within the inclusive range. * * @param value a int value * @param lower the lower endpoint of the inclusive range * @param upper the upper endpoint of the inclusive range * @param valueName the name of the argument to use if the check fails * * @return the validated int value * * @throws IllegalArgumentException if {@code value} was not within the range */ public static int checkArgumentInRange(int value, int lower, int upper, String valueName) { if (value < lower) { throw new IllegalArgumentException( String.format(Locale.ROOT, "%s is out of range of [%d, %d] (too low)", valueName, lower, upper)); } else if (value > upper) { throw new IllegalArgumentException( String.format(Locale.ROOT, "%s is out of range of [%d, %d] (too high)", valueName, lower, upper)); } return value; } }