Synthia
Generic and flexible data structure generator
MutateElement.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 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.List;
23 
24 import ca.uqac.lif.synthia.Picker;
26 import ca.uqac.lif.synthia.util.Mutator;
27 
28 /**
29  * Picker that selects an element of a list and applies the result of a mutator
30  * on this element.
31  *
32  * @param <T> The type of the elements in the list
33  * @ingroup API
34  */
35 public class MutateElement<T> extends Mutator<List<T>>
36 {
37  /**
38  * A picker used to select the position of the first element.
39  */
41 
42  /**
43  * A mutator picker to mutate elements of the list.
44  */
46 
47  /**
48  * Creates a new instance of the picker.
49  * @param picker The underlying picker producing the lists to transform
50  * @param position A picker used to select the position of the first element
51  * @param mutator A mutator picker to mutate elements of the list
52  */
53  public MutateElement(Picker<? extends List<T>> picker, Picker<Float> position, Picker<Mutator<? extends T>> mutator)
54  {
55  super(picker);
56  m_position = position;
57  m_mutator = mutator;
58  }
59 
60  @SuppressWarnings("unchecked")
61  @Override
62  public List<T> pick()
63  {
64  List<T> list = m_picker.pick();
65  int size = list.size();
66  int position = (int) Math.floor(m_position.pick() * (double) size);
67  T e = list.get(position);
68  Mutator<T> m = (Mutator<T>) m_mutator.pick();
69  m.setPicker(new Constant<T>(e));
70  T e_m = m.pick();
71  List<T> new_list = new ArrayList<T>(size);
72  for (int i = 0; i < size; i++)
73  {
74  if (i == position)
75  {
76  new_list.add(e_m);
77  }
78  else
79  {
80  new_list.add(list.get(i));
81  }
82  }
83  return new_list;
84  }
85 
86  @Override
87  public void reset()
88  {
89  m_position.reset();
90  m_mutator.reset();
91  }
92 
93  @Override
94  public MutateElement<T> duplicate(boolean with_state)
95  {
96  return new MutateElement<T>(m_picker.duplicate(with_state), m_position.duplicate(with_state), m_mutator.duplicate(with_state));
97  }
98 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.collection.MutateElement.MutateElement
MutateElement(Picker<? extends List< T >> picker, Picker< Float > position, Picker< Mutator<? extends T >> mutator)
Creates a new instance of the picker.
Definition: MutateElement.java:53
ca.uqac.lif.synthia.collection.MutateElement.reset
void reset()
Puts the picker back into its initial state.
Definition: MutateElement.java:87
ca.uqac.lif.synthia.util.Mutator.m_picker
Picker<? extends T > m_picker
The underlying picker producing the values to transform.
Definition: Mutator.java:38
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.collection.MutateElement.duplicate
MutateElement< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: MutateElement.java:94
ca.uqac.lif.synthia.collection.MutateElement
Picker that selects an element of a list and applies the result of a mutator on this element.
Definition: MutateElement.java:35
ca.uqac
ca.uqac.lif.synthia.util.Mutator.setPicker
Mutator< T > setPicker(Picker<? extends T > picker)
Sets the picker producing the values to transform.
Definition: Mutator.java:55
ca.uqac.lif.synthia.collection.MutateElement.m_mutator
Picker< Mutator<? extends T > > m_mutator
A mutator picker to mutate elements of the list.
Definition: MutateElement.java:45
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
ca.uqac.lif.synthia.collection.MutateElement.pick
List< T > pick()
Picks an object.
Definition: MutateElement.java:62
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.util.Mutator
A picker that applies a transformation ("mutation") on the value produced by another picker.
Definition: Mutator.java:33
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.MutateElement.m_position
Picker< Float > m_position
A picker used to select the position of the first element.
Definition: MutateElement.java:40