-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBehaviourMap.java
More file actions
143 lines (126 loc) · 3.09 KB
/
Copy pathBehaviourMap.java
File metadata and controls
143 lines (126 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package bdv.behaviour;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.ActionMap;
/**
* Maps {@link String} keys to {@link Behaviour}s. Equivalent to
* {@link ActionMap}.
*
* @author Tobias Pietzsch <tobias.pietzsch@gmail.com>
*/
public class BehaviourMap
{
/**
* Maps key to {@link Behaviour}.
* Similar to {@link ActionMap}.
*/
private final Map< String, Behaviour > behaviours;
/**
* Parent that handles any bindings we don't contain.
*/
private BehaviourMap parent;
private int expectedParentModCount;
private int modCount;
/**
* Creates an {@link BehaviourMap} with no parent and no mappings.
*/
public BehaviourMap()
{
behaviours = new HashMap<>();
parent = null;
expectedParentModCount = 0;
modCount = 0;
}
/**
* Sets this {@link BehaviourMap}'s parent.
*
* @param map
* the map that is the parent of this one
*/
public void setParent( final BehaviourMap map )
{
this.parent = map;
if ( map != null )
expectedParentModCount = parent.modCount();
++modCount;
}
/**
* Gets this {@link BehaviourMap}'s parent.
*
* @return map the map that is the parent of this one, or {@code null} if
* this map has no parent
*/
public BehaviourMap getParent()
{
return parent;
}
/**
* Adds a binding for {@code key} to {@code behaviour}. If {@code behaviour}
* is {@code null}, this removes the current binding for {@code key}.
*
* @param key
* @param behaviour
*/
public synchronized void put( final String key, final Behaviour behaviour )
{
behaviours.put( key, behaviour );
++modCount;
}
/**
* Returns the binding for {@code key}, messaging the parent
* {@link BehaviourMap} if the binding is not locally defined.
*/
public synchronized Behaviour get( final String key )
{
final Behaviour behaviour = behaviours.get( key );
if ( behaviour == null && parent != null )
return parent.get( key );
else
return behaviour;
}
/**
* Removes the binding for {@code key} from this map.
*/
public synchronized void remove( final String key )
{
behaviours.remove( key );
++modCount;
}
/**
* Removes all bindings from this map..
*/
public synchronized void clear()
{
behaviours.clear();
++modCount;
}
/**
* Get all bindings defined in this map and its parents. Note the returned
* map <em>not</em> backed by the {@link BehaviourMap}, i.e., it will not
* reflect changes to the {@link BehaviourMap}.
*
* @return all bindings defined in this map and its parents.
*/
public synchronized Map< String, Behaviour > getAllBindings()
{
final Map< String, Behaviour > allBindings =
( parent == null ) ? new HashMap<>() : parent.getAllBindings();
for ( final Entry< String, Behaviour > entry : behaviours.entrySet() )
allBindings.put( entry.getKey(), entry.getValue() );
return allBindings;
}
protected int modCount()
{
if ( parent != null )
{
final int m = parent.modCount();
if ( m != expectedParentModCount )
{
expectedParentModCount = m;
++modCount;
}
}
return modCount;
}
}