Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* * Copyright SpringSource Inc 2007,2009. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.springsource.util.common;import java.util.Collection;import java.util.Map;
A set of useful assertions based on those provided by the Spring Framework's Assert class. Concurrent Semantics
This class is thread safe.

Since:
Jersey
See also:
org.junit.Assert
  public final class Assert {    
Assert a boolean expression, throwing a IllegalArgumentException if the test result is false.
 Assert.isTrue(i > 0, "The value must be greater than zero");
 

Parameters:
expression a boolean expression
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if expression is false
      public static void isTrue(boolean expressionString messageObject... inserts) {    	if (!expression) {    		throw new IllegalArgumentException(String.format(messageinserts));    	}    }        
Assert a boolean expression, throwing a IllegalArgumentException if the test result is true.
 Assert.isFalse(state.isBroken(), "The state is broken");
 

Parameters:
expression a boolean expression
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if expression is false
      public static void isFalse(boolean expressionString messageObject... inserts) {        if (expression) {        	throw new IllegalArgumentException(String.format(messageinserts));        }    }    
Assert that an object is null.
 Assert.isNull(value, "The value must be null");
 

Parameters:
object the object to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the object is not null
      public static void isNull(Object objectString messageObject... inserts) {    	if (object != null) {    		throw new IllegalArgumentException(String.format(messageinserts));    	}    }    
Assert that an object is not null.
 Assert.notNull(clazz, "The class must not be null");
 

Parameters:
object the object to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the object is null
      public static void notNull(Object objectString messageObject... inserts) {        if (object == null) {        	throw new IllegalArgumentException(String.format(messageinserts));        }    }    
Assert that the given String is not empty; that is, it must not be null and not the empty String.
 Assert.hasLength(name, "Name must not be empty");
 

Parameters:
text the String to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
See also:
StringUtils.hasLength(java.lang.CharSequence)
      public static void hasLength(String textString messageObject... inserts) {        if (text == null || text.length() == 0) {        	throw new IllegalArgumentException(String.format(messageinserts));        }    }    
Assert that an array has elements; that is, it must not be null and must have at least one element.
 Assert.notEmpty(array, "The array must have elements");
 

Parameters:
array the array to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the object array is null or has no elements
      public static void notEmpty(Object[] arrayString messageObject... inserts) {        if (array == null || array.length == 0) {            throw new IllegalArgumentException(String.format(messageinserts));        }    }    
Assert that a collection has elements; that is, it must not be null and must have at least one element.
 Assert.notEmpty(collection, "Collection must have elements");
 

Parameters:
<T> the type of the elements of the collection
collection the collection to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the collection is null or has no elements
      public static <T> void notEmpty(Collection<T> collectionString messageObject... inserts) {    	if (collection == null || collection.isEmpty()) {            throw new IllegalArgumentException(String.format(messageinserts));        }    }    
Assert that a Map has entries; that is, it must not be null and must have at least one entry.
 Assert.notEmpty(map, "Map must have entries");
 

Parameters:
<K> type of Key (domain)
<V> type of Value (range)
map the map to check
message the exception message to use if the assertion fails
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the map is null or has no entries
      public static <K, V> void notEmpty(Map<K, V> mapString messageObject... inserts) {        if (map == null || map.isEmpty()) {            throw new IllegalArgumentException(String.format(messageinserts));        }    }    
Assert that the provided object is a non-null instance of the provided class.
 Assert.instanceOf(Foo.class, foo);
 

Parameters:
<T> the type to check this for
type the type to check against
obj the object to check
message a message which will be prepended to the message produced by the function itself, and which may be used to provide context. It should normally end in a ": " or ". " so that the function generate message looks ok when appended to it.
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the object is not an instance of clazz
See also:
java.lang.Class.isInstance(java.lang.Object)
      public static <T> void isInstanceOf(Class<T> typeObject objString messageObject... inserts) {    	notNull(type"The type to check against must not be null");    	if (!type.isInstance(obj)) {            throw new IllegalArgumentException(String.format(messageinserts) + "Object of class [" + (obj != null ? obj.getClass().getName() : "null")                + "] must be an instance of " + type);        }    }    
Assert that superType.isAssignableFrom(subType) is true.
 Assert.isAssignable(Number.class, myClass);
 

Parameters:
<T> the supertype
<U> the subtype
superType the super type to check against
subType the sub type to check
message a message which will be prepended to the message produced by the function itself, and which may be used to provide context. It should normally end in a ": " or ". " so that the function generate message looks ok when appended to it.
inserts any inserts to include if the message is a format string.
Throws:
java.lang.IllegalArgumentException if the classes are not assignable
      public static <T, U> void isAssignable(Class<T> superTypeClass<U> subTypeString messageObject... inserts) {    	notNull(superType"Type to check against must not be null");    	if (subType == null || !superType.isAssignableFrom(subType)) {    		throw new IllegalArgumentException(String.format(messageinserts) + subType + " is not assignable to " + superType);        }    }    /*     * Prevent instantiation - Java does not allow final abstract classes.     */    private Assert() {    }}
New to GrepCode? Check out our FAQ X