Synthia
Generic and flexible data structure generator
Share.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 java.util.ArrayDeque;
22 import java.util.HashSet;
23 import java.util.Queue;
24 import java.util.Set;
25 
27 import ca.uqac.lif.synthia.Picker;
28 
29 /**
30  * Allows values of a single picker to be shared among multiple copies of a
31  * picker instance.
32  *
33  * @param <T> The type of the elements to pick
34  * @ingroup API
35  */
36 public class Share<T>
37 {
38  /**
39  * The underlying picker from which elements are taken.
40  */
42 
43  /**
44  * The set of "spawned" copies created.
45  */
46  protected Set<QueuePicker> m_pickers;
47 
48  /**
49  * Creates a new instance of the share picker.
50  * @param picker The underlying picker from which elements are taken
51  */
52  public Share(Picker<? extends T> picker)
53  {
54  super();
55  m_picker = picker;
56  m_pickers = new HashSet<QueuePicker>();
57  }
58 
59  /**
60  * Creates a new copy of {@link QueuePicker} from the original picker.
61  * @return A new copy
62  */
64  {
65  QueuePicker qp = new QueuePicker();
66  m_pickers.add(qp);
67  return qp;
68  }
69 
70  /**
71  * Resets the original picker and all its copies.
72  */
73  public void reset()
74  {
75  m_picker.reset();
76  for (QueuePicker qp : m_pickers)
77  {
78  qp.resetInternal();
79  }
80  }
81 
82  /**
83  * Asks for a new value from the underlying picker, and adds this value to
84  * the internal queue of each linked instance of {@link QueuePicker}.
85  */
86  protected void ask()
87  {
88  T e = m_picker.pick();
89  for (QueuePicker qp : m_pickers)
90  {
91  qp.m_values.add(e);
92  }
93  }
94 
95  /**
96  * A picker that acts as a proxy for the values obtained by the parent
97  * {@link Share} picker. Each instance of {@link QueuePicker} is guaranteed
98  * to output the same values in the same order as the picker given to
99  * {@link Share} when instantiated.
100  */
101  protected class QueuePicker implements Picker<T>
102  {
103  protected Queue<T> m_values;
104 
105  public QueuePicker()
106  {
107  super();
108  m_values = new ArrayDeque<T>();
109  }
110 
111  @Override
112  public void reset()
113  {
114  m_picker.reset();
115  }
116 
117  public void resetInternal()
118  {
119  m_values.clear();
120  }
121 
122  @Override
123  public T pick()
124  {
125  if (m_values.isEmpty())
126  {
127  ask();
128  }
129  if (m_values.isEmpty())
130  {
131  throw new NoMoreElementException();
132  }
133  T value = m_values.remove();
134  return value;
135  }
136 
137  @Override
138  public Picker<T> duplicate(boolean with_state)
139  {
140  throw new UnsupportedOperationException();
141  }
142  }
143 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.util.Share.QueuePicker
A picker that acts as a proxy for the values obtained by the parent Share picker.
Definition: Share.java:101
ca.uqac.lif.synthia.util.Share.m_pickers
Set< QueuePicker > m_pickers
The set of "spawned" copies created.
Definition: Share.java:46
ca.uqac.lif.synthia.util.Share.QueuePicker.reset
void reset()
Puts the picker back into its initial state.
Definition: Share.java:112
ca.uqac.lif.synthia.util.Share.m_picker
Picker<? extends T > m_picker
The underlying picker from which elements are taken.
Definition: Share.java:41
ca.uqac.lif.synthia.util.Share.QueuePicker.resetInternal
void resetInternal()
Definition: Share.java:117
ca.uqac.lif.synthia.util.Share.QueuePicker.pick
T pick()
Picks an object.
Definition: Share.java:123
ca.uqac.lif.synthia.util.Share.Share
Share(Picker<? extends T > picker)
Creates a new instance of the share picker.
Definition: Share.java:52
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.util.Share.ask
void ask()
Asks for a new value from the underlying picker, and adds this value to the internal queue of each li...
Definition: Share.java:86
ca.uqac.lif.synthia.util.Share.QueuePicker.m_values
Queue< T > m_values
Definition: Share.java:103
ca.uqac.lif
ca.uqac.lif.synthia.util.Share.getCopy
QueuePicker getCopy()
Creates a new copy of QueuePicker from the original picker.
Definition: Share.java:63
ca.uqac.lif.synthia.util.Share.reset
void reset()
Resets the original picker and all its copies.
Definition: Share.java:73
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.util.Share.QueuePicker.duplicate
Picker< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition: Share.java:138
ca.uqac.lif.synthia.util.Share
Allows values of a single picker to be shared among multiple copies of a picker instance.
Definition: Share.java:36
ca.uqac.lif.synthia.NoMoreElementException
An exception to throw when a picker can't pick an other element.
Definition: NoMoreElementException.java:25
ca.uqac.lif.synthia.util.Share.QueuePicker.QueuePicker
QueuePicker()
Definition: Share.java:105
ca.uqac.lif.synthia.Picker.reset
void reset()
Puts the picker back into its initial state.