net.sf.gluebooster.java.booster.basic.math.ConditionBasics Maven / Gradle / Ivy
package net.sf.gluebooster.java.booster.basic.math;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang.ObjectUtils;
import net.sf.gluebooster.java.booster.essentials.math.Condition;
import net.sf.gluebooster.java.booster.essentials.meta.HasName;
import net.sf.gluebooster.java.booster.essentials.objects.BoostedObject;
/**
* Abstract implementation of a condition with some methods already implemented.
*
* @author CBauer
*
* @param
* The concrete subclass
* @param
* Class of the names (probably String)
*/
public abstract class ConditionBasics
extends BoostedObject implements Condition {
@Override
public Collection implies(Condition condition2) throws Exception {
if (condition2 == null){
return Collections.EMPTY_LIST;
} else if (getClass().isInstance(condition2)) {
return impliesImplementation((Implementation) condition2);
} else {
return Arrays.asList("classes are too different to compute an implication");
}
}
@Override
public Collection isImpliedBy(Condition otherCondition) throws Exception {
if (otherCondition == null) {
return Arrays.asList("null cannot imply this");
}
return otherCondition.implies(this);
}
/**
* Subclasses must overwrite this method with the concrete implication logic
* if implications should be possible.
*
* @param condition2
* the implication
* @return true if this implies the condition2
*/
protected Collection impliesImplementation(Implementation condition2)
throws Exception {
throw new UnsupportedOperationException(
"this method must be overwritten to have effects");
}
@Override
public boolean isElementary() {
throw new UnsupportedOperationException(
"this method must be overwritten to have effects");
}
@Override
public Condition createEmptyCondition() {
throw new UnsupportedOperationException(
"this method must be overwritten to have effects");
}
/**
* Just puts this element into a set. This method must be overwritten if
* anything other should take place.
*/
@Override
public Set split() {
HashSet result = new HashSet(1);
result.add(this);
return null;
}
@Override
public Collection isInConflict(Condition condition) throws Exception {
throw new IllegalStateException("not yet implemented in "
+ getClass().getSimpleName());
}
@Override
public Condition difference(Condition condition) {
throw new IllegalStateException("not yet implemented");
}
@Override
public void remove(Condition condition) {
throw new IllegalStateException("not yet implemented");
}
/**
* Adds more constraints
*
* @param condition
* the additional constraints
*/
protected void addFrom(Implementation condition) throws Exception {
throw new UnsupportedOperationException(
"this method must be overwritten to have effects");
}
@Override
public void add(Condition... conditions) throws Exception {
for (Condition condition : conditions) {
if (getClass().isInstance(condition)) {
addFrom((Implementation) condition);
return;
}
}
}
/**
* Ist condition1 in conflict with condition2
*
* @param condition1
* the first condition
* @param condition2
* the second condition
* @return true if there is a conflict
*/
public static boolean isInConflict(Condition condition1,
Condition condition2) throws Exception {
if (condition1 == null || condition2 == null)
return false;
else {
Collection conflicts = condition1.isInConflict(condition2);
return conflicts != null && !conflicts.isEmpty();
}
}
}