001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * 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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.junit.client;
017:
018: /**
019: * A {@link com.google.gwt.junit.client.Range} that iterates over a start and
020: * end value by a stepping function. Typically used by benchmarks to supply a
021: * range of values over an integral parameter, such as size or length.
022: *
023: */
024: public class IntRange implements Range<Integer> {
025:
026: /**
027: * Implementation of the Iterator.
028: *
029: */
030: private static class IntRangeIterator extends
031: RangeIterator<Integer> {
032:
033: int end;
034:
035: Operator operator;
036:
037: int start;
038:
039: int step;
040:
041: int value;
042:
043: IntRangeIterator(IntRange r) {
044: this .value = this .start = r.start;
045: this .end = r.end;
046: this .operator = r.operator;
047: if (operator == null) {
048: throw new IllegalArgumentException(
049: "operator must be \"*\" or \"+\"");
050: }
051: this .step = r.step;
052: }
053:
054: public boolean hasNext() {
055: return value <= end;
056: }
057:
058: public Integer next() {
059: int currentValue = value;
060: value = step();
061: return currentValue;
062: }
063:
064: public int step() {
065: if (operator == Operator.MULTIPLY) {
066: return value * step;
067: } else {
068: return value + step;
069: }
070: }
071: }
072:
073: int end;
074:
075: Operator operator;
076:
077: int start;
078:
079: int step;
080:
081: /**
082: * Creates a new range that produces Iterators which begin at
083: * <code>start</code>, end at <code>end</code> and increment by the
084: * stepping function described by <code>operator</code> and
085: * <code>step</code>.
086: *
087: * @param start Initial starting value, inclusive.
088: * @param end Ending value, inclusive.
089: * @param operator The function used to step.
090: * @param step The amount to step by, for each iteration.
091: */
092: public IntRange(int start, int end, Operator operator, int step) {
093: this .start = start;
094: this .end = end;
095: this .operator = operator;
096: this .step = step;
097: if (step <= 0) {
098: throw new IllegalArgumentException("step must be > 0");
099: }
100: }
101:
102: public IntRangeIterator iterator() {
103: return new IntRangeIterator(this);
104: }
105: }
|