Synthia
Generic and flexible data structure generator
DeleteElement.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;
25 import ca.uqac.lif.synthia.util.Mutator;
26 
27 /**
28  * Mutator that receives a list and selects an element to delete from it.
29  *
30  * @param <T> The type of elements in the list
31  * @ingroup API
32  */
33 public class DeleteElement<T> extends Mutator<List<T>>
34 {
35  /**
36  * A picker used to select the position of the element.
37  */
38  /*@ non_null @*/ protected Picker<Float> m_position;
39 
40  /**
41  * Creates a new instance of the picker.
42  * @param picker The underlying picker producing the lists to transform
43  * @param position A picker used to select the position of the first element
44  */
45  public DeleteElement(Picker<? extends List<T>> picker, Picker<Float> position)
46  {
47  super(picker);
48  m_position = position;
49  }
50 
51  @Override
52  public List<T> pick()
53  {
54  List<T> original = m_picker.pick();
55  int len = original.size();
56  int position1 = (int) Math.floor(m_position.pick() * len);
57  List<T> new_list = new ArrayList<T>(len);
58  new_list.addAll(original);
59  new_list.remove(position1);
60  return new_list;
61  }
62 
63  @Override
64  public DeleteElement<T> duplicate(boolean with_state)
65  {
66  return new DeleteElement<T>(m_picker.duplicate(with_state), m_position.duplicate(with_state));
67  }
68 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.collection.DeleteElement.duplicate
DeleteElement< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: DeleteElement.java:64
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
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.collection.DeleteElement.pick
List< T > pick()
Picks an object.
Definition: DeleteElement.java:52
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.collection.DeleteElement.DeleteElement
DeleteElement(Picker<? extends List< T >> picker, Picker< Float > position)
Creates a new instance of the picker.
Definition: DeleteElement.java:45
ca.uqac.lif.synthia.collection.DeleteElement.m_position
Picker< Float > m_position
A picker used to select the position of the element.
Definition: DeleteElement.java:38
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
ca.uqac.lif.synthia.collection.DeleteElement
Mutator that receives a list and selects an element to delete from it.
Definition: DeleteElement.java:33