Synthia
Generic and flexible data structure generator
Once.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 
22 import ca.uqac.lif.synthia.Picker;
23 
24 /**
25  * Picker that returns the first value fetched from another picker, and
26  * then <tt>null</tt> afterwards. For example, the following piece of
27  * code instantiates a
28  * {@link ca.uqac.lif.synthia.random.RandomFloat RandomFloat}
29  * <tt>r</tt>; the <tt>Once</tt> picker wraps around it, returns the
30  * first random float returned by <tt>r</tt>, and then <tt>null</tt> on
31  * subsequent calls.
32  * <pre>
33  * RandomFloat r = new RandomFloat();
34  * Once&lt;Float&gt; f = new Once&lt;Float&gt;(r);
35  * float f1 = f.pick(); // 0.8104950, for example
36  * float f2 = f.pick(); // null
37  * float f3 = f.pick(); // null again
38  * ...</pre>
39  * Note that a call to {@link #reset()} will cause the picker to return a
40  * non-null object on its next call to {@link #pick()}.
41  *
42  * @param <T> The type of object to pick
43  * @ingroup API
44  */
45 public class Once<T> implements Picker<T>
46 {
47  /**
48  * The internal picker that is to be called
49  */
50  /*@ non_null @*/ Picker<T> m_picker;
51 
52  /**
53  * A flag indicating whether <tt>m_picker</tt> has been called once
54  */
55  boolean m_picked;
56 
57  /**
58  * Creates a new Once picker.
59  * @param picker The underlying picker used to get the first
60  * value
61  */
62  public Once(/*@ non_null @*/ Picker<T> picker)
63  {
64  super();
65  m_picker = picker;
66  m_picked = false;
67  }
68 
69 
70  /**
71  * Puts the once picker back into its initial state. This means that the
72  * sequence of calls to {@link #pick()} will produce the same values
73  * as when the object was instantiated.
74  */
75  @Override
76  public void reset()
77  {
78  m_picked = false;
79  m_picker.reset();
80 
81  }
82 
83 
84  /**
85  * Picks a random value. Typically, this method is expected to return non-null
86  * objects; a <tt>null</tt> return value is used to signal that no more
87  * objects will be produced. That is, once this method returns
88  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
89  * calls.
90  * @return The random string.
91  */
92  @Override
93  public T pick()
94  {
95  if (m_picked)
96  {
97  throw new NoMoreElementException();
98  }
99  m_picked = true;
100  return m_picker.pick();
101  }
102 
103 
104  /**
105  * Creates a copy of the once picker.
106  * @param with_state If set to <tt>false</tt>, the returned copy is set to
107  * the class' initial state (i.e. same thing as calling the picker's
108  * constructor). If set to <tt>true</tt>, the returned copy is put into the
109  * same internal state as the object it is copied from.
110  * @return The copy of the RandomString picker
111  */
112  @Override
113  public Once<T> duplicate(boolean with_state)
114  {
115  Once<T> o = new Once<>(m_picker.duplicate(with_state));
116  if (with_state)
117  {
118  o.m_picked = m_picked;
119  }
120  return o;
121  }
122 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.util.Once.reset
void reset()
Puts the once picker back into its initial state.
Definition: Once.java:76
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca.uqac.lif.synthia.util.Once.pick
T pick()
Picks a random value.
Definition: Once.java:93
ca.uqac.lif.synthia.util.Once
Picker that returns the first value fetched from another picker, and then null afterwards.
Definition: Once.java:45
ca
ca.uqac.lif.synthia.util.Once.Once
Once(Picker< T > picker)
Creates a new Once picker.
Definition: Once.java:62
ca.uqac.lif.synthia.util.Once.duplicate
Once< T > duplicate(boolean with_state)
Creates a copy of the once picker.
Definition: Once.java:113
ca.uqac.lif.synthia.NoMoreElementException
An exception to throw when a picker can't pick an other element.
Definition: NoMoreElementException.java:25