View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package pl.kernelpanic.dbmonster.generator;
48  
49  import java.util.Random;
50  
51  import pl.kernelpanic.dbmonster.DBMonster;
52  import pl.kernelpanic.dbmonster.DBMonsterContext;
53  import pl.kernelpanic.dbmonster.util.Converter;
54  
55  /***
56   * The generator which produces boolean values.
57   *
58   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
59   *
60   * @version $Id: BooleanGenerator.html,v 1.1 2007/06/21 08:38:13 sbahloul Exp $
61   */
62  public class BooleanGenerator extends BasicDataGenerator implements Initializable {
63  
64      /***
65       * Probability.
66       */
67      private int probability = 50;
68  
69      /***
70       * Context.
71       */
72      private DBMonsterContext context = null;
73  
74      /***
75       * Random number generator.
76       */
77      private Random random = null;
78  
79      /***
80       * @see Initializable#initialize(DBMonsterContext)
81       */
82      public final void initialize(final DBMonsterContext ctx) throws Exception {
83          context = ctx;
84          random = (Random) context.getProperty(DBMonster.RANDOM_KEY);
85      }
86  
87      /***
88       * Generates boolean value.
89       *
90       * @return <code>true</code>, <code>false</code> or <code>null</code>.
91       */
92      public final Object generate() {
93  
94          if (nulls != 0 && random.nextInt(101) <= nulls) {
95              return null;
96          }
97  
98          if (probability != 0 && random.nextInt(101) <= probability) {
99              return Boolean.valueOf(true);
100         }
101         return Boolean.valueOf(false);
102     }
103 
104     /***
105      * Returns the probability.
106      *
107      * @return number of <code>true</code> per each 100 generatied values.
108      */
109     public final int getProbability() {
110         return probability;
111     }
112 
113     /***
114      * Sets the probability.
115      *
116      * @param prob probability
117      */
118     public final void setProbability(final int prob) {
119         probability = Converter.checkNulls(prob);
120     }
121 
122     /***
123      * Reset generator.
124      */
125     public final void reset() {
126     }
127 }