1 /*
2  * JaLingo, http://jalingo.sourceforge.net/
3  *
4  * Copyright (c) 2002-2006 Oleksandr Shyshko
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 package ja.centre.util.assertions;
22 
23 import java.util.Collection;
24 import java.util.Iterator;
25 
26 public class Arguments {
Arguments()27     private Arguments() {
28     }
29 
30     // TODO swap args
assertNotNull( String argumentName, Object o )31     public static void assertNotNull( String argumentName, Object o ) throws NullPointerException {
32         if ( o == null ) {
33             throw new NullPointerException( "Argument \"" + argumentName + "\" can not be null." );
34         }
35     }
36 
assertNotEmpty( String argumentName, String argument )37     public static void assertNotEmpty( String argumentName, String argument ) {
38         assertNotNull( argumentName, argument );
39 
40         if ( argument.length() == 0 ) {
41             Arguments.doThrow( "String argument \"" + argumentName + "\" can not have zero length." );
42         }
43     }
assertNotEmpty( String argumentName, Collection collection )44     public static void assertNotEmpty( String argumentName, Collection collection ) {
45         assertNotNull( argumentName, collection );
46 
47         if ( collection.isEmpty() ) {
48             Arguments.doThrow( "Argument \"" + argumentName + "\" (which is a collection) can not be empty" );
49         }
50     }
assertNotEmpty( String argumentName, Object[] array )51     public static void assertNotEmpty( String argumentName, Object[] array ) {
52         assertNotNull( argumentName, array );
53 
54         if ( array.length == 0 ) {
55             Arguments.doThrow( "Argument \"" + argumentName + "\" (which is an array) can not be empty" );
56         }
57     }
58 
assertInstanceOf( String argumentName, Object object, Class<?> requiredType )59     public static void assertInstanceOf( String argumentName, Object object, Class<?> requiredType ) {
60         if ( !requiredType.isAssignableFrom( object.getClass() ) ) {
61             Arguments.doThrow( "Argument \"" + argumentName + "\" should be of type \"" + requiredType.getName() + "\"" );
62         }
63     }
assertInstacesOf( String argumentName, Collection collection, Class requiredType )64     public static void assertInstacesOf( String argumentName, Collection collection, Class requiredType ) {
65         assertNotNull( argumentName, collection );
66 
67         for ( Iterator i = collection.iterator(); i.hasNext(); ) {
68             assertInstanceOf( "item of: " + argumentName, i.next(), requiredType );
69         }
70     }
71 
assertPositiveNonZero( String argumentName, int value )72     public static void assertPositiveNonZero( String argumentName, int value ) {
73         if ( value <= 0 ) {
74             Arguments.doThrow( "Argument \"" + argumentName + "\" must be positive non zero integer. Actual value was " + value );
75         }
76     }
77 
assertInBounds( String argumentName, int value, int minValue, int maxValue )78     public static void assertInBounds( String argumentName, int value, int minValue, int maxValue ) {
79         if ( value < minValue || value > maxValue ) {
80             throw new IndexOutOfBoundsException( "Argument \"" + argumentName + "\" must be in range [" + minValue + ";" + maxValue + "]. Actual value was " + value );
81         }
82     }
83 
doThrow( String message )84     public static IllegalArgumentException doThrow( String message ) throws IllegalArgumentException {
85         throw new IllegalArgumentException( message );
86     }
doThrow( String message, Throwable cause )87     public static void doThrow( String message, Throwable cause ) {
88         throw new IllegalArgumentException( message, cause );
89     }
90 
doThrowNull( String message )91     public static NullPointerException doThrowNull( String message ) throws NullPointerException {
92         throw new NullPointerException( message );
93     }
94 }
95