org.cp.elements.biz.rules.AbstractRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cp-elements Show documentation
Show all versions of cp-elements Show documentation
Java Simplified. Extensions and Useful Constructs for the Java Platform.
Codeprimate Elements (a.k.a. cp-elements) is a Java library and micro-framework used to simplify
the development of software applications written in Java. Elements packages several APIs into one
library in order to address various application concerns and aspects of software design and development
collectively and conveniently. Elements is a highly simple, yet robust and proven library built on
solid OO principles, software design patterns and best practices to effectively solve common
and reoccurring problems in software development.
/*
* Copyright 2016 Author or Authors.
*
* 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 org.cp.elements.biz.rules;
import org.cp.elements.lang.Assert;
import org.cp.elements.lang.Constants;
/**
* The AbstractRule class is an abstract base class for encapsulating functionality common to all Rule implementations.
*
* @author John J. Blum
* @param the Class type of Objects evaluated by this business rule.
* @see org.cp.elements.biz.rules.Rule
* @since 1.0.0
*/
@SuppressWarnings("unused")
public abstract class AbstractRule> implements Rule {
protected static final boolean DEFAULT_EXPECTED_OUTCOME = true;
protected static final boolean DEFAULT_THROW_EXCEPTION_ON_FAILURE = false;
private volatile boolean expectedOutcome = DEFAULT_EXPECTED_OUTCOME;
private volatile boolean throwExceptionOnFailure = DEFAULT_THROW_EXCEPTION_ON_FAILURE;
private volatile ID id;
/**
* Gets the identifier for this Rule.
*
* @return the identifier for this Rule.
* @see org.cp.elements.lang.Identifiable#getId()
* @throws IllegalStateException if the identifier for this Rule was not properly set.
*/
public ID getId() {
Assert.state(id != null, "The identifier for Rule ({0}) was not properly initialized!", getClass().getName());
return id;
}
/**
* Sets the identifier for this Rule.
*
* @param id a value of type T assigned as this object's unique identifier.
* @see org.cp.elements.lang.Identifiable#setId(Comparable)
* @throws NullPointerException if the identifier for this Rule is null.
*/
public final void setId(final ID id) {
Assert.notNull(id, "The identifier for Rule ({0}) cannot be null!", getClass().getName());
this.id = id;
}
/**
* Unsupported operation for this Rule.
*
* @return boolean indicating whether this Rule is a newly created object.
* @throws UnsupportedOperationException operation not support by the Rule class.
*/
public boolean isNew() {
throw new UnsupportedOperationException(Constants.NOT_IMPLEMENTED);
}
/**
* Determines the outcome expected when evaluating an object with this business rule. The object is expected to
* satisfy or violate the criteria of this business rule when evaluated.
*
* @return the outcome expected when evaluating an object with this business rule.
* @see #evaluate(Object)
*/
public boolean getExpectedOutcome() {
return expectedOutcome;
}
/**
* Sets the expected outcome (pass or fail) of this business rule when evaluating an Object.
*
* @param expectedOutcome a boolean value indicating the expected outcome when evaluating an Object with this
* business rule.
* @see #evaluate(Object)
*/
protected final void setExpectedOutcome(final boolean expectedOutcome) {
this.expectedOutcome = expectedOutcome;
}
/**
* Indicates whether this business rule is configured to throw an exception on failure when evaluated. If the object
* evaluated by this business rule violates the criteria, then an exception is thrown.
*
* @return a boolean value indicating whether this business rule is configured to throw an exception on failure
* when evaluated.
* @see #evaluate(Object)
*/
public boolean isThrowExceptionOnFailure() {
return throwExceptionOnFailure;
}
/**
* Configures this business rule to throw or not throw an Exception on failure when an Object is evaluated.
*
* @param throwExceptionOnFailure a boolean value indicating whether this business rule should throw an Exception
* on failure when evaluated.
* @see #evaluate(Object)
*/
protected final void setThrowExceptionOnFailure(final boolean throwExceptionOnFailure) {
this.throwExceptionOnFailure = throwExceptionOnFailure;
}
}