1 /*
2  * Copyright (C) 2020 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.storage.util;
18 
19 /** Useful condition check methods. */
20 public final class Conditions {
21 
Conditions()22     private Conditions() {
23     }
24 
25     /**
26      * Throws {@link IllegalArgumentException} with a suitable message if value is outside of the
27      * inclusive range specified.
28      */
checkArgInRange(String valueName, int value, int minIncl, int maxIncl)29     public static void checkArgInRange(String valueName, int value, int minIncl, int maxIncl) {
30         if (minIncl > value || value > maxIncl) {
31             throw new IllegalArgumentException(
32                     valueName + "=" + value + " must be between " + minIncl + " and " + maxIncl);
33         }
34     }
35 
36     /**
37      * Throws {@link IllegalArgumentException} with a suitable message if value is outside of the
38      * inclusive range specified.
39      */
checkArgInRange( String valueName, int value, String minName, int minIncl, String maxName, int maxIncl)40     public static void checkArgInRange(
41             String valueName, int value, String minName, int minIncl, String maxName, int maxIncl) {
42         if (minIncl > value || value > maxIncl) {
43             throw new IllegalArgumentException(valueName + "=" + value + " must be between "
44                     + minName + "=" + minIncl + " and " + maxName + "=" + maxIncl);
45         }
46     }
47 
48     /**
49      * Throws {@link IllegalStateException} with a suitable message if value is outside of the
50      * inclusive range specified.
51      */
checkStateInRange( String valueName, int value, String minName, int minIncl, String maxName, int maxIncl)52     public static void checkStateInRange(
53             String valueName, int value, String minName, int minIncl, String maxName, int maxIncl) {
54         if (minIncl > value || value > maxIncl) {
55             throw new IllegalStateException(valueName + "=" + value + " must be between "
56                     + minName + "=" + minIncl + " and " + maxName + "=" + maxIncl);
57         }
58     }
59 }
60