Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.nakedobjects.metamodel.commons.ensure;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
/**
* Uses the {@link Matcher Hamcrest API} as a means of verifying arguments and so on.
*/
public final class Ensure {
private Ensure() {}
/**
* To ensure that the provided argument is correct.
*
* @see #ensureThatArg(Object, Matcher,State)
* @see #ensureThatState(Object, Matcher, String)
* @see #ensureThatContext(Object, Matcher)
*
* @throws IllegalArgumentException if matcher does not {@link Matcher#matches(Object) match}.
*/
public static T ensureThatArg(final T object, final Matcher matcher) {
if (!matcher.matches(object)) {
throw new IllegalArgumentException(
"illegal argument, expected: " + descriptionOf(matcher));
}
return object;
}
/**
* To ensure that the provided argument is correct.
*
* @see #ensureThatArg(Object, Matcher)
* @see #ensureThatState(Object, Matcher, String)
* @see #ensureThatContext(Object, Matcher)
*
* @throws IllegalArgumentException if matcher does not {@link Matcher#matches(Object) match}.
*/
public static T ensureThatArg(final T arg, final Matcher matcher, final String message) {
if (!matcher.matches(arg)) {
throw new IllegalArgumentException(
message);
}
return arg;
}
/**
* To ensure that the current state of this object (instance fields) is correct.
*
* @see #ensureThatArg(Object, Matcher)
* @see #ensureThatContext(Object, Matcher)
* @see #ensureThatState(Object, Matcher, String)
*
* @throws IllegalStateException if matcher does not {@link Matcher#matches(Object) match}.
*/
public static T ensureThatState(final T field, final Matcher matcher) {
if (!matcher.matches(field)) {
throw new IllegalStateException("illegal argument, expected: " + descriptionOf(matcher));
}
return field;
}
/**
* To ensure that the current state of this object (instance fields) is correct.
*
* @see #ensureThatArg(Object, Matcher)
* @see #ensureThatContext(Object, Matcher)
* @see #ensureThatState(Object, Matcher)
*
* @throws IllegalStateException if matcher does not {@link Matcher#matches(Object) match}.
*/
public static T ensureThatState(final T field, final Matcher matcher, final String message) {
if (!matcher.matches(field)) {
throw new IllegalStateException(message);
}
return field;
}
/**
* To ensure that the current context (NakedObjectsContext) is correct.
*
* @see #ensureThatArg(Object, Matcher)
* @see #ensureThatState(Object, Matcher)
* @see #ensureThatContext(Object, Matcher, String)
*
* @throws IllegalThreadStateException if matcher does not {@link Matcher#matches(Object) match}.
*/
public static T ensureThatContext(final T contextProperty, final Matcher matcher) {
if (!matcher.matches(contextProperty)) {
throw new IllegalThreadStateException("illegal argument, expected: " + descriptionOf(matcher));
}
return contextProperty;
}
/**
* To ensure that the current context (NakedObjectsContext) is correct.
*
* @see #ensureThatArg(Object, Matcher)
* @see #ensureThatState(Object, Matcher)
* @see #ensureThatContext(Object, Matcher, String)
*
* @throws IllegalThreadStateException if matcher does not {@link Matcher#matches(Object) match}.
*/
public static T ensureThatContext(final T contextProperty, final Matcher matcher, final String message) {
if (!matcher.matches(contextProperty)) {
throw new IllegalThreadStateException(message);
}
return contextProperty;
}
private static String descriptionOf(final Matcher matcher) {
StringDescription stringDescription = new StringDescription();
matcher.describeTo(stringDescription);
String description = stringDescription.toString();
return description;
}
}