Synthia
Generic and flexible data structure generator
Constant.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.util;
20 
21 import ca.uqac.lif.synthia.Picker;
24 
25 /**
26  * Picker that returns the same object every time. For example, the following
27  * code snippet will create an object that will return the string "foo" every
28  * time its {@link #pick()} method is called:
29  * <pre>
30  * Constant&lt;String&gt; c = new Constant&lt;String&gt;("foo");
31  * String s1 = c.pick(); // "foo"
32  * String s2 = c.pick(); // "foo"
33  * ...
34  * </pre>
35  * @ingroup API
36  */
37 public class Constant<T> implements Shrinkable<T>
38 {
39  /**
40  * The value to return
41  */
42  /*@ non_null @*/ protected T m_value;
43 
44  /**
45  * Creates a new constant
46  * @param value The value to be returned on every call to {@link #pick()}
47  */
48  public Constant(/*@ non_null @*/ T value)
49  {
50  super();
51  m_value = value;
52  }
53 
54  /**
55  * Picks the constant value.
56  * @return The constant value.
57  */
58  @Override
59  public T pick()
60  {
61  return m_value;
62  }
63 
64 
65  /**
66  * Puts the constant back into its initial state. This means that the
67  * sequence of calls to {@link #pick()} will produce the same values
68  * as when the object was instantiated.
69  */
70  @Override
71  public void reset()
72  {
73  // Nothing to do
74 
75  }
76 
77 
78  /**
79  * Creates a copy of the constant picker.
80  * @param with_state If set to <tt>false</tt>, the returned copy is set to
81  * the class' initial state (i.e. same thing as calling the picker's
82  * constructor). If set to <tt>true</tt>, the returned copy is put into the
83  * same internal state as the object it is copied from.
84  * @return The copy of the constant picker
85  */
86  @Override
87  public Constant<T> duplicate(boolean with_state)
88  {
89  return new Constant<>(m_value);
90  }
91 
92 
93  /**
94  * Returns the constant value into a string.
95  * @return The string who contains the constant value.
96  */
97  @Override
98  public String toString()
99  {
100  return m_value.toString();
101  }
102 
103  @Override
104  public Shrinkable<T> shrink(T o, Picker<Float> decision, float m)
105  {
106  return new NothingPicker<T>();
107  }
108 
109  @Override
110  public Shrinkable<T> shrink(T o)
111  {
112  return shrink(o, RandomFloat.instance, 1);
113  }
114 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.util.Constant.pick
T pick()
Picks the constant value.
Definition: Constant.java:59
ca.uqac.lif.synthia.util.Constant.shrink
Shrinkable< T > shrink(T o)
Shrinks a picker with default parameters.
Definition: Constant.java:110
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac.lif.synthia.util.Constant.m_value
T m_value
The value to return.
Definition: Constant.java:42
ca.uqac
ca.uqac.lif.synthia.util.Constant.toString
String toString()
Returns the constant value into a string.
Definition: Constant.java:98
ca.uqac.lif.synthia.util.Constant.duplicate
Constant< T > duplicate(boolean with_state)
Creates a copy of the constant picker.
Definition: Constant.java:87
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.util.Constant
Picker that returns the same object every time.
Definition: Constant.java:37
ca.uqac.lif.synthia.util.Constant.Constant
Constant(T value)
Creates a new constant.
Definition: Constant.java:48
ca.uqac.lif
ca.uqac.lif.synthia.util.NothingPicker
A RelativePicker that only throws a NoMoreElementException when the pick() method is called.
Definition: NothingPicker.java:36
ca.uqac.lif.synthia.util.Constant.shrink
Shrinkable< T > shrink(T o, Picker< Float > decision, float m)
Shrinks a picker.
Definition: Constant.java:104
ca
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.util.Constant.reset
void reset()
Puts the constant back into its initial state.
Definition: Constant.java:71