Synthia
Generic and flexible data structure generator
Delay.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.util;
20 
21 import ca.uqac.lif.synthia.Picker;
22 
23 /**
24  * Picker that does not produce any value, but causes the thread to wait
25  * for a moment on every call to {@link #pick()}.
26  * @author Sylvain Hallé
27  */
28 public class Delay implements Picker<Object>
29 {
30  /**
31  * The picker deciding on the time to wait, in seconds.
32  */
34 
35  /**
36  * Creates a new delay picker.
37  * @param delay The picker deciding on the time to wait, in seconds
38  */
40  {
41  super();
42  m_delay = delay;
43  }
44 
45  @Override
46  public void reset()
47  {
48  m_delay.reset();
49  }
50 
51  @Override
52  public Object pick()
53  {
54  wait(m_delay.pick().floatValue());
55  return null;
56  }
57 
58  @Override
59  public Delay duplicate(boolean with_state)
60  {
61  return new Delay(m_delay.duplicate(with_state));
62  }
63 
64  /**
65  * Waits for some time.
66  * @param duration The time to wait, in seconds
67  */
68  public static void wait(float duration)
69  {
70  try
71  {
72  Thread.sleep((long) (duration * 1000));
73  }
74  catch (InterruptedException e)
75  {
76  // Do nothing
77  }
78  }
79 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.util.Delay.m_delay
Picker<? extends Number > m_delay
The picker deciding on the time to wait, in seconds.
Definition: Delay.java:33
ca.uqac.lif.synthia.util.Delay.pick
Object pick()
Picks an object.
Definition: Delay.java:52
ca.uqac.lif.synthia.util.Delay.Delay
Delay(Picker<? extends Number > delay)
Creates a new delay picker.
Definition: Delay.java:39
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.util.Delay.wait
static void wait(float duration)
Waits for some time.
Definition: Delay.java:68
ca.uqac.lif.synthia.util.Delay
Picker that does not produce any value, but causes the thread to wait for a moment on every call to p...
Definition: Delay.java:28
ca.uqac.lif
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.util.Delay.duplicate
Delay duplicate(boolean with_state)
Creates a copy of the picker.
Definition: Delay.java:59
ca.uqac.lif.synthia.util.Delay.reset
void reset()
Puts the picker back into its initial state.
Definition: Delay.java:46
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.