Synthia
Generic and flexible data structure generator
AllIntegers.java
Go to the documentation of this file.
1 package ca.uqac.lif.synthia.enumerative;
2 
3 import ca.uqac.lif.synthia.Bounded;
5 import ca.uqac.lif.synthia.Seedable;
7 
8 import java.util.ArrayList;
9 import java.util.List;
10 
11 /**
12  * Picker who implements EnumerativePicker. This picker enumerates the integer values
13  * from x to y and throws a {@link NoMoreElementException} if the picker picks
14  * another value after the picker has finished to enumerates the values.
15  * @ingroup API
16  */
17 public class AllIntegers implements Bounded<Integer>, Seedable
18 {
19  /**
20  * The minimal value the picker can generate.
21  */
22  protected int m_min;
23 
24  /**
25  * The maximal value the picker can generate.
26  */
27  protected int m_max;
28 
29  /**
30  * The actual value to generate.
31  */
32  protected int m_actualValue;
33 
34  /**
35  * Flag to check if the picker scrambles the values to generate or not.
36  */
37  protected final boolean m_scramble;
38 
39  /**
40  * List to store the values to generate if the scramble flag is set to <tt>true</tt>.
41  */
42  protected List<Integer> m_valuesToScramble;
43 
44  /**
45  * RandomInteger to generate random index if the scramble flag is set to <tt>true</tt>.
46  */
48 
49  /**
50  * Private constructor used to duplicate the picker.
51  * @param min The m_min value to copy in a new AllIntegers instance.
52  * @param max The m_max value to copy in a new AllIntegers instance.
53  * @param actual_value The m_actualValue value to copy in a new AllIntegers instance.
54  * @param scramble The m_scramble value to copy in a new AllIntegers instance.
55  */
56  private AllIntegers(int min, int max, int actual_value, boolean scramble
57  , List<Integer> values_to_scramble, RandomInteger index_generator)
58  {
59  m_min = min;
60  m_max = max;
61  m_actualValue = actual_value;
62  m_scramble = scramble;
63  m_valuesToScramble = values_to_scramble;
64  m_indexGenerator = index_generator;
65  }
66 
67  /**
68  * Private method to initialize m_valueToScramble if the scramble flag if set to <tt>true</tt>.
69  */
70  private void InitializeList()
71  {
72  for (int i = m_min; i <= m_max; i++)
73  {
74  m_valuesToScramble.add(i);
75  }
76  }
77 
78  /**
79  * Constructor with <tt>false</tt> as a default value for the scramble flag.
80  * @param min The minimal value to generate.
81  * @param max The maximal value to generate.
82  */
83  public AllIntegers(int min, int max)
84  {
85  this(min, max, false);
86  }
87 
88  /**
89  * Constructor without default value for the scramble flag.
90  * @param min The minimal value to generate.
91  * @param max The maximal value to generate.
92  * @param scramble False to generate values in order or True to generate scrambled values.
93  */
94  public AllIntegers(int min, int max, boolean scramble)
95  {
96  m_min = min;
97  m_max = max;
98  m_actualValue = min;
99  m_scramble = scramble;
100  m_valuesToScramble = new ArrayList<>();
101  m_indexGenerator = new RandomInteger(0, 1);
102 
103  if(scramble)
104  {
105  InitializeList();
107  }
108  }
109 
110  @Override
111  public boolean isDone()
112  {
113  if (!m_scramble)
114  {
115  return m_actualValue > m_max;
116  }
117  return m_valuesToScramble.size() == 0;
118  }
119 
120  @Override
121  public void reset()
122  {
123  if(!m_scramble)
124  {
126  }
127  else
128  {
129  m_valuesToScramble.clear();
130  InitializeList();
133  }
134 
135  }
136 
137  @Override
138  public Integer pick()
139  {
140  if (isDone())
141  {
142  throw new NoMoreElementException();
143  }
144 
145  int picked_value;
146 
147  if (!m_scramble)
148  {
149  picked_value = m_actualValue;
150  m_actualValue++;
151  }
152  else
153  {
154  picked_value = scrambledPick();
155  }
156 
157  return picked_value;
158  }
159 
160  /**
161  * Private method used by the public pick method to pick scrambled values if the scramble flag
162  * is set to <tt>true</tt>.
163  * @return A scrambled Integer object.
164  */
165  private Integer scrambledPick()
166  {
167  Integer picked_index = m_indexGenerator.pick();
168  Integer picked_value = m_valuesToScramble.get(picked_index);
169 
170  m_valuesToScramble.remove(picked_value);
172 
173  return picked_value;
174  }
175 
176  @Override
177  public AllIntegers duplicate(boolean with_state)
178  {
180  new ArrayList<>(m_valuesToScramble), m_indexGenerator.duplicate(with_state));
181 
182  if (!with_state)
183  {
184  copy.reset();
185  }
186 
187  return copy;
188  }
189 
190  @Override
191  public AllIntegers setSeed(int seed)
192  {
193  if (m_scramble)
194  {
196  }
197  return this;
198  }
199 }
ca.uqac.lif.synthia.enumerative.AllIntegers.duplicate
AllIntegers duplicate(boolean with_state)
Creates a copy of the picker.
Definition: AllIntegers.java:177
ca.uqac.lif.synthia.enumerative.AllIntegers.m_indexGenerator
RandomInteger m_indexGenerator
RandomInteger to generate random index if the scramble flag is set to true.
Definition: AllIntegers.java:47
ca.uqac.lif.synthia.enumerative.AllIntegers.m_max
int m_max
The maximal value the picker can generate.
Definition: AllIntegers.java:27
ca.uqac.lif.synthia.enumerative.AllIntegers
Picker who implements EnumerativePicker.
Definition: AllIntegers.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.synthia.enumerative.AllIntegers.reset
void reset()
Puts the picker back into its initial state.
Definition: AllIntegers.java:121
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.Bounded
Interface used to signal that a picker enumerates all values from a set.
Definition: Bounded.java:26
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.enumerative.AllIntegers.m_valuesToScramble
List< Integer > m_valuesToScramble
List to store the values to generate if the scramble flag is set to true.
Definition: AllIntegers.java:42
ca.uqac.lif.synthia.enumerative.AllIntegers.setSeed
AllIntegers setSeed(int seed)
Set the seed of the random generator.
Definition: AllIntegers.java:191
ca.uqac.lif.synthia.enumerative.AllIntegers.m_min
int m_min
The minimal value the picker can generate.
Definition: AllIntegers.java:22
ca.uqac.lif.synthia.enumerative.AllIntegers.pick
Integer pick()
Picks an object.
Definition: AllIntegers.java:138
ca.uqac.lif.synthia.enumerative.AllIntegers.AllIntegers
AllIntegers(int min, int max)
Constructor with false as a default value for the scramble flag.
Definition: AllIntegers.java:83
ca.uqac.lif.synthia.enumerative.AllIntegers.m_scramble
final boolean m_scramble
Flag to check if the picker scrambles the values to generate or not.
Definition: AllIntegers.java:37
ca.uqac.lif.synthia.enumerative.AllIntegers.AllIntegers
AllIntegers(int min, int max, boolean scramble)
Constructor without default value for the scramble flag.
Definition: AllIntegers.java:94
ca.uqac.lif.synthia.enumerative.AllIntegers.m_actualValue
int m_actualValue
The actual value to generate.
Definition: AllIntegers.java:32
ca.uqac.lif.synthia.NoMoreElementException
An exception to throw when a picker can't pick an other element.
Definition: NoMoreElementException.java:25
ca.uqac.lif.synthia.enumerative.AllIntegers.isDone
boolean isDone()
Signals if the picker enumerates all values from a set.
Definition: AllIntegers.java:111
ca.uqac.lif.synthia.Seedable
Interface implemented by objects that can be seeded.
Definition: Seedable.java:29