1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.aop.pointcut.CompositePointcut;
    18  import org.as2lib.aop.pointcut.AbstractCompositePointcut;
    19  import org.as2lib.aop.AopConfig;
    20  import org.as2lib.aop.Pointcut;
    21  import org.as2lib.aop.JoinPoint;
    22  
    23  /**
    24   * {@code OrPointcut} combines multiple pointcuts with a logical OR. This means that
    25   * this pointcut captures a given join point if at least one of its contained pointcuts
    26   * captures the given join point.
    27   *
    28   * <p>This pointcut expects a string representation as parameter on construction. Such
    29   * a string representation may look something like this:
    30   * <code>execution(org.as2lib.env.Logger.*) || execution(org.as2lib.reflect.*.*)</code>
    31   * 
    32   * @author Simon Wacker
    33   */
    34  class org.as2lib.aop.pointcut.OrPointcut extends AbstractCompositePointcut implements CompositePointcut {
    35  	
    36  	/**
    37  	 * Constructs a new {@code OrPointcut} instance.
    38  	 * 
    39  	 * <p>The string representation is supposed to be a combination of multiple
    40  	 * pointcuts where some of them are combined with the {@code ||} operator.
    41  	 * 
    42  	 * @param pointcut the string representation of this or pointcut
    43  	 */
    44  	public function OrPointcut(pointcut:String) {
    45  		if (pointcut != null && pointcut != "") {
    46  			// source this out
    47  			var pointcuts:Array = pointcut.split("||");
    48  			for (var i:Number = 0; i < pointcuts.length; i++) {
    49  				addPointcut(AopConfig.getPointcutFactory().getPointcut(pointcuts[i]));
    50  			}
    51  		}
    52  	}
    53  	
    54  	/**
    55  	 * Checks whether this pointcut captures the given {@code joinPoint}. The
    56  	 * {@code joinPoint} is captured if at least one sub-pointcut of this pointcut
    57  	 * captures it.
    58  	 * 
    59  	 * {@code false} will be returned if:
    60  	 * <ul>
    61  	 *   <li>The passed-in {@code joinPoint} is {@code null} or {@code undefined}.</li>
    62  	 *   <li>Not even one sub-pointcut captures the given {@code joinPoint}.</li>
    63  	 *   <li>There are no pointcuts added.</li>
    64  	 * </ul>
    65  	 *
    66  	 * @param joinPoint the join point to check whether it is captured by this pointcut
    67  	 * @return {@code true} if this pointcut captures the given {@code joinPoint} else
    68  	 * {@code false}
    69  	 */
    70  	public function captures(joinPoint:JoinPoint):Boolean {
    71  		if (!joinPoint) return false;
    72  		var i:Number = this.pointcuts.length;
    73  		if (i < 1) return false;
    74  		while (--i-(-1)) {
    75  			var pointcut:Pointcut = this.pointcuts[i];
    76  			if (pointcut.captures(joinPoint)) {
    77  				return true;
    78  			}
    79  		}
    80  		return false;
    81  	}
    82  	
    83  }