KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > junit > client > IntRange


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.junit.client;
17
18 import java.util.Iterator JavaDoc;
19
20 /**
21  * A {@link com.google.gwt.junit.client.Range} that iterates over a start and
22  * end value by a stepping function. Typically used by benchmarks to supply a
23  * range of values over an integral parameter, such as size or length.
24  *
25  */

26 public class IntRange implements Range {
27
28   /**
29    * Implementation of the Iterator.
30    *
31    */

32   private static class IntRangeIterator extends RangeIterator {
33
34     int end;
35
36     Operator operator;
37
38     int start;
39
40     int step;
41
42     int value;
43
44     IntRangeIterator(IntRange r) {
45       this.value = this.start = r.start;
46       this.end = r.end;
47       this.operator = r.operator;
48       if (operator == null) {
49         throw new IllegalArgumentException JavaDoc("operator must be \"*\" or \"+\"");
50       }
51       this.step = r.step;
52     }
53
54     public boolean hasNext() {
55       return value <= end;
56     }
57
58     public Object JavaDoc next() {
59       int currentValue = value;
60       value = step();
61       return new Integer JavaDoc(currentValue);
62     }
63
64     public int step() {
65       if (operator == Operator.MULTIPLY) {
66         return value * step;
67       } else {
68         return value + step;
69       }
70     }
71   }
72
73   int end;
74
75   Operator operator;
76
77   int start;
78
79   int step;
80
81   /**
82    * Creates a new range that produces Iterators which begin at
83    * <code>start</code>, end at <code>end</code> and increment by the
84    * stepping function described by <code>operator</code> and
85    * <code>step</code>.
86    *
87    * @param start Initial starting value, inclusive.
88    * @param end Ending value, inclusive.
89    * @param operator The function used to step.
90    * @param step The amount to step by, for each iteration.
91    */

92   public IntRange(int start, int end, Operator operator, int step) {
93     this.start = start;
94     this.end = end;
95     this.operator = operator;
96     this.step = step;
97     if (step <= 0) {
98       throw new IllegalArgumentException JavaDoc("step must be > 0");
99     }
100   }
101
102   public Iterator JavaDoc iterator() {
103     return new IntRangeIterator(this);
104   }
105 }
106
Popular Tags