View Javadoc

1   /**
2    * Copyright (c) 2010 Yahoo! Inc. All rights reserved.                                                                                                                             
3    *
4    *   http://www.apache.org/licenses/LICENSE-2.0
5    *
6    * Unless required by applicable law or agreed to in writing, software
7    * distributed under the License is distributed on an "AS IS" BASIS,
8    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9    * See the License for the specific language governing permissions and
10   * limitations under the License.
11   */
12  package org.apache.omid.benchmarks.utils;
13  
14  /**
15   * A generator that is capable of generating ints as well as strings
16   *
17   * @author cooperb
18   *
19   */
20  public abstract class IntegerGenerator extends Generator {
21      int lastint;
22  
23      /**
24       * Set the last value generated. IntegerGenerator subclasses must use this call
25       * to properly set the last string value, or the lastString() and lastInt() calls won't work.
26       * @param last value
27       */
28      protected void setLastInt(int last) {
29          lastint = last;
30      }
31  
32      /**
33       *  When overriding this method, be sure to call setLastString() properly, or the lastString() call won't work
34       * @return Return the next value as an int.
35       */
36      public abstract int nextInt();
37  
38      /**
39       * @return Generate the next string in the distribution.
40       */
41      public String nextString() {
42          return "" + nextInt();
43      }
44  
45      /**
46       * @return the previous string generated by the distribution; e.g., returned from the last nextString() call.
47       * Calling lastString() should not advance the distribution or have any side effects. If nextString() has not yet
48       * been called, lastString() should return something reasonable.
49       */
50      @Override
51      public String lastString() {
52          return "" + lastInt();
53      }
54  
55      /**
56       * @return the previous int generated by the distribution. This call is unique to IntegerGenerator subclasses, and assumes
57       * IntegerGenerator subclasses always return ints for nextInt() (e.g. not arbitrary strings).
58       */
59      public int lastInt() {
60          return lastint;
61      }
62  
63      /**
64       * @return the expected value (mean) of the values this generator will return.
65       */
66      public abstract double mean();
67  }