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       */
27      protected void setLastInt(int last) {
28          lastint = last;
29      }
30  
31      /**
32       * Return the next value as an int. When overriding this method, be sure to call setLastString() properly, or the lastString() call won't work.
33       */
34      public abstract int nextInt();
35  
36      /**
37       * Generate the next string in the distribution.
38       */
39      public String nextString() {
40          return "" + nextInt();
41      }
42  
43      /**
44       * Return the previous string generated by the distribution; e.g., returned from the last nextString() call.
45       * Calling lastString() should not advance the distribution or have any side effects. If nextString() has not yet
46       * been called, lastString() should return something reasonable.
47       */
48      @Override
49      public String lastString() {
50          return "" + lastInt();
51      }
52  
53      /**
54       * Return the previous int generated by the distribution. This call is unique to IntegerGenerator subclasses, and assumes
55       * IntegerGenerator subclasses always return ints for nextInt() (e.g. not arbitrary strings).
56       */
57      public int lastInt() {
58          return lastint;
59      }
60  
61      /**
62       * Return the expected value (mean) of the values this generator will return.
63       */
64      public abstract double mean();
65  }