001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.util;
029
030import java.util.Collection;
031
032public class Args {
033
034    public static void check(final boolean expression, final String message) {
035        if (!expression) {
036            throw new IllegalArgumentException(message);
037        }
038    }
039
040    public static void check(final boolean expression, final String message, final Object... args) {
041        if (!expression) {
042            throw new IllegalArgumentException(String.format(message, args));
043        }
044    }
045
046    public static void check(final boolean expression, final String message, final Object arg) {
047        if (!expression) {
048            throw new IllegalArgumentException(String.format(message, arg));
049        }
050    }
051
052    public static <T> T notNull(final T argument, final String name) {
053        if (argument == null) {
054            throw new IllegalArgumentException(name + " may not be null");
055        }
056        return argument;
057    }
058
059    public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
060        if (argument == null) {
061            throw new IllegalArgumentException(name + " may not be null");
062        }
063        if (TextUtils.isEmpty(argument)) {
064            throw new IllegalArgumentException(name + " may not be empty");
065        }
066        return argument;
067    }
068
069    public static <T extends CharSequence> T notBlank(final T argument, final String name) {
070        if (argument == null) {
071            throw new IllegalArgumentException(name + " may not be null");
072        }
073        if (TextUtils.isBlank(argument)) {
074            throw new IllegalArgumentException(name + " may not be blank");
075        }
076        return argument;
077    }
078
079    public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {
080        if (argument == null) {
081            throw new IllegalArgumentException(name + " may not be null");
082        }
083        if (argument.length() == 0) {
084            throw new IllegalArgumentException(name + " may not be empty");
085        }
086        if (TextUtils.containsBlanks(argument)) {
087            throw new IllegalArgumentException(name + " may not contain blanks");
088        }
089        return argument;
090    }
091
092    public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
093        if (argument == null) {
094            throw new IllegalArgumentException(name + " may not be null");
095        }
096        if (argument.isEmpty()) {
097            throw new IllegalArgumentException(name + " may not be empty");
098        }
099        return argument;
100    }
101
102    public static int positive(final int n, final String name) {
103        if (n <= 0) {
104            throw new IllegalArgumentException(name + " may not be negative or zero");
105        }
106        return n;
107    }
108
109    public static long positive(final long n, final String name) {
110        if (n <= 0) {
111            throw new IllegalArgumentException(name + " may not be negative or zero");
112        }
113        return n;
114    }
115
116    public static int notNegative(final int n, final String name) {
117        if (n < 0) {
118            throw new IllegalArgumentException(name + " may not be negative");
119        }
120        return n;
121    }
122
123    public static long notNegative(final long n, final String name) {
124        if (n < 0) {
125            throw new IllegalArgumentException(name + " may not be negative");
126        }
127        return n;
128    }
129
130}