Synthia
Generic and flexible data structure generator
AllPickers.java
Go to the documentation of this file.
1 package ca.uqac.lif.synthia.enumerative;
2 
3 import ca.uqac.lif.synthia.Bounded;
5 
6 /**
7  * Picker who implements {@link Bounded}. This picker enumerates all the possibility of
8  * of combinaisons of picked value from an array of Enumerative pickers. For example, an
9  * AllPickers containing an array of 2 {@link AllBooleans} will generates one array
10  * in the following order :
11  * <ol>
12  * <li>[<Boolean>false</Boolean>, <Boolean>false</Boolean>]</li>
13  * <li>[<Boolean>true</Boolean>, <Boolean>false</Boolean>]</li>
14  * <li>[<Boolean>false</Boolean>, <Boolean>true</Boolean>]</li>
15  * <li>[<Boolean>true</Boolean>, <Boolean>true</Boolean>]</li>
16  * </ol>
17  * After that, the picker will throw a {@link NoMoreElementException} if the pick method is called
18  * one more time.
19  * @ingroup API
20  */
21 public class AllPickers implements Bounded<Object[]>
22 {
23  /**
24  * The array of pickers used to generate all the possible combinations.
25  */
27 
28  /**
29  * Flag to check if it's the first pick.
30  */
31  protected boolean m_firstPick;
32 
33  /**
34  * Flag to check if the picker finished generating objects.
35  */
36  protected boolean m_done;
37 
38  /**
39  * An array to store the combination to return.
40  */
41  protected Object[] m_values;
42 
43  /**
44  * Private constructor used to duplicate the picker.
45  * @param enum_pickers The m_enumPickers attribute of the AllPickers instance to duplicate.
46  * @param first_pick The m_firstPick attribute of the AllPickers instance to duplicate.
47  * @param values The m_values attribute of the AllPickers instance to duplicate.
48  * @param done The m_done attribute of the AllPickers instance to duplicate.
49  */
50  private AllPickers(Bounded<?>[] enum_pickers, boolean first_pick, Object[] values
51  , boolean done)
52  {
53  m_enumPickers = enum_pickers;
54  m_firstPick = first_pick;
55  m_values = values;
56  m_done = done;
57  }
58 
59  public AllPickers(Bounded<?>[] enum_pickers)
60  {
61  m_enumPickers = enum_pickers;
62  m_firstPick = true;
63  m_values = new Object[m_enumPickers.length];
64  m_done = false;
65  }
66 
67  @Override
68  public boolean isDone()
69  {
70  return m_done;
71  }
72 
73  @Override
74  public void reset()
75  {
76  m_firstPick = true;
77  m_values = new Object[m_enumPickers.length];
78  m_done = false;
79 
80  for (Bounded<?> m_enumPicker : m_enumPickers)
81  {
82  m_enumPicker.reset();
83  }
84  }
85 
86  @Override
87  public Object[] pick()
88  {
89 
90  if (m_firstPick)
91  {
92  firstPick();
93  return m_values;
94  }
95 
96  if (isDone())
97  {
98  throw new NoMoreElementException();
99  }
100 
101  internalPick();
102  return m_values;
103  }
104 
105  /**
106  * Private method to generate a combination of values from the array of pickers.
107  * This private method is to simplify the pick public method.
108  */
109  private void internalPick()
110  {
111  if (!m_enumPickers[0].isDone())
112  {
113  m_values[0] = m_enumPickers[0].pick();
114  m_done = internalIsDone();
115  }
116  else
117  {
118  int i =0;
119  while (m_enumPickers[i].isDone())
120  {
121  i++;
122  }
123 
124  if (i < m_enumPickers.length)
125  {
126  for (int j = 0; j < i; j++)
127  {
128  m_enumPickers[j].reset();
129  m_values[j] = m_enumPickers[j].pick();
130  }
131  m_values[i] = m_enumPickers[i].pick();
132 
133  m_done = internalIsDone();
134 
135  }
136  else
137  {
138  throw new NoMoreElementException();
139  }
140 
141  }
142  }
143 
144  /**
145  * Private method used by the private pick method to check if at least one more object can be
146  * generated.
147  * @return <tt>true</tt> if the picker can still generate at least one more object and
148  * <tt>false</tt> if it's not the case.
149  */
150  private boolean internalIsDone()
151  {
152  int counter = 0;
153  for (int i = 0; i < m_enumPickers.length; i++)
154  {
155  if (m_enumPickers[i].isDone())
156  {
157  counter++;
158  }
159  }
160  return counter == m_enumPickers.length;
161  }
162 
163  /**
164  * Private method used to generate the first combination of value from the
165  * array of EnumarativePickers.
166  */
167  private void firstPick()
168  {
169  for (int i = 0; i < m_enumPickers.length; i++)
170  {
171  m_values[i] = m_enumPickers[i].pick();
172  }
173  m_firstPick = false;
174  }
175 
176  @SuppressWarnings("rawtypes")
177  @Override
178  public AllPickers duplicate(boolean with_state)
179  {
180  Bounded[] enum_picker_copy = new Bounded[m_enumPickers.length];
181  Object[] values_copy = new Object[m_enumPickers.length];
182 
183  for (int i = 0; i < m_enumPickers.length; i++)
184  {
185  enum_picker_copy[i] = (Bounded<?>) m_enumPickers[i].duplicate(with_state);
186  values_copy[i] = m_values[i];
187  }
188 
189  AllPickers copy = new AllPickers(enum_picker_copy, m_firstPick, values_copy, m_done);
190 
191  if (!with_state)
192  {
193  copy.reset();
194  }
195 
196  return copy;
197  }
198 }
ca.uqac.lif.synthia.enumerative.AllPickers.m_done
boolean m_done
Flag to check if the picker finished generating objects.
Definition: AllPickers.java:36
ca.uqac.lif.synthia.enumerative.AllPickers.duplicate
AllPickers duplicate(boolean with_state)
Creates a copy of the picker.
Definition: AllPickers.java:178
ca.uqac.lif.synthia.enumerative.AllPickers.m_values
Object[] m_values
An array to store the combination to return.
Definition: AllPickers.java:41
ca.uqac.lif.synthia.enumerative.AllPickers.reset
void reset()
Puts the picker back into its initial state.
Definition: AllPickers.java:74
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.enumerative.AllPickers.m_firstPick
boolean m_firstPick
Flag to check if it's the first pick.
Definition: AllPickers.java:31
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
ca.uqac.lif.synthia.enumerative.AllPickers.pick
Object[] pick()
Picks an object.
Definition: AllPickers.java:87
ca.uqac.lif.synthia.enumerative.AllPickers
Picker who implements Bounded.
Definition: AllPickers.java:21
ca.uqac.lif.synthia.enumerative.AllPickers.m_enumPickers
Bounded<?>[] m_enumPickers
The array of pickers used to generate all the possible combinations.
Definition: AllPickers.java:26
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.AllPickers.AllPickers
AllPickers(Bounded<?>[] enum_pickers)
Definition: AllPickers.java:59
ca.uqac.lif.synthia.enumerative.AllPickers.isDone
boolean isDone()
Signals if the picker enumerates all values from a set.
Definition: AllPickers.java:68