Synthia
Generic and flexible data structure generator
Unbound.java
Go to the documentation of this file.
1
package
ca.uqac.lif.synthia.enumerative;
2
3
import
java.util.ArrayList;
4
import
java.util.List;
5
6
import
ca
.
uqac
.
lif
.
synthia
.
NoMoreElementException
;
7
import
ca
.
uqac
.
lif
.
synthia
.
Picker
;
8
9
/**
10
*
11
* @author Sylvain Hallé
12
*
13
* @param <T>
14
* @ingroup API
15
*/
16
public
class
Unbound
<T>
implements
Picker
<T>
17
{
18
/*@ non_null @*/
protected
Picker<T>
m_source
;
19
20
/*@ non_null @*/
protected
Picker<Float>
m_floatSource
;
21
22
/*@ non_null @*/
protected
List<T>
m_previousValues
;
23
24
protected
boolean
m_sourceDone
;
25
26
public
Unbound
(
/*@ non_null @*/
Picker<T>
source,
Picker<Float>
float_source)
27
{
28
super();
29
m_source
= source;
30
m_floatSource
= float_source;
31
m_previousValues
=
new
ArrayList<T>();
32
m_sourceDone
=
false
;
33
}
34
35
@Override
36
public
void
reset
()
37
{
38
m_previousValues
.clear();
39
m_source
.reset();
40
m_floatSource
.
reset
();
41
}
42
43
@Override
44
public
T
pick
()
45
{
46
if
(
m_sourceDone
)
47
{
48
return
pickFromList
();
49
}
50
try
51
{
52
T t =
m_source
.pick();
53
if
(!
m_previousValues
.contains(t))
54
{
55
m_previousValues
.add(t);
56
}
57
return
t;
58
}
59
catch
(
NoMoreElementException
e)
60
{
61
m_sourceDone
=
true
;
62
return
pickFromList
();
63
}
64
}
65
66
protected
T
pickFromList
()
67
{
68
if
(
m_previousValues
.isEmpty())
69
{
70
throw
new
NoMoreElementException
();
71
}
72
int
index = (int) Math.floor((
float
)
m_previousValues
.size() *
m_floatSource
.
pick
());
73
return
m_previousValues
.get(index);
74
}
75
76
@Override
77
public
Unbound<T>
duplicate
(
boolean
with_state)
78
{
79
Unbound<T>
u =
new
Unbound<T>
(
m_source
.duplicate(with_state),
m_floatSource
.
duplicate
(with_state));
80
if
(with_state)
81
{
82
u.
m_sourceDone
=
m_sourceDone
;
83
u.
m_previousValues
.addAll(
m_previousValues
);
84
}
85
return
u;
86
}
87
}
ca.uqac.lif.synthia.Picker
Picks an object.
Definition:
Picker.java:36
ca.uqac.lif.synthia.enumerative.Unbound.reset
void reset()
Puts the picker back into its initial state.
Definition:
Unbound.java:36
ca.uqac.lif.synthia.enumerative.Unbound.pick
T pick()
Picks an object.
Definition:
Unbound.java:44
ca.uqac.lif.synthia.enumerative.Unbound.pickFromList
T pickFromList()
Definition:
Unbound.java:66
ca.uqac
ca.uqac.lif.synthia.enumerative.Unbound.Unbound
Unbound(Picker< T > source, Picker< Float > float_source)
Definition:
Unbound.java:26
ca.uqac.lif.synthia
Definition:
Bounded.java:19
ca.uqac.lif.synthia.enumerative.Unbound.duplicate
Unbound< T > duplicate(boolean with_state)
Creates a copy of the picker.
Definition:
Unbound.java:77
ca.uqac.lif
ca.uqac.lif.synthia.enumerative.Unbound.m_sourceDone
boolean m_sourceDone
Definition:
Unbound.java:24
ca
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.enumerative.Unbound.m_floatSource
Picker< Float > m_floatSource
Definition:
Unbound.java:20
ca.uqac.lif.synthia.enumerative.Unbound
Definition:
Unbound.java:16
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.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.
ca.uqac.lif.synthia.enumerative.Unbound.m_source
Picker< T > m_source
Definition:
Unbound.java:18
ca.uqac.lif.synthia.enumerative.Unbound.m_previousValues
List< T > m_previousValues
Definition:
Unbound.java:22
Source
Core
src
ca
uqac
lif
synthia
enumerative
Unbound.java
Generated by
1.8.17