View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.util;
18  
19  /**
20   * This class provides methods to check arguments of a variable number for null values, or anything else that might be required on a routine basis. These
21   * methods should be used for early failures as close to the end user as possible, so things do not fail later on the server side, when they are harder to
22   * debug.
23   * 
24   * Methods are created for a specific number of arguments, due to the poor performance of array allocation for varargs methods.
25   */
26  public class ArgumentChecker {
27    private static final String NULL_ARG_MSG = "argument was null";
28    
29    public static final void notNull(final Object arg1) {
30      if (arg1 == null)
31        throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null));
32    }
33    
34    public static final void notNull(final Object arg1, final Object arg2) {
35      if (arg1 == null || arg2 == null)
36        throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null) + " arg2? " + (arg2 == null));
37    }
38    
39    public static final void notNull(final Object arg1, final Object arg2, final Object arg3) {
40      if (arg1 == null || arg2 == null || arg3 == null)
41        throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null) + " arg2? " + (arg2 == null) + " arg3? " + (arg3 == null));
42    }
43    
44    public static final void notNull(final Object arg1, final Object arg2, final Object arg3, final Object arg4) {
45      if (arg1 == null || arg2 == null || arg3 == null || arg4 == null)
46        throw new IllegalArgumentException(NULL_ARG_MSG + ":Is null- arg1? " + (arg1 == null) + " arg2? " + (arg2 == null) + " arg3? " + (arg3 == null)
47            + " arg4? " + (arg4 == null));
48    }
49    
50    public static final void notNull(final Object[] args) {
51      if (args == null)
52        throw new IllegalArgumentException(NULL_ARG_MSG + ":arg array is null");
53      
54      for (int i = 0; i < args.length; i++)
55        if (args[i] == null)
56          throw new IllegalArgumentException(NULL_ARG_MSG + ":arg" + i + " is null");
57    }
58    
59    public static final void strictlyPositive(final int i) {
60      if (i <= 0)
61        throw new IllegalArgumentException("integer should be > 0, was " + i);
62    }
63  }