001    /*
002     * Copyright (c) 2011, Cloudera, Inc. All Rights Reserved.
003     *
004     * Cloudera, Inc. licenses this file to you under the Apache License,
005     * Version 2.0 (the "License"). You may not use this file except in
006     * compliance with the License. You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
011     * CONDITIONS OF ANY KIND, either express or implied. See the License for
012     * the specific language governing permissions and limitations under the
013     * License.
014     */
015    package com.cloudera.lib.util;
016    
017    import java.text.MessageFormat;
018    import java.util.List;
019    import java.util.regex.Pattern;
020    
021    /**
022     * Utility methods to check preconditions.
023     * <p/>
024     * Commonly used for method arguments preconditions.
025     */
026    public class Check {
027    
028      /**
029       * Verifies a variable is not NULL.
030       *
031       * @param obj the variable to check.
032       * @param name the name to use in the exception message.
033       * @return the variable.
034       * @throws IllegalArgumentException if the variable is NULL.
035       */
036      public static <T> T notNull(T obj, String name) {
037        if (obj == null) {
038          throw new IllegalArgumentException(name + " cannot be null");
039        }
040        return obj;
041      }
042    
043      /**
044       * Verifies a list does not have any NULL elements.
045       *
046       * @param list the list to check.
047       * @param name the name to use in the exception message.
048       * @return the list.
049       * @throws IllegalArgumentException if the list has NULL elements.
050       */
051      public static <T> List<T> notNullElements(List<T> list, String name) {
052        notNull(list, name);
053        for (int i = 0; i < list.size(); i++) {
054          notNull(list.get(i), MessageFormat.format("list [{0}] element [{1}]", name, i));
055        }
056        return list;
057      }
058    
059      /**
060       * Verifies a string is not NULL and not emtpy
061       *
062       * @param str the variable to check.
063       * @param name the name to use in the exception message.
064       * @return the variable.
065       * @throws IllegalArgumentException if the variable is NULL or empty.
066       */
067      public static String notEmpty(String str, String name) {
068        if (str == null) {
069          throw new IllegalArgumentException(name + " cannot be null");
070        }
071        if (str.length() == 0) {
072          throw new IllegalArgumentException(name + " cannot be empty");
073        }
074        return str;
075      }
076    
077      /**
078       * Verifies a string list is not NULL and not emtpy
079       *
080       * @param list the list to check.
081       * @param name the name to use in the exception message.
082       * @return the variable.
083       * @throws IllegalArgumentException if the string list has NULL or empty
084       * elements.
085       */
086      public static List<String> notEmptyElements(List<String> list, String name) {
087        notNull(list, name);
088        for (int i = 0; i < list.size(); i++) {
089          notEmpty(list.get(i), MessageFormat.format("list [{0}] element [{1}]", name, i));
090        }
091        return list;
092      }
093    
094      private static final String IDENTIFIER_PATTERN_STR = "[a-zA-z_][a-zA-Z0-9_\\-]*";
095    
096      private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("^" + IDENTIFIER_PATTERN_STR + "$");
097    
098      /**
099       * Verifies a value is a valid identifier,
100       * <code>[a-zA-z_][a-zA-Z0-9_\-]*</code>, up to a maximum length.
101       *
102       *
103       * @param value string to check if it is a valid identifier.
104       * @param maxLen maximun length.
105       * @param name the name to use in the exception message.
106       * @return the value.
107       * @throws IllegalArgumentException if the string is not a valid identifier.
108       */
109      public static String validIdentifier(String value, int maxLen, String name) {
110        Check.notEmpty(value, name);
111        if (value.length() > maxLen) {
112          throw new IllegalArgumentException(
113            MessageFormat.format("[{0}] = [{1}] exceeds max len [{2}]", name, value, maxLen));
114        }
115        if (!IDENTIFIER_PATTERN.matcher(value).find()) {
116          throw new IllegalArgumentException(
117            MessageFormat.format("[{0}] = [{1}] must be '{2}'", name, value, IDENTIFIER_PATTERN_STR));
118        }
119        return value;
120      }
121    
122      /**
123       * Verifies an integer is greater than zero.
124       *
125       * @param value integer value.
126       * @param name the name to use in the exception message.
127       * @return the value.
128       * @throws IllegalArgumentException if the integer is zero or less.
129       */
130      public static int gt0(int value, String name) {
131        return (int) gt0((long) value, name);
132      }
133    
134      /**
135       * Verifies an long is greater than zero.
136       *
137       * @param value long value.
138       * @param name the name to use in the exception message.
139       * @return the value.
140       * @throws IllegalArgumentException if the long is zero or less.
141       */
142      public static long gt0(long value, String name) {
143        if (value <= 0) {
144          throw new IllegalArgumentException(
145            MessageFormat.format("parameter [{0}] = [{1}] must be greater than zero", name, value));
146        }
147        return value;
148      }
149    
150      /**
151       * Verifies an integer is greater or equal to zero.
152       *
153       * @param value integer value.
154       * @param name the name to use in the exception message.
155       * @return the value.
156       * @throws IllegalArgumentException if the integer is greater or equal to zero.
157       */
158      public static int ge0(int value, String name) {
159        return (int) ge0((long) value, name);
160      }
161    
162      /**
163       * Verifies an long is greater or equal to zero.
164       *
165       * @param value integer value.
166       * @param name the name to use in the exception message.
167       * @return the value.
168       * @throws IllegalArgumentException if the long is greater or equal to zero.
169       */
170      public static long ge0(long value, String name) {
171        if (value < 0) {
172          throw new IllegalArgumentException(MessageFormat.format(
173            "parameter [{0}] = [{1}] must be greater than or equals zero", name, value));
174        }
175        return value;
176      }
177    
178    }