
org.ujoframework.criterion.BinaryCriterion Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ujo-orm Show documentation
Show all versions of ujo-orm Show documentation
Quick ORM implementation based on the UJO objects.
/*
* Copyright 2007-2010 Pavel Ponec
*
* 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.ujoframework.criterion;
import org.ujoframework.Ujo;
/**
* The BinaryCriterion implementation allows to join two another Criterions into the binary tree.
* @since 0.90
* @author Pavel Ponec
*/
public class BinaryCriterion extends Criterion {
final private Criterion crn1;
final private Criterion crn2;
final private BinaryOperator operator;
public BinaryCriterion
( final Criterion criterion1
, final BinaryOperator operator
, final Criterion criterion2
) {
this.crn1 = criterion1;
this.crn2 = criterion2;
this.operator = operator;
}
/** Returns the left node of the parrent */
@Override
public final Criterion getLeftNode() {
return crn1;
}
/** Returns the right node of the parrent */
@Override
public final Criterion getRightNode() {
return crn2;
}
/** Returns an operator */
@Override
public final BinaryOperator getOperator() {
return operator;
}
@Override
public boolean evaluate(UJO ujo) {
boolean e1 = crn1.evaluate(ujo);
switch (operator) {
case AND : return e1 && crn2.evaluate(ujo);
case OR : return e1 || crn2.evaluate(ujo);
case XOR : return e1 != crn2.evaluate(ujo);
case NAND : return !(e1 && crn2.evaluate(ujo));
case NOR : return !(e1 || crn2.evaluate(ujo));
case EQ : return e1 == crn2.evaluate(ujo) ;
case NOT : return !e1 ;
default:
throw new IllegalArgumentException("Unsupported operator: " + operator);
}
}
/** Is it a Binary Criterion */
@Override
final public boolean isBinary() {
return true;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
printNode(crn1, result);
result.append(' ').append(operator.name()).append(' ');
printNode(crn2, result);
return result.toString();
}
/** Print simple node */
private void printNode(final Criterion cn, final StringBuilder out) {
boolean parentheses = cn.getOperator()!=BinaryOperator.AND;
if (parentheses) out.append('(');
out.append(cn);
if (parentheses) out.append(')');
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy