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;
48
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.Random;
52
53 /***
54 * Dictionary.
55 *
56 * @author Piotr Maj <piotr.maj@kernelpanic.pl>
57 *
58 * @version $Id: Dictionary.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
59 */
60 public class Dictionary {
61
62 /***
63 * The name of the dictionary.
64 */
65 private String name = null;
66
67 /***
68 * A randomizer.
69 */
70 private Random random = null;
71
72 /***
73 * List of items.
74 */
75 private List items = new ArrayList();
76
77 /***
78 * The next unique object index.
79 */
80 private int nextUniqueIndex = -1;
81
82 /***
83 * Returns next random object.
84 *
85 * @return next random object
86 */
87 public final Object getNextRandomItem() {
88 return items.get(random.nextInt(items.size()));
89 }
90
91 /***
92 * Returns next unique object.
93 *
94 * @return next unique object
95 *
96 * @throws Exception if there is no more unique values
97 */
98 public final Object getNextUniqueItem() throws Exception {
99 setNextUnique();
100 return items.get(nextUniqueIndex);
101 }
102
103 /***
104 * Resets this dictionary.
105 */
106 public final void reset() {
107 nextUniqueIndex = -1;
108 }
109
110 /***
111 * Returns the name of this dictionary.
112 *
113 * @return name
114 */
115 public final String getName() {
116 return name;
117 }
118
119 /***
120 * Sets the name of the dictionary.
121 *
122 * @param s name
123 */
124 public final void setName(final String s) {
125 name = s;
126 }
127
128 /***
129 * Sets the random generator.
130 *
131 * @param r random number generator
132 */
133 public final void setRandom(final Random r) {
134 random = r;
135 }
136
137 /***
138 * Adds new item to this dictionary.
139 *
140 * @param item new item
141 */
142 public final void addItem(final String item) {
143 items.add(item);
144 }
145
146 /***
147 * Sets the next unique index.
148 *
149 * @throws Exception if there are no more unique items left.
150 */
151 private final void setNextUnique() throws Exception {
152 if (items.size() > 0 && nextUniqueIndex < items.size() - 1) {
153 ++nextUniqueIndex;
154 } else {
155 throw new Exception("No more unique values in dictionary: "
156 + name);
157 }
158 }
159 }