All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nakedobjects.metamodel.commons.ensure.Ensure Maven / Gradle / Ivy

The newest version!
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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy