Synthia
Generic and flexible data structure generator
PickSmallerComparable.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.relative;
20 
21 import ca.uqac.lif.synthia.Picker;
25 
26 /**
27  * A variant of {@link PickIf} that selects an element if it is smaller than
28  * a reference object. In order for the "smaller" condition to be evaluated,
29  * the objects that are manipulated must implement the {@link Comparable}
30  * interface.
31  *
32  * @author Sylvain Hallé
33  *
34  * @param <T> The type of objects manipulated by this picker
35  * @ingroup API
36  */
37 public class PickSmallerComparable<T> extends PickIf<T> implements Shrinkable<T>
38 {
39  /**
40  * The reference object that candidate values are compared to.
41  */
42  protected Comparable<T> m_reference;
43 
44  /**
45  * Creates a new instance of the picker.
46  * @param source The source of objects
47  * @param o The reference object that candidate values are compared to
48  */
49  @SuppressWarnings("unchecked")
50  public PickSmallerComparable(Picker<? extends T> source, T o)
51  {
52  super(source);
53  if (!(o instanceof Comparable))
54  {
55  throw new PickerException("Expected a comparable object");
56  }
57  m_reference = (Comparable<T>) o;
58  }
59 
60  @SuppressWarnings("unchecked")
61  @Override
62  public PickSmallerComparable<T> duplicate(boolean with_state)
63  {
65  super.copyInto(this, with_state);
66  if (with_state)
67  {
69  }
70  return ps;
71  }
72 
73  @Override
74  public PickSmallerComparable<T> shrink(T o, Picker<Float> decision, float m)
75  {
78  return ps;
79  }
80 
81  @Override
82  protected boolean select(T element)
83  {
84  return m_reference.compareTo(element) > 0;
85  }
86 
87  @Override
88  public Shrinkable<T> shrink(T o)
89  {
90  return shrink(o, RandomFloat.instance, 1);
91  }
92 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.relative.PickSmallerComparable.select
boolean select(T element)
Method to evaluate if an element is satisfying a condition.
Definition: PickSmallerComparable.java:82
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
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.relative.PickSmallerComparable.m_reference
Comparable< T > m_reference
The reference object that candidate values are compared to.
Definition: PickSmallerComparable.java:42
ca.uqac.lif.synthia.relative.PickSmallerComparable
A variant of PickIf that selects an element if it is smaller than a reference object.
Definition: PickSmallerComparable.java:37
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.PickerException
Generic runtime exception thrown by the Picker interface.
Definition: PickerException.java:26
ca.uqac.lif
ca.uqac.lif.synthia.relative.PickIf
Returns object from a picker satisfying a condition.
Definition: PickIf.java:39
ca
ca.uqac.lif.synthia.relative.PickSmallerComparable.shrink
Shrinkable< T > shrink(T o)
Shrinks a picker with default parameters.
Definition: PickSmallerComparable.java:88
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.relative.PickIf.m_maxIteration
int m_maxIteration
The maximal number of iteration that the while loop of the pick() method can do.
Definition: PickIf.java:45
ca.uqac.lif.synthia.relative.PickSmallerComparable.shrink
PickSmallerComparable< T > shrink(T o, Picker< Float > decision, float m)
Shrinks a picker.
Definition: PickSmallerComparable.java:74
ca.uqac.lif.synthia.Picker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
ca.uqac.lif.synthia.relative.PickSmallerComparable.duplicate
PickSmallerComparable< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: PickSmallerComparable.java:62