001: package org.mockejb.interceptor;
002:
003: import java.lang.reflect.Method;
004:
005: /**
006: * Provides a way to create conditional expressions from pointcuts.
007: *
008: * @author Alexander Ananiev
009: */
010: public class PointcutPair implements Pointcut {
011:
012: private Pointcut pointcut1;
013: private Pointcut pointcut2;
014: private boolean isOr = false;
015:
016: /**
017: * Creates a new instance of PointcutPair.
018: * AND condition will be used by "matchesJointpoint".
019: *
020: * @param pointcut1 first pointcut
021: * @param pointcut2 second pointcut
022: *
023: */
024: public static PointcutPair and(Pointcut pointcut1,
025: Pointcut pointcut2) {
026: return new PointcutPair(pointcut1, pointcut2);
027: }
028:
029: /**
030: * Creates a new instance of PointcutPair.
031: * OR condition will be used by "matchesJointpoint".
032: *
033: * @param pointcut1 first pointcut
034: * @param pointcut2 second pointcut
035: *
036: */
037: public static PointcutPair or(Pointcut pointcut1, Pointcut pointcut2) {
038: return new PointcutPair(pointcut1, pointcut2, true);
039: }
040:
041: /**
042: * Creates a new instance of PointcutPair
043: * @param pointcut1 first pointcut
044: * @param pointcut2 second pointcut
045: *
046: */
047: PointcutPair(final Pointcut pointcut1, final Pointcut pointcut2) {
048: this .pointcut1 = pointcut1;
049: this .pointcut2 = pointcut2;
050: }
051:
052: /**
053: * Creates a new instance of PointcutPair
054: * @param pointcut1 first pointcut
055: * @param pointcut2 second pointcut
056: * @param isOr if true, use OR instead of AND
057: */
058: PointcutPair(final Pointcut pointcut1, final Pointcut pointcut2,
059: boolean isOr) {
060: this .pointcut1 = pointcut1;
061: this .pointcut2 = pointcut2;
062: this .isOr = isOr;
063: }
064:
065: /**
066: * Tests if both poincuts match
067: *
068: * @return true if the provided method should be intercepted
069: */
070: public boolean matchesJointpoint(Method method) {
071: if (!isOr)
072: return pointcut1.matchesJointpoint(method)
073: && pointcut2.matchesJointpoint(method);
074: else
075: return pointcut1.matchesJointpoint(method)
076: || pointcut2.matchesJointpoint(method);
077:
078: }
079:
080: /**
081: * Returns true if the given object is of the same type and
082: * it has the same pattern.
083: */
084: public boolean equals(Object obj) {
085: if (!(obj instanceof PointcutPair))
086: return false;
087:
088: PointcutPair pointcutPair = (PointcutPair) obj;
089:
090: return (pointcut1.equals(pointcutPair.pointcut1)
091: && pointcut2.equals(pointcutPair.pointcut2) && isOr == pointcutPair.isOr);
092:
093: }
094:
095: public int hashCode() {
096: int result = 17;
097: result = 37 * result + pointcut1.hashCode();
098: result = 37 * result + pointcut2.hashCode();
099: result = 37 * result + (isOr ? 0 : 1);
100:
101: return result;
102: }
103:
104: }
|