001/*
002 * #%L
003 * Netarchivesuite - common
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.common.exceptions;
024
025import java.io.File;
026import java.util.Collection;
027
028/**
029 * Indicates that one or more arguments are invalid.
030 */
031@SuppressWarnings("serial")
032public class ArgumentNotValid extends NetarkivetException {
033    /**
034     * Constructs new ArgumentNotValid with the specified detail message.
035     *
036     * @param message The detail message
037     */
038    public ArgumentNotValid(String message) {
039        super(message);
040    }
041
042    /**
043     * Constructs new ArgumentNotValid with the specified detail message and cause.
044     *
045     * @param message The detail message
046     * @param cause The cause
047     */
048    public ArgumentNotValid(String message, Throwable cause) {
049        super(message, cause);
050    }
051
052    /**
053     * Check if a String argument is null or the empty string.
054     *
055     * @param val the value to check
056     * @param name the name and type of the value being checked
057     * @throws ArgumentNotValid if test fails
058     */
059    public static void checkNotNullOrEmpty(String val, String name) {
060        checkNotNull(val, name);
061
062        if (val.isEmpty()) {
063            throw new ArgumentNotValid("The value of the variable '" + name + "' must not be an empty string.");
064        }
065    }
066
067    /**
068     * Check if an Object argument is null.
069     *
070     * @param val the value to check
071     * @param name the name and type of the value being checked.
072     * @throws ArgumentNotValid if test fails
073     */
074    public static void checkNotNull(Object val, String name) {
075        if (val == null) {
076            throw new ArgumentNotValid("The value of the variable '" + name + "' must not be null.");
077        }
078    }
079
080    /**
081     * Check if an int argument is less than 0.
082     *
083     * @param num argument to check
084     * @param name the name and type of the value being checked.
085     * @throws ArgumentNotValid if test fails
086     */
087    public static void checkNotNegative(int num, String name) {
088        if (num < 0) {
089            throw new ArgumentNotValid("The value of the variable '" + name + "' must be non-negative, but is " + num
090                    + ".");
091        }
092    }
093
094    /**
095     * Check if a long argument is less than 0.
096     *
097     * @param num argument to check
098     * @param name the name and type of the value being checked.
099     * @throws ArgumentNotValid if test fails
100     */
101    public static void checkNotNegative(long num, String name) {
102        if (num < 0) {
103            throw new ArgumentNotValid("The value of the variable '" + name + "' must be non-negative, but is " + num
104                    + ".");
105        }
106    }
107
108    /**
109     * Check if an int argument is less than or equal to 0.
110     *
111     * @param num argument to check
112     * @param name the name and type of the value being checked.
113     * @throws ArgumentNotValid if test fails
114     */
115    public static void checkPositive(int num, String name) {
116        if (num <= 0) {
117            throw new ArgumentNotValid("The value of the variable '" + name + "' must be positive, but is " + num + ".");
118        }
119    }
120
121    /**
122     * Check if a long argument is less than 0.
123     *
124     * @param num argument to check
125     * @param name the name and type of the value being checked.
126     * @throws ArgumentNotValid if test fails
127     */
128    public static void checkPositive(long num, String name) {
129        if (num <= 0) {
130            throw new ArgumentNotValid("The value of the variable '" + name + "' must be positive, but is " + num + ".");
131        }
132    }
133
134    /**
135     * Check if a List argument is not null and the list is not empty.
136     *
137     * @param c argument to check
138     * @param name the name and type of the value being checked.
139     * @throws ArgumentNotValid if test fails
140     */
141    public static void checkNotNullOrEmpty(Collection<?> c, String name) {
142        checkNotNull(c, name);
143
144        if (c.isEmpty()) {
145            throw new ArgumentNotValid("The contents of the variable '" + name + "' must not be empty.");
146        }
147    }
148
149    /**
150     * Check that some condition on input parameters is true and throw an ArgumentNotValid if it is false.
151     *
152     * @param b the condition to check
153     * @param s the error message to be reported
154     * @throws ArgumentNotValid if b is false
155     */
156    public static void checkTrue(boolean b, String s) {
157        if (!b) {
158            throw new ArgumentNotValid(s);
159        }
160    }
161
162    /**
163     * Check, if the given argument is an existing directory.
164     *
165     * @param aDir a given File object.
166     * @param name Name of object
167     * @throws ArgumentNotValid If aDir is not an existing directory
168     */
169    public static void checkExistsDirectory(File aDir, String name) {
170        checkNotNull(aDir, name);
171        if (!aDir.isDirectory()) {
172            String message = "The file '" + aDir.getAbsolutePath() + "' does not exist or is not a directory.";
173            throw new ArgumentNotValid(message);
174        }
175    }
176
177    /**
178     * Check, if the given argument is an existing normal file.
179     *
180     * @param aFile a given File object.
181     * @param name Name of object
182     * @throws ArgumentNotValid If aDir is not an existing file
183     */
184    public static void checkExistsNormalFile(File aFile, String name) {
185        checkNotNull(aFile, name);
186        if (!aFile.isFile()) {
187            String message = "The file '" + aFile.getAbsolutePath() + "' does not exist or is not a normal file.";
188            throw new ArgumentNotValid(message);
189        }
190    }
191}