Synthia
Generic and flexible data structure generator
RandomString.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package ca.uqac.lif.synthia.string;
20 
21 import ca.uqac.lif.synthia.Picker;
22 import ca.uqac.lif.synthia.Seedable;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Generates a random character string.
34  *
35  * @ingroup API
36  */
37 public class RandomString implements Shrinkable<String>, Seedable
38 {
39  /**
40  * A picker used to determine the string's length
41  */
43 
45 
46  protected char[] m_chars;
47 
48  /**
49  * Creates a new RandomString picker with a default alphanumeric values array
50  *
51  * @param length A picker used to determine the string's length
52  */
54  {
55  super();
56  defaultCharArrayInitialize();
57  m_lengthPicker = length;
58  m_charIndexPicker = new RandomInteger(0, m_chars.length);
59  }
60 
61  /**
62  * Creates a new RandomString picker with a default alphanumeric values array
63  *
64  * @param length A picker used to determine the string's length
65  * @param char_array An array containing the characters allowed in the random string.
66  */
67  public RandomString(Picker<Integer> length, char[] char_array)
68  {
69  super();
70  m_chars = char_array;
71  m_lengthPicker = length;
72  m_charIndexPicker = new RandomInteger(0, m_chars.length);
73  }
74 
75  /**
76  * Private constructor used to duplicate the picker.
77  *
78  * @param length The picker used to pick the lenght of the random string.
79  * @param char_index_picker The picker used to generate random strings.
80  * @param char_array An array containing the characters allowed in the random string.
81  */
82  private RandomString(Picker<Integer> length, RandomInteger char_index_picker, char[] char_array)
83  {
84  super();
85  m_lengthPicker = length;
86  m_charIndexPicker = char_index_picker;
87  m_chars = char_array;
88  }
89 
90  /**
91  * Creates a new RandomString picker, with a default alphanumeric values array.
92  *
93  * @param length The max length of each generated string.
94  */
95  public RandomString(int length)
96  {
97  super();
98  defaultCharArrayInitialize();
99  m_lengthPicker = new Constant<>(length);
100  m_charIndexPicker = new RandomInteger(0, m_chars.length);
101  }
102 
103  /**
104  * Creates a new RandomString picker, with a specified alphanumeric values array.
105  *
106  * @param length The length of each generated string.
107  * @param char_array An array containing the characters allowed in the random string.
108  */
109  public RandomString(int length, char[] char_array)
110  {
111  super();
112  m_lengthPicker = new Constant<>(length);
113  m_chars = char_array;
114  m_charIndexPicker = new RandomInteger(0, m_chars.length);
115  }
116 
117  /**
118  * A private method to initialize the default characters array
119  */
120  private void defaultCharArrayInitialize()
121  {
122  m_chars = new char[62];
123  int i;
124 
125  // 0 to 9
126  for (i = 0; i < 10; i++)
127  {
128  m_chars[i] = (char) (i + 48);
129  }
130 
131  // A to Z and a to z
132  for (i = 0; i < 26; i++)
133  {
134  m_chars[i + 10] = (char) (i + 65);
135  m_chars[i + 36] = (char) (i + 97);
136  }
137  }
138 
139  /**
140  * Puts the RandomString picker back into its initial state. This means that the
141  * sequence of calls to {@link #pick()} will produce the same values
142  * as when the object was instantiated.
143  */
144  @Override
145  public void reset()
146  {
149  }
150 
151  /**
152  * Picks a random string. Typically, this method is expected to return non-null
153  * objects; a <tt>null</tt> return value is used to signal that no more
154  * objects will be produced. That is, once this method returns
155  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
156  * calls.
157  *
158  * @return The random string.
159  */
160  @Override
161  public String pick()
162  {
163  int len = m_lengthPicker.pick();
164  List<Integer> char_index_list = new ArrayList<>();
165  int char_index;
166  for (int i = 0; i < len; i++)
167  {
168  char_index = m_charIndexPicker.pick();
169  char_index_list.add(char_index);
170  }
171 
172  return toString(char_index_list);
173  }
174 
175  /**
176  * Private method used to convert a list of integers representing indexes in the m_chars
177  * array into a string.
178  *
179  * @param char_index_list The list containing the indexes in the m_chars array who will be used
180  * to create the string.
181  * @return A string.
182  */
183  private String toString(List<Integer> char_index_list)
184  {
185  StringBuilder str = new StringBuilder();
186 
187  for (Integer char_code_value : char_index_list)
188  {
189  str.append(m_chars[char_code_value]);
190  }
191 
192  return str.toString();
193  }
194 
195  /**
196  * Creates a copy of the RandomString picker.
197  *
198  * @param with_state If set to <tt>false</tt>, the returned copy is set to
199  * the class' initial state (i.e. same thing as calling the picker's
200  * constructor). If set to <tt>true</tt>, the returned copy is put into the
201  * same internal state as the object it is copied from.
202  * @return The copy of the RandomString picker
203  */
204  @Override
205  public Picker<String> duplicate(boolean with_state)
206  {
207  return new RandomString(m_lengthPicker.duplicate(with_state),
208  m_charIndexPicker.duplicate(with_state), m_chars);
209  }
210 
211  @Override
212  public RandomString setSeed(int seed)
213  {
215  return this;
216  }
217 
218  @Override
219  public Shrinkable<String> shrink(String o, Picker<Float> decision, float magnitude)
220  {
221  return new PickSmallerComparable<String>(this, o);
222  }
223 
224  //can only be used if m_lenghtPicker is a RandomInteger or RandomIndex
225  public void setInterval(int min, int max)
226  {
227 
228  if ( m_lengthPicker.getClass().getSimpleName().equals("RandomInteger"))
229  {
230  RandomInteger random_integer_copy = (RandomInteger) m_lengthPicker.duplicate(true);
231  random_integer_copy.setInterval(min, max);
232  m_lengthPicker = random_integer_copy;
233  }
234  else if (m_lengthPicker.getClass().getSimpleName().equals("RandomIndex"))
235  {
236  RandomInteger random_integer_copy = (RandomInteger) m_lengthPicker.duplicate(true);
237  random_integer_copy.setInterval(min, max);
238  m_lengthPicker = random_integer_copy;
239  }
240  }
241 
242  @Override
243  public Shrinkable<String> shrink(String o)
244  {
245  return shrink(o, RandomFloat.instance, 1);
246  }
247 
248 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.string.RandomString.m_chars
char[] m_chars
Definition: RandomString.java:46
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.string.RandomString.RandomString
RandomString(Picker< Integer > length)
Creates a new RandomString picker with a default alphanumeric values array.
Definition: RandomString.java:53
ca.uqac.lif.synthia.string.RandomString.pick
String pick()
Picks a random string.
Definition: RandomString.java:161
ca.uqac.lif.synthia.relative.PickSmallerComparable
A variant of PickIf that selects an element if it is smaller than a reference object.
Definition: PickSmallerComparable.java:37
ca.uqac.lif.synthia.string.RandomString.shrink
Shrinkable< String > shrink(String o)
Definition: RandomString.java:243
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac.lif.synthia.string.RandomString.shrink
Shrinkable< String > shrink(String o, Picker< Float > decision, float magnitude)
Definition: RandomString.java:219
ca.uqac
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia.string.RandomString.RandomString
RandomString(int length, char[] char_array)
Creates a new RandomString picker, with a specified alphanumeric values array.
Definition: RandomString.java:109
ca.uqac.lif.synthia.string.RandomString.setSeed
RandomString setSeed(int seed)
Set the seed of the random generator.
Definition: RandomString.java:212
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.util.Constant
Picker that returns the same object every time.
Definition: Constant.java:37
ca.uqac.lif.synthia.string.RandomString.reset
void reset()
Puts the RandomString picker back into its initial state.
Definition: RandomString.java:145
ca.uqac.lif.synthia.string.RandomString.setInterval
void setInterval(int min, int max)
Definition: RandomString.java:225
ca.uqac.lif.synthia.string.RandomString.duplicate
Picker< String > duplicate(boolean with_state)
Creates a copy of the RandomString picker.
Definition: RandomString.java:205
ca.uqac.lif.synthia.random.RandomInteger.setInterval
RandomInteger setInterval(int min, int max)
Sets the interval in which integers are picked.
Definition: RandomInteger.java:84
ca.uqac.lif
ca.uqac.lif.synthia.random.RandomPicker.reset
void reset()
Puts the picker back into its initial state.
Definition: RandomPicker.java:68
ca.uqac.lif.synthia.random.RandomInteger.setSeed
RandomInteger setSeed(int seed)
Definition: RandomInteger.java:102
ca.uqac.lif.synthia.random.RandomInteger
Picks an integer uniformly in an interval.
Definition: RandomInteger.java:31
ca.uqac.lif.synthia.random.RandomInteger.pick
Integer pick()
Picks a random integer.
Definition: RandomInteger.java:118
ca.uqac.lif.synthia.string.RandomString
Generates a random character string.
Definition: RandomString.java:37
ca.uqac.lif.synthia.string.RandomString.RandomString
RandomString(Picker< Integer > length, char[] char_array)
Creates a new RandomString picker with a default alphanumeric values array.
Definition: RandomString.java:67
ca
ca.uqac.lif.synthia.random.RandomInteger.duplicate
RandomInteger duplicate(boolean with_state)
Creates a copy of the RandomInteger picker.
Definition: RandomInteger.java:133
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.relative
Pickers that produce a value in relation to another value.
Definition: package-info.java:24
ca.uqac.lif.synthia.string.RandomString.m_charIndexPicker
RandomInteger m_charIndexPicker
Definition: RandomString.java:44
ca.uqac.lif.synthia.string.RandomString.RandomString
RandomString(int length)
Creates a new RandomString picker, with a default alphanumeric values array.
Definition: RandomString.java:95
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.string.RandomString.m_lengthPicker
Picker< Integer > m_lengthPicker
A picker used to determine the string's length.
Definition: RandomString.java:42
ca.uqac.lif.synthia.Seedable
Interface implemented by objects that can be seeded.
Definition: Seedable.java:29
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
ca.uqac.lif.synthia.Picker.reset
void reset()
Puts the picker back into its initial state.