View Javadoc

1   /*
2    * Copyright 2011 Vincent Behar
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  package org.rundeck.api.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  import java.util.Arrays;
21  import java.util.List;
22  
23  /**
24   * Utility class for assertions
25   *
26   * @author Vincent Behar
27   */
28  public class AssertUtil {
29  
30      /**
31       * Test if the given value is in the list
32       *
33       * @param errorMessage message
34       * @param value        value
35       * @param list         list
36       */
37      public static void inList(String errorMessage, Object value, List<Object> list) {
38          if (!list.contains(value)) {
39              throw new IllegalArgumentException(errorMessage + ": " + list);
40          }
41      }
42  
43      /**
44       * Test if the given value is in the list
45       *
46       * @param errorMessage message
47       * @param value        value
48       * @param list         list
49       */
50      public static void inList(String errorMessage, Object value, Object... list) {
51          inList(errorMessage, value, Arrays.asList(list));
52      }
53      /**
54       * Test if the given {@link Object} is null
55       *
56       * @param object
57       * @param errorMessage to be used if the object is null
58       * @throws IllegalArgumentException if the given object is null
59       */
60      public static void notNull(Object object, String errorMessage) throws IllegalArgumentException {
61          if (object == null) {
62              throw new IllegalArgumentException(errorMessage);
63          }
64      }
65  
66      /**
67       * Test if the given {@link String} is blank (null, empty or only whitespace)
68       *
69       * @param input string
70       * @param errorMessage to be used if the string is blank
71       * @throws IllegalArgumentException if the given string is blank
72       */
73      public static void notBlank(String input, String errorMessage) throws IllegalArgumentException {
74          if (StringUtils.isBlank(input)) {
75              throw new IllegalArgumentException(errorMessage);
76          }
77      }
78  
79  }