Synthia
Generic and flexible data structure generator
ListPicker.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 java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 
25 import ca.uqac.lif.synthia.Picker;
27 
28 /**
29  * Picker that merges the result of other pickers into a list.
30  * @ingroup API
31  */
32 public class ListPicker extends CompositePicker<List<Object>>
33 {
35 
36 
37  /**
38  * Default constructor using a constant list size.
39  * @param pickers The array of pickers used to generate the values
40  */
41  public ListPicker(Picker<?> ... pickers)
42  {
43  super(pickers);
44  m_sizePicker = new Constant<>(pickers.length);
45  }
46 
47  /**
48  * Constructor who takes a {@link ca.uqac.lif.synthia.Picker<Integer>} to determine the size of
49  * the list returned by the {@link #pick()} method.
50  *
51  * @param size_picker A picker who picks the size of the list returned by
52  * the {@link #pick()} method.
53  * @param pickers A list of {@link ca.uqac.lif.synthia.Picker} used to produce lists of values.
54  */
55  public ListPicker(Picker<Integer> size_picker, Picker<?>... pickers)
56  {
57  super(pickers);
58  m_sizePicker = size_picker;
59  }
60 
61  /**
62  * Returns a new list picker. This version will return an instance who returns lists of constant
63  * size with {@link #pick()} method.
64  *
65  * @param pickers The pickers used to generate the values
66  * @return A new instance of {@link ListPicker}.
67  */
68  @Override
69  public ListPicker newPicker(Picker<?> ... pickers)
70  {
71  return new ListPicker(pickers);
72  }
73 
74  /**
75  * Returns a new list picker. This version will return an instance who returns lists of variable
76  * size with {@link #pick()} method. The size of the lists returned by the {@link #pick()} method
77  * is determined by a {@link ca.uqac.lif.synthia.Picker<Integer>}.
78  *
79  * @param size_picker A picker who picks the size of the list returned by
80  * the {@link #pick()} method.
81  * @param pickers A list of {@link ca.uqac.lif.synthia.Picker} used to produce lists of values.
82  * @return A new instance of {@link ListPicker}.
83  */
84  public ListPicker newPicker(Picker<Integer> size_picker, Picker<?>... pickers)
85  {
86  return new ListPicker(size_picker, pickers);
87  }
88 
89  /**
90  * Return a list of values
91  * @param values The values to return
92  * @return The values
93  */
94  @Override
95  public List<Object> getOutput(Object ... values)
96  {
97  List<Object> list = new ArrayList<>(values.length);
98  Collections.addAll(list, values);
99  return list;
100  }
101 
102  @Override
103  public List<Object> pick ()
104  {
105  List<Object> picked_elements = new ArrayList<>();
106  int size = m_sizePicker.pick();
107  int index =0;
108 
109  for (int i = 0; i < size; i++)
110  {
111  picked_elements.add(m_pickers[index].pick());
112  index++;
113 
114  // reset index if size >= m_pickers.length
115  if (index == m_pickers.length)
116  {
117  index = 0;
118  }
119  }
120 
121  return picked_elements;
122  }
123 
124  @Override
125  public void reset()
126  {
127  super.reset(); //reset m_pickers
129  }
130 
131  @Override
132  public ListPicker duplicate(boolean with_state)
133  {
134  return newPicker(m_sizePicker.duplicate(with_state), super.duplicateM_pickers(with_state));
135  }
136 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.collection.ListPicker.newPicker
ListPicker newPicker(Picker< Integer > size_picker, Picker<?>... pickers)
Returns a new list picker.
Definition: ListPicker.java:84
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.collection.CompositePicker< List< Object > >::m_pickers
Picker<?>[] m_pickers
The pickers used to generate the values.
Definition: CompositePicker.java:34
ca.uqac
ca.uqac.lif.synthia.collection.ListPicker
Picker that merges the result of other pickers into a list.
Definition: ListPicker.java:32
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.util.Constant
Picker that returns the same object every time.
Definition: Constant.java:37
ca.uqac.lif.synthia.collection.ListPicker.duplicate
ListPicker duplicate(boolean with_state)
Definition: ListPicker.java:132
ca.uqac.lif
ca.uqac.lif.synthia.collection.ListPicker.reset
void reset()
Definition: ListPicker.java:125
ca.uqac.lif.synthia.collection.ListPicker.m_sizePicker
Picker< Integer > m_sizePicker
Definition: ListPicker.java:34
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.collection.ListPicker.newPicker
ListPicker newPicker(Picker<?> ... pickers)
Returns a new list picker.
Definition: ListPicker.java:69
ca.uqac.lif.synthia.collection.ListPicker.ListPicker
ListPicker(Picker< Integer > size_picker, Picker<?>... pickers)
Constructor who takes a ca.uqac.lif.synthia.Picker<Integer> to determine the size of the list returne...
Definition: ListPicker.java:55
ca.uqac.lif.synthia.collection.ListPicker.ListPicker
ListPicker(Picker<?> ... pickers)
Default constructor using a constant list size.
Definition: ListPicker.java:41
ca.uqac.lif.synthia.collection.ListPicker.pick
List< Object > pick()
Definition: ListPicker.java:103
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
ca.uqac.lif.synthia.Picker.reset
void reset()
Puts the picker back into its initial state.
ca.uqac.lif.synthia.collection.ListPicker.getOutput
List< Object > getOutput(Object ... values)
Return a list of values.
Definition: ListPicker.java:95