Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.eclipse.emf.texo.utils;

Collection of static utility methods for checking invariants. Methods will throw an java.lang.IllegalStateException or an java.lang.IllegalArgumentException if an invariant fails.

Author(s):
Martin Taal
  
  public class Check {

  
Always throws an IllegalStateException.

Parameters:
message The message for the exception to throw.
Throws:
java.lang.IllegalStateException
 
   public static void fail(final String message) {
     throw new IllegalStateException(message);
   }

  
Check if a value is true.

Parameters:
value The value to check.
message The message for the exception to throw if the value is not true.
Throws:
java.lang.IllegalStateException If the value is not true.
 
   public static void isTrue(final boolean valuefinal String message) {
     if (!value) {
       throw new IllegalStateException(message);
     }
   }

  
Check if a value is false.

Parameters:
value The value to check.
message The message for the exception to throw if the value is not false.
Throws:
java.lang.IllegalStateException If the value is not false.
 
   public static void isFalse(final boolean valuefinal String message) {
     if (value) {
       throw new IllegalStateException(message);
     }
   }

  
Check if a value is not null.

Parameters:
value The value to check.
message The message for the exception to throw if the value is null.
Throws:
java.lang.IllegalStateException If the value is null.
 
   public static void isNotNull(final Object valuefinal String message) {
     if (value == null) {
       throw new IllegalStateException(message);
     }
   }

  
Check if a value is not null. The message is specific for parameters

Parameters:
value The value to check.
parameterName The parameterName of the parameter which is checked
Throws:
java.lang.IllegalArgumentException If the value is null.
 
   public static void isNotNullArgument(final Object valuefinal String parameterName) {
     if (value == null) {
       throw new IllegalArgumentException(parameterName + " is not set."); //$NON-NLS-1$
     }
   }

  
Check if a value is null.

Parameters:
value The value to check.
message The message for the exception to throw if the value is not null.
Throws:
java.lang.IllegalStateException If the value is not null.
 
   public static void isNull(final Object valuefinal String message) {
     if (value != null) {
       throw new IllegalStateException(message);
     }
  }

  
Checks if an array is not empty (!= null and length > 0)..

Parameters:
array The array to check.
message The message for the exception to throw if the array is empty.
Throws:
java.lang.IllegalStateException If the array is empty or null.
  public static void isNotEmpty(final Object[] arrayfinal String message) {
    if (array == null || array.length == 0) {
      throw new IllegalStateException(message);
    }
  }

  
Checks if a String is set (!= null and length > 0)..

Parameters:
array The array to check.
message The message for the exception to throw if the String is not set.
Throws:
java.lang.IllegalStateException If the String is empty or null.
  public static void isNotEmpty(final String valuefinal String message) {
    if (value == null || value.length() == 0) {
      throw new IllegalStateException(message);
    }
  }

  
Checks if the passed object is of the class specified, null values are ignored

Parameters:
obj
expClass
Throws:
java.lang.IllegalStateException If the obj is not of (or inherits from) the expClass
  public static void isInstanceOf(final Object objfinal Class<?> expClass) {
    if (obj == null) {
      return;
    }
    if (!expClass.isAssignableFrom(obj.getClass())) {
      throw new IllegalStateException("Expected class: " //$NON-NLS-1$
          + expClass.getName() + " but object has class: "//$NON-NLS-1$ 
          + obj.getClass().getName());
    }
  }

  
Checks object memory equality.

Parameters:
obj1 the first object
obj2 the second object
Throws:
java.lang.IllegalStateException If obj1 != obj2
  public static void isSameObject(final Object obj1final Object obj2) {
    if (obj1 != obj2) {
      throw new IllegalStateException("Objects are not the same");//$NON-NLS-1$
    }
  }

  
Checks object memory inequality.

Parameters:
obj1 the first object
obj2 the second object
Throws:
java.lang.IllegalStateException If obj1 == obj2
  public static void isNotSameObject(final Object obj1final Object obj2) {
    if (obj1 == obj2) {
      throw new IllegalStateException("Objects are not the same");//$NON-NLS-1$
    }
  }

  
Constructor made private to prevent instantiation and subclassing.
  private Check() {
  }
New to GrepCode? Check out our FAQ X