• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.media.util;
18 
19 public final class Preconditions {
20 
21     /**
22      * Ensures that that the argument numeric value is non-negative (greater than or equal to 0).
23      *
24      * @param value a numeric int value
25      * @return the validated numeric value
26      * @throws IllegalArgumentException if {@code value} was negative
27      */
checkArgumentNonnegative(final int value)28     public static int checkArgumentNonnegative(final int value) {
29         if (value < 0) {
30             throw new IllegalArgumentException();
31         }
32 
33         return value;
34     }
35 
36     /**
37      * Ensures that the argument int value is within the inclusive range.
38      *
39      * @param value a int value
40      * @param lower the lower endpoint of the inclusive range
41      * @param upper the upper endpoint of the inclusive range
42      * @param valueName the name of the argument to use if the check fails
43      *
44      * @return the validated int value
45      *
46      * @throws IllegalArgumentException if {@code value} was not within the range
47      */
checkArgumentInRange(int value, int lower, int upper, String valueName)48     public static int checkArgumentInRange(int value, int lower, int upper,
49             String valueName) {
50         if (value < lower) {
51             throw new IllegalArgumentException(
52                     String.format(
53                             "%s is out of range of [%d, %d] (too low)", valueName, lower, upper));
54         } else if (value > upper) {
55             throw new IllegalArgumentException(
56                     String.format(
57                             "%s is out of range of [%d, %d] (too high)", valueName, lower, upper));
58         }
59 
60         return value;
61     }
62 }
63