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.core.BasicClass; 18 import org.as2lib.env.overload.Overload; 19 20 /** 21 * {@code MethodCallRange} represents the number of expected method calls. 22 * 23 * @author Simon Wacker 24 */ 25 class org.as2lib.test.mock.MethodCallRange extends BasicClass { 26 27 /** The minimum that is used if none has been specified. */ 28 public static var DEFAULT_MINIMUM:Number = 0; 29 30 /** The maximum that is used if none has been specified. */ 31 public static var DEFAULT_MAXIMUM:Number = Number.POSITIVE_INFINITY; 32 33 /** The minimum. */ 34 private var minimum:Number; 35 36 /** The maximum. */ 37 private var maximum:Number; 38 39 /** 40 * @overload #MethodCallRangeByVoid 41 * @overload #MethodCallRangeByQuantity 42 * @overload #MethodCallRangeByMinimumAndMaximumQuantity 43 */ 44 public function MethodCallRange() { 45 var o:Overload = new Overload(this); 46 o.addHandler([], MethodCallRangeByVoid); 47 o.addHandler([Number], MethodCallRangeByQuantity); 48 o.addHandler([Number, Number], MethodCallRangeByMinimumAndMaximumQuantity); 49 o.forward(arguments); 50 } 51 52 /** 53 * Uses the default minimum and the default maximum value. 54 */ 55 private function MethodCallRangeByVoid(Void):Void { 56 MethodCallRangeByMinimumAndMaximumQuantity(null, null); 57 } 58 59 /** 60 * Uses the passed-in {@code quantity} as minimum and maximum. 61 * 62 * <p>If the passed-in {@code quantity} is {@code null} {@link #DEFAULT_MINIMUM} 63 * and {@link #DEFAULT_MAXIMUM} is used. 64 * 65 * @param quantity the quantity 66 */ 67 private function MethodCallRangeByQuantity(quantity:Number):Void { 68 if (quantity == null) { 69 MethodCallRangeByMinimumAndMaximumQuantity(null, null); 70 } 71 MethodCallRangeByMinimumAndMaximumQuantity(quantity, quantity); 72 } 73 74 /** 75 * Creates a new range with the passed-in {@code minimum} and {@code maximum}. 76 * 77 * <ul> 78 * <li>If {@code minimum} is {@code null} {@link #DEFAULT_MINIMUM} will be used.</li> 79 * <li>If {@code maximum} is {@code null} {@link #DEFAULT_MAXIMUM} will be used.</li> 80 * <li>If {@code minimum} is negative it will be made positive.</li> 81 * <li>If {@code maximum} is negative it will be made positive.</li> 82 * <li> 83 * If {@code minimum} is bigger than {@code maximum} the two values will be 84 * exchanged. 85 * </li> 86 * </ul> 87 * 88 * @param minimum the minimum 89 * @param maximum the maximum 90 */ 91 private function MethodCallRangeByMinimumAndMaximumQuantity(minimum:Number, maximum:Number):Void { 92 if (minimum == null) minimum = DEFAULT_MINIMUM; 93 if (maximum == null) maximum = DEFAULT_MAXIMUM; 94 if (minimum < 0) minimum = -minimum; 95 if (maximum < 0) maximum = -maximum; 96 if (minimum > maximum) { 97 var oldMinimum:Number = minimum; 98 minimum = maximum; 99 maximum = oldMinimum; 100 } 101 this.minimum = minimum; 102 this.maximum = maximum; 103 } 104 105 /** 106 * Returns the minimum of this range. 107 * 108 * @return the set minimum 109 */ 110 public function getMinimum(Void):Number { 111 return minimum; 112 } 113 114 /** 115 * Returns the maximum of this range. 116 * 117 * @return the set maximum 118 */ 119 public function getMaximum(Void):Number { 120 return maximum; 121 } 122 123 /** 124 * Checks whether the passed-in {@code quantity} is between the minimum and 125 * maximum. 126 * 127 * <p>The quantity will be made positive if it is negative. 128 * 129 * <p>{@code false} will be returned if: 130 * <ul> 131 * <li>The passed-in {@code quantity} is {@code null}.</li> 132 * <li>The passed-in {@code quantity} is smaller than the minimum.</li> 133 * <li>The passed-in {@code quantity} is bigger than the maximum.</li> 134 * </ul> 135 * 136 * @param quantity the quantity 137 * @return {@code true} if the quantity is contained by this range else 138 * {@code false} 139 */ 140 public function contains(quantity:Number):Boolean { 141 if (quantity == null) return false; 142 if (quantity < 0) quantity = -quantity; 143 if (minimum > quantity || maximum < quantity) { 144 return false; 145 } 146 return true; 147 } 148 149 /** 150 * Returns the string representation of this range. 151 * 152 * @return the string representation of this range 153 */ 154 public function toString():String { 155 if (minimum == maximum) return minimum.toString(); 156 var interval:String = "["; 157 if (minimum == Number.POSITIVE_INFINITY) interval += "∞"; 158 else interval += minimum.toString(); 159 interval += ","; 160 if (maximum == Number.POSITIVE_INFINITY) interval += "∞"; 161 else interval += maximum.toString(); 162 interval += "]"; 163 return interval; 164 } 165 166 }