1/*
2 * Copyright 2016 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
17package com.android.mediaframeworktest.helpers;
18
19import java.util.Collection;
20import java.util.Objects;
21
22/**
23 * Helper set of methods to perform precondition checks before starting method execution.
24 *
25 * <p>Typically used to sanity check arguments or the current object state.</p>
26 */
27/**
28 * (non-Javadoc)
29 * @see android.hardware.camera2.cts.helpers.Preconditions
30 */
31public final class Preconditions {
32
33    /**
34     * Checks that the value has the expected bitwise flags set.
35     *
36     * @param argName Name of the argument
37     * @param arg Argument to check
38     * @param flagsName Name of the bitwise flags
39     * @param flags Bit flags to check.
40     * @return arg
41     *
42     * @throws IllegalArgumentException if the bitwise flags weren't set
43     */
44    public static int checkBitFlags(String argName, int arg, String flagsName, int flags) {
45        if ((arg & flags) == 0) {
46            throw new IllegalArgumentException(
47                    String.format("Argument '%s' must have flags '%s' set", argName, flagsName));
48        }
49
50        return arg;
51    }
52
53    /**
54     * Checks that the value is {@link Object#equals equal} to the expected value.
55     *
56     * @param argName Name of the argument
57     * @param arg Argument to check
58     * @param expectedName Name of the expected value
59     * @param expectedValue Expected value
60     * @return arg
61     *
62     * @throws IllegalArgumentException if the values were not equal
63     */
64    public static <T> T checkEquals(String argName, T arg,
65            String expectedName, T expectedValue) {
66        if (!Objects.equals(arg, expectedValue)) {
67            throw new IllegalArgumentException(
68                    String.format(
69                            "Argument '%s' must be equal to '%s' (was '%s', but expected '%s')",
70                            argName, expectedName, arg, expectedValue));
71        }
72
73        return arg;
74    }
75
76    /**
77     * Checks that the value is not {@code null}.
78     *
79     * <p>
80     * Returns the value directly, so you can use {@code checkNotNull("value", value)} inline.
81     * </p>
82     *
83     * @param argName Name of the argument
84     * @param arg Argument to check
85     * @return arg
86     *
87     * @throws NullPointerException if arg was {@code null}
88     */
89    public static <T> T checkNotNull(String argName, T arg) {
90        if (arg == null) {
91            throw new NullPointerException("Argument '" + argName + "' must not be null");
92        }
93
94        return arg;
95    }
96
97    /**
98     * Checks that the value is not {@code null}.
99     *
100     * <p>
101     * Returns the value directly, so you can use {@code checkNotNull("value", value)} inline.
102     * </p>
103     *
104     * @param arg Argument to check
105     * @return arg
106     *
107     * @throws NullPointerException if arg was {@code null}
108     */
109    public static <T> T checkNotNull(T arg) {
110        return checkNotNull("", arg);
111    }
112
113    /**
114     * Checks that the state is currently {@link true}.
115     *
116     * @param message Message to raise an exception with if the state checking fails.
117     * @param state State to check
118     *
119     * @throws IllegalStateException if state was {@code false}
120     *
121     * @return The state value (always {@code true}).
122     */
123    public static boolean checkState(String message, boolean state) {
124        if (!state) {
125            throw new IllegalStateException(message);
126        }
127
128        return state;
129    }
130
131        /**
132     * Ensures that the {@link Collection} is not {@code null}, and none of its elements are
133     * {@code null}.
134     *
135     * @param value a {@link Collection} of boxed objects
136     * @param valueName the name of the argument to use if the check fails
137     *
138     * @return the validated {@link Collection}
139     *
140     * @throws NullPointerException if the {@code value} or any of its elements were {@code null}
141     */
142    public static <T> Collection<T> checkCollectionElementsNotNull(final Collection<T> value,
143            final String valueName) {
144        if (value == null) {
145            throw new NullPointerException(valueName + " must not be null");
146        }
147
148        long ctr = 0;
149        for (T elem : value) {
150            if (elem == null) {
151                throw new NullPointerException(
152                        String.format("%s[%d] must not be null", valueName, ctr));
153            }
154            ++ctr;
155        }
156
157        return value;
158    }
159
160    /**
161     * Ensures that the {@link Collection} is not {@code null}, and contains at least one element.
162     *
163     * @param value a {@link Collection} of boxed elements.
164     * @param valueName the name of the argument to use if the check fails.
165
166     * @return the validated {@link Collection}
167     *
168     * @throws NullPointerException if the {@code value} was {@code null}
169     * @throws IllegalArgumentException if the {@code value} was empty
170     */
171    public static <T> Collection<T> checkCollectionNotEmpty(final Collection<T> value,
172            final String valueName) {
173        if (value == null) {
174            throw new NullPointerException(valueName + " must not be null");
175        }
176        if (value.isEmpty()) {
177            throw new IllegalArgumentException(valueName + " is empty");
178        }
179        return value;
180    }
181
182    // Suppress default constructor for noninstantiability
183    private Preconditions() { throw new AssertionError(); }
184}
185