Synthia
Generic and flexible data structure generator
RandomPrefix.java
Go to the documentation of this file.
1 package ca.uqac.lif.synthia.string;
2 
3 import ca.uqac.lif.synthia.Picker;
4 import ca.uqac.lif.synthia.Seedable;
9 
10 /**
11  * Like {@link RandomSubString}, but this time, the {@link Picker} returns a prefix of the original
12  * strings of randomly selected length. The {@link RandomSubString#m_charSelect} is replaced by a
13  * {@link RandomInteger}.
14  *
15  * @ingroup API
16  */
17 public class RandomPrefix implements Shrinkable<String>, Seedable
18 {
19 
20  /**
21  * {@link RandomInteger} picker to select the size of a generated prefix.
22  */
24 
25  /**
26  * The string used to generate prefixes.
27  */
28  protected String m_string;
29 
30  /**
31  * Private constructor used by the {@link #duplicate(boolean)} method.
32  *
33  * @param string The {@link #m_string} attributes.
34  * @param prefix_size The {@link #m_prefixSize} attributes.
35  */
36  protected RandomPrefix(String string, RandomInteger prefix_size)
37  {
38  m_string = string;
39  m_prefixSize = prefix_size;
40  }
41 
42  /**
43  * Public constructor to create a new instance of this {@link Picker}.
44  *
45  * @param string The string used to generate prefixes.
46  */
47  public RandomPrefix(String string)
48  {
49  m_string = string;
50  m_prefixSize = new RandomInteger(0, m_string.length()+1);
51  }
52 
53  /**
54  * Create a new {@link RandomPrefix} picker based on a given string. If this given string is
55  * empty, the method will return a {@link NothingPicker}. The new instance will also have the same
56  * internal states for the {@link #m_prefixSize} attributes as the original one.
57  *
58  * @param element The string used by the new {@link RandomPrefix} picker to produce substrings.
59  *
60  * @return The new instance of the class or a {@link NothingPicker}.
61  */
62  @Override
63  public Shrinkable<String> shrink(String element, Picker<Float> decision, float magnitude)
64  {
65  if(element.isEmpty())
66  {
67  return new NothingPicker<>();
68  }
69  return new RandomPrefix(element, m_prefixSize.duplicate(true));
70  }
71 
72  @Override
73  public void reset()
74  {
76  }
77 
78  @Override
79  public String pick()
80  {
81  return m_string.substring(0, m_prefixSize.pick());
82  }
83 
84  @Override
85  public RandomPrefix duplicate(boolean with_state)
86  {
87  return new RandomPrefix(m_string, m_prefixSize.duplicate(with_state));
88  }
89 
90  @Override
91  public RandomPrefix setSeed(int seed)
92  {
93  m_prefixSize.setSeed(seed);
94  return this;
95  }
96 
97  @Override
98  public Shrinkable<String> shrink(String o)
99  {
100  return shrink(o, RandomFloat.instance, 1);
101  }
102 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.string.RandomPrefix.RandomPrefix
RandomPrefix(String string, RandomInteger prefix_size)
Private constructor used by the duplicate(boolean) method.
Definition: RandomPrefix.java:36
ca.uqac.lif.synthia.string.RandomPrefix.duplicate
RandomPrefix duplicate(boolean with_state)
Creates a copy of the picker.
Definition: RandomPrefix.java:85
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.string.RandomPrefix.m_string
String m_string
The string used to generate prefixes.
Definition: RandomPrefix.java:28
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.string.RandomPrefix.shrink
Shrinkable< String > shrink(String o)
Definition: RandomPrefix.java:98
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.RandomPrefix.RandomPrefix
RandomPrefix(String string)
Public constructor to create a new instance of this Picker.
Definition: RandomPrefix.java:47
ca.uqac.lif.synthia.string.RandomPrefix
Like RandomSubString, but this time, the Picker returns a prefix of the original strings of randomly ...
Definition: RandomPrefix.java:17
ca.uqac
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca.uqac.lif.synthia.util.NothingPicker
A RelativePicker that only throws a NoMoreElementException when the pick() method is called.
Definition: NothingPicker.java:36
ca.uqac.lif.synthia.string.RandomPrefix.shrink
Shrinkable< String > shrink(String element, Picker< Float > decision, float magnitude)
Create a new RandomPrefix picker based on a given string.
Definition: RandomPrefix.java:63
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
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.string.RandomPrefix.reset
void reset()
Puts the picker back into its initial state.
Definition: RandomPrefix.java:73
ca.uqac.lif.synthia.string.RandomPrefix.setSeed
RandomPrefix setSeed(int seed)
Set the seed of the random generator.
Definition: RandomPrefix.java:91
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.Seedable
Interface implemented by objects that can be seeded.
Definition: Seedable.java:29
ca.uqac.lif.synthia.string.RandomPrefix.pick
String pick()
Picks an object.
Definition: RandomPrefix.java:79
ca.uqac.lif.synthia.string.RandomPrefix.m_prefixSize
RandomInteger m_prefixSize
RandomInteger picker to select the size of a generated prefix.
Definition: RandomPrefix.java:23