001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * Created on May 14, 2007
017: */
018: package org.drools.rule;
019:
020: import java.util.Arrays;
021:
022: import org.drools.common.InternalWorkingMemory;
023: import org.drools.reteoo.ReteTuple;
024: import org.drools.rule.AbstractCompositeConstraint.MultiFieldConstraintContextEntry;
025: import org.drools.util.ArrayUtils;
026:
027: /**
028: * AND composite constraint, so the user can do things like:
029: *
030: * Person( name == 'Edson' && surname == 'Tirelli' )
031: *
032: * @author etirelli
033: *
034: */
035: public class AndConstraint extends AbstractCompositeConstraint {
036:
037: private static final long serialVersionUID = 400L;
038:
039: /**
040: * {@inheritDoc}
041: */
042: public boolean isAllowed(Object object,
043: InternalWorkingMemory workingMemory) {
044: if (this .alphaConstraints.length > 0) {
045: for (int i = 0; i < this .alphaConstraints.length; i++) {
046: if (!this .alphaConstraints[i].isAllowed(object,
047: workingMemory)) {
048: return false;
049: }
050: }
051: }
052: return true;
053: }
054:
055: /**
056: * {@inheritDoc}
057: */
058: public boolean isAllowedCachedLeft(ContextEntry context,
059: Object object) {
060: if (this .betaConstraints.length > 0) {
061: for (int i = 0; i < this .betaConstraints.length; i++) {
062: if (!this .betaConstraints[i]
063: .isAllowedCachedLeft(
064: ((MultiFieldConstraintContextEntry) context).contexts[i],
065: object)) {
066: return false;
067: }
068: }
069: }
070: return true;
071: }
072:
073: /**
074: * {@inheritDoc}
075: */
076: public boolean isAllowedCachedRight(ReteTuple tuple,
077: ContextEntry context) {
078: if (this .betaConstraints.length > 0) {
079: for (int i = 0; i < this .betaConstraints.length; i++) {
080: if (!this .betaConstraints[i]
081: .isAllowedCachedRight(
082: tuple,
083: ((MultiFieldConstraintContextEntry) context).contexts[i])) {
084: return false;
085: }
086: }
087: }
088: return true;
089: }
090:
091: public int hashCode() {
092: final int PRIME = 31;
093: int result = 1;
094: result = PRIME * result
095: + ArrayUtils.hashCode(this .alphaConstraints);
096: result = PRIME * result
097: + ArrayUtils.hashCode(this .betaConstraints);
098: result = PRIME * result
099: + ArrayUtils.hashCode(this .requiredDeclarations);
100: return result;
101: }
102:
103: public boolean equals(final Object object) {
104: if (this == object) {
105: return true;
106: }
107: if (object == null || object.getClass() != AndConstraint.class) {
108: return false;
109: }
110: final AndConstraint other = (AndConstraint) object;
111:
112: return Arrays.equals(this.alphaConstraints,
113: other.alphaConstraints)
114: && Arrays.equals(this.betaConstraints,
115: other.betaConstraints)
116: && Arrays.equals(this.requiredDeclarations,
117: other.requiredDeclarations);
118: }
119:
120: }
|