Synthia
Generic and flexible data structure generator
RandomSubString.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;
10 
11 /**
12  * {@link RandomPicker} that produces random substrings from an original one.
13  * @author Marc-Antoine Plourde
14  * @ingroup API
15  */
16 public class RandomSubString implements Shrinkable<String>, Seedable
17 {
18 
19  /**
20  * The string used to generate substrings.
21  */
22  protected String m_string;
23 
24  /**
25  * A {@link RandomBoolean} picker to select if we pick a character or not.
26  */
28 
29  /**
30  * Private constructor used by the {@link #duplicate(boolean)} method.
31  *
32  * @param s The {@link #m_string} attribute.
33  * @param char_select The {@link #m_charSelect} attribute.
34  */
35  private RandomSubString(String s, RandomBoolean char_select)
36  {
37  m_string = s;
38  m_charSelect = char_select;
39  }
40 
41  /**
42  * Public constructor used to create a new instance of the class.
43  *
44  * @param s The string used to generate substrings.
45  */
46  public RandomSubString(String s)
47  {
48  m_string = s;
50  }
51 
52  /**
53  * Create a new {@link RandomSubString} picker based on a given string. If this given string is
54  * empty, the method will return a {@link NothingPicker}. The new instance will also have the same
55  * internal states for the {@link #m_charSelect} attributes as the original one.
56  *
57  * @param element The string used by the new {@link RandomSubString} picker to produce substrings.
58  *
59  * @return The new instance of the class or a {@link NothingPicker}.
60  */
61  @Override
62  public Shrinkable<String> shrink(String element, Picker<Float> decision, float magnitude)
63  {
64  if (element.isEmpty())
65  {
66  return new NothingPicker<String>();
67  }
68  else
69  {
70  return new RandomSubString(element, m_charSelect.duplicate(true));
71  }
72  }
73 
74  @Override
75  public void reset()
76  {
78  }
79 
80  @Override
81  public String pick()
82  {
83  boolean initial_bool = m_charSelect.pick();
84  boolean picked_bool = initial_bool;
85  StringBuilder substring = new StringBuilder();
86  int i =0;
87  while ((initial_bool == picked_bool) && (i < m_string.length()))
88  {
89  if(initial_bool)
90  {
91  substring.append(m_string.charAt(i));
92  }
93  picked_bool = m_charSelect.pick();
94  i++;
95  }
96 
97  if(!initial_bool)
98  {
99  while ((picked_bool) && (i < m_string.length()))
100  {
101  substring.append(m_string.charAt(i));
102  picked_bool = m_charSelect.pick();
103  i++;
104  }
105  }
106 
107  return substring.toString();
108  }
109 
110  @Override
111  public Picker<String> duplicate(boolean with_state)
112  {
113  return new RandomSubString(m_string, m_charSelect.duplicate(with_state));
114  }
115 
116  @Override
117  public RandomSubString setSeed(int seed)
118  {
119  m_charSelect.setSeed(seed);
120  return this;
121  }
122 
123  @Override
124  public Shrinkable<String> shrink(String o)
125  {
126  return shrink(o, RandomFloat.instance, 1);
127  }
128 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.string.RandomSubString
RandomPicker that produces random substrings from an original one.
Definition: RandomSubString.java:16
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.string.RandomSubString.m_string
String m_string
The string used to generate substrings.
Definition: RandomSubString.java:22
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.RandomSubString.duplicate
Picker< String > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: RandomSubString.java:111
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.synthia.string.RandomSubString.pick
String pick()
Picks an object.
Definition: RandomSubString.java:81
ca.uqac.lif.synthia.random.RandomBoolean.pick
Boolean pick()
Picks a random boolean.
Definition: RandomBoolean.java:93
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.random.RandomPicker.reset
void reset()
Puts the picker back into its initial state.
Definition: RandomPicker.java:68
ca
ca.uqac.lif.synthia.string.RandomSubString.shrink
Shrinkable< String > shrink(String element, Picker< Float > decision, float magnitude)
Create a new RandomSubString picker based on a given string.
Definition: RandomSubString.java:62
ca.uqac.lif.synthia.random.RandomBoolean.duplicate
RandomBoolean duplicate(boolean with_state)
Creates a copy of the RandomBoolean picker.
Definition: RandomBoolean.java:108
ca.uqac.lif.synthia.random.RandomPicker
Picks an object based on the value of a random number generator.
Definition: RandomPicker.java:39
ca.uqac.lif.synthia.string.RandomSubString.setSeed
RandomSubString setSeed(int seed)
Set the seed of the random generator.
Definition: RandomSubString.java:117
ca.uqac.lif.synthia.random.RandomPicker.setSeed
RandomPicker< T > setSeed(int seed)
Set the seed of the random generator.
Definition: RandomPicker.java:60
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.string.RandomSubString.shrink
Shrinkable< String > shrink(String o)
Definition: RandomSubString.java:124
ca.uqac.lif.synthia.Seedable
Interface implemented by objects that can be seeded.
Definition: Seedable.java:29
ca.uqac.lif.synthia.string.RandomSubString.reset
void reset()
Puts the picker back into its initial state.
Definition: RandomSubString.java:75
ca.uqac.lif.synthia.string.RandomSubString.m_charSelect
RandomBoolean m_charSelect
A RandomBoolean picker to select if we pick a character or not.
Definition: RandomSubString.java:27
ca.uqac.lif.synthia.string.RandomSubString.RandomSubString
RandomSubString(String s)
Public constructor used to create a new instance of the class.
Definition: RandomSubString.java:46
ca.uqac.lif.synthia.random.RandomBoolean
Picks a Boolean value.
Definition: RandomBoolean.java:34