/** * Asserts that the specified first object is the same as (==) the specified second object. * * @param <T> * @param argument The argument to assert as the same as <code>object</code>. * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown * @param object The object to assert as the same as <code>argument</code>. * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will * be used. * @throws IllegalArgumentException If the specified objects are not the same. */ public static <T> void isSame( final T argument, String argumentName, final T object, String objectName ) { if (argument != object) { if (objectName == null) objectName = getObjectName(object); throw new IllegalArgumentException(CommonI18n.argumentMustBeSameAs.text(argumentName, objectName)); } }
/** * Asserts that the specified first object is not the same as (==) the specified second object. * * @param <T> * @param argument The argument to assert as not the same as <code>object</code>. * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown * @param object The object to assert as not the same as <code>argument</code>. * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will * be used. * @throws IllegalArgumentException If the specified objects are the same. */ public static <T> void isNotSame( final T argument, String argumentName, final T object, String objectName ) { if (argument == object) { if (objectName == null) objectName = getObjectName(object); throw new IllegalArgumentException(CommonI18n.argumentMustNotBeSameAs.text(argumentName, objectName)); } }
/** * Asserts that the specified first object is not {@link Object#equals(Object) equal to} the specified second object. This * method does take null references into consideration. * * @param <T> * @param argument The argument to assert equal to <code>object</code>. * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown * @param object The object to assert as equal to <code>argument</code>. * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will * be used. * @throws IllegalArgumentException If the specified objects are equals. */ public static <T> void isNotEquals( final T argument, String argumentName, final T object, String objectName ) { if (argument == null) { if (object != null) return; // fall through ... both are null } else { if (!argument.equals(object)) return; // handles object==null // fall through ... they are equal } if (objectName == null) objectName = getObjectName(object); throw new IllegalArgumentException(CommonI18n.argumentMustNotBeEquals.text(argumentName, objectName)); }
/** * Check that the collection contains the value * * @param argument Collection to check * @param value Value to check for, may be null * @param name The name of the argument * @throws IllegalArgumentException If collection is null or doesn't contain value */ public static void contains( Collection<?> argument, Object value, String name ) { isNotNull(argument, name); if (!argument.contains(value)) { throw new IllegalArgumentException(CommonI18n.argumentDidNotContainObject.text(name, getObjectName(value))); } }
/** * Asserts that the specified first object is {@link Object#equals(Object) equal to} the specified second object. This method * does take null references into consideration. * * @param <T> * @param argument The argument to assert equal to <code>object</code>. * @param argumentName The name that will be used within the exception message for the argument should an exception be thrown * @param object The object to assert as equal to <code>argument</code>. * @param objectName The name that will be used within the exception message for <code>object</code> should an exception be * thrown; if <code>null</code> and <code>object</code> is not <code>null</code>, <code>object.toString()</code> will * be used. * @throws IllegalArgumentException If the specified objects are not equal. */ public static <T> void isEquals( final T argument, String argumentName, final T object, String objectName ) { if (argument == null) { if (object == null) return; // fall through ... one is null } else { if (argument.equals(object)) return; // fall through ... they are not equal } if (objectName == null) objectName = getObjectName(object); throw new IllegalArgumentException(CommonI18n.argumentMustBeEquals.text(argumentName, objectName)); }
/** * Check that the map contains the key * * @param argument Map to check * @param key Key to check for, may be null * @param name The name of the argument * @throws IllegalArgumentException If map is null or doesn't contain key */ public static void containsKey( Map<?, ?> argument, Object key, String name ) { isNotNull(argument, name); if (!argument.containsKey(key)) { throw new IllegalArgumentException(CommonI18n.argumentDidNotContainKey.text(name, getObjectName(key))); } }