Synthia
Generic and flexible data structure generator
CompositePicker.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.collection;
20 
21 import ca.uqac.lif.synthia.Picker;
22 
23 /**
24  * Picker that merges the result of other pickers into a
25  * composite data structure. Descendants of this class use a
26  * different data structure.
27  * @ingroup API
28  */
29 public abstract class CompositePicker<T> implements Picker<T>
30 {
31  /**
32  * The pickers used to generate the values
33  */
34  /*@ non_null @*/ protected Picker<?>[] m_pickers;
35 
36  /**
37  * Creates a new composite picker
38  * @param pickers The pickers used to generate the values
39  */
40  public CompositePicker(/*@ non_null @*/ Picker<?> ... pickers)
41  {
42  super();
43  m_pickers = pickers;
44  }
45 
46  /**
47  * Protected method to duplicate {@link #m_pickers}. This method allows to reuse code in the
48  * duplicate method of other classes who extend this one.
49  *
50  * @param with_state If set to <tt>false</tt>, the returned copy is set to
51  * the class' initial state (i.e. same thing as calling the picker's
52  * constructor). If set to <tt>true</tt>, the returned copy is put into the
53  * same internal state as the object it is copied from.
54  * @return The copy of the {@link #m_pickers}.
55  */
56  protected Picker<?>[] duplicateM_pickers(boolean with_state)
57  {
58  Picker<?>[] duplicates = new Picker<?>[m_pickers.length];
59  for (int i = 0; i < m_pickers.length; i++)
60  {
61  duplicates[i] = m_pickers[i].duplicate(with_state);
62  }
63 
64  return duplicates;
65  }
66 
67 
68  /**
69  * Creates a copy of the composite picker.
70  *
71  * @param with_state If set to <tt>false</tt>, the returned copy is set to
72  * the class' initial state (i.e. same thing as calling the picker's
73  * constructor). If set to <tt>true</tt>, the returned copy is put into the
74  * same internal state as the object it is copied from.
75  * @return The copy of the composite picker
76  */
77  @Override
78  public Picker<T> duplicate(boolean with_state)
79  {
80  return newPicker(duplicateM_pickers(with_state));
81  }
82 
83 
84 
85  /**
86  * Picks a value of each picker in m_pickers. Typically, this method is expected to return non-null
87  * objects; a <tt>null</tt> return value is used to signal that no more
88  * objects will be produced. That is, once this method returns
89  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
90  * calls.
91  * @return An array with a value picked from each picker in the picker list.
92  */
93  @Override
94  public T pick()
95  {
96  Object[] out = new Object[m_pickers.length];
97  for (int i = 0; i < m_pickers.length; i++)
98  {
99  out[i] = m_pickers[i].pick();
100  }
101  return getOutput(out);
102  }
103 
104 
105  /**
106  * Puts the composite picker back into its initial state. This means that the
107  * sequence of calls to {@link #pick()} will produce the same values
108  * as when the object was instantiated.
109  */
110  @Override
111  public void reset()
112  {
113  for (Picker<?> m_picker : m_pickers)
114  {
115  m_picker.reset();
116  }
117  }
118 
119  /**
120  * Creates a duplicate of the current picker
121  * @param pickers The internal pickers, already duplicated
122  * @return A new instance of the picker
123  */
124  protected abstract CompositePicker<T> newPicker(Picker<?> ... pickers);
125 
126  /**
127  * Creates the output composite object from the internal
128  * values that have been picked
129  * @param objects The internal values
130  * @return The composite object
131  */
132  protected abstract T getOutput(Object ... objects);
133 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.collection.CompositePicker.duplicateM_pickers
Picker<?>[] duplicateM_pickers(boolean with_state)
Protected method to duplicate m_pickers.
Definition: CompositePicker.java:56
ca.uqac.lif.synthia.collection.CompositePicker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the composite picker.
Definition: CompositePicker.java:78
ca.uqac.lif.synthia.collection.CompositePicker.m_pickers
Picker<?>[] m_pickers
The pickers used to generate the values.
Definition: CompositePicker.java:34
ca.uqac.lif.synthia.collection.CompositePicker.pick
T pick()
Picks a value of each picker in m_pickers.
Definition: CompositePicker.java:94
ca.uqac
ca.uqac.lif.synthia.collection.CompositePicker
Picker that merges the result of other pickers into a composite data structure.
Definition: CompositePicker.java:29
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.collection.CompositePicker.CompositePicker
CompositePicker(Picker<?> ... pickers)
Creates a new composite picker.
Definition: CompositePicker.java:40
ca.uqac.lif
ca.uqac.lif.synthia.collection.CompositePicker.newPicker
abstract CompositePicker< T > newPicker(Picker<?> ... pickers)
Creates a duplicate of the current picker.
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.collection.CompositePicker.reset
void reset()
Puts the composite picker back into its initial state.
Definition: CompositePicker.java:111
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
ca.uqac.lif.synthia.collection.CompositePicker.getOutput
abstract T getOutput(Object ... objects)
Creates the output composite object from the internal values that have been picked.