/
ProxyIdTagManager.java
232 lines (205 loc) · 6.58 KB
/
ProxyIdTagManager.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package jmri.managers;
import java.util.List;
import java.util.ArrayList;
import java.util.SortedSet;
import javax.annotation.Nonnull;
import jmri.IdTag;
import jmri.IdTagManager;
import jmri.Manager;
import jmri.Reporter;
import jmri.InstanceManager;
import jmri.jmrix.internal.InternalSystemConnectionMemo;
/**
* Implementation of a IdTagManager that can serve as a proxy for multiple
* system-specific implementations.
*
* @author Bob Jacobsen Copyright (C) 2010, 2018
* @author Dave Duchamp Copyright (C) 2004
* @author Paul Bender Copyright (C) 2019
*/
public class ProxyIdTagManager extends AbstractProxyManager<IdTag>
implements IdTagManager {
public ProxyIdTagManager() {
super();
}
@Override
public int getXMLOrder() {
return jmri.Manager.IDTAGS;
}
@Override
public void init() {
if (!isInitialised()) {
getDefaultManager();
}
}
@Override
public boolean isInitialised() {
return defaultManager!= null &&
getManagerList().stream().noneMatch(o->((IdTagManager)o).isInitialised());
}
/**
* {@inheritDoc}
*/
@Override
@Nonnull
public Manager<IdTag> getDefaultManager() {
if(defaultManager != getInternalManager()){
defaultManager = getInternalManager();
}
return defaultManager;
}
@Override
protected AbstractManager<IdTag> makeInternalManager() {
// since this really is an internal tracking mechanisim,
// build the new manager and add it here.
DefaultIdTagManager tagMan = new DefaultIdTagManager(InstanceManager.getDefault(InternalSystemConnectionMemo.class));
InstanceManager.setIdTagManager(tagMan);
return tagMan;
}
/**
* Locate via user name, then system name if needed.
*
* @return Null if nothing by that name exists
*/
@Override
public IdTag getIdTag(@Nonnull String name) {
init();
return super.getNamedBean(name);
}
/** {@inheritDoc} */
@Override
@Nonnull
public SortedSet<IdTag> getNamedBeanSet() {
init();
return super.getNamedBeanSet();
}
@Override
protected IdTag makeBean(Manager<IdTag> manager, String systemName, String userName) {
init();
return ((IdTagManager) manager).newIdTag(systemName, userName);
}
/**
* {@inheritDoc}
*/
@Override
@Nonnull
public IdTag provide(@Nonnull String name) throws IllegalArgumentException {
return provideIdTag(name);
}
/**
* Locate via user name, then system name if needed. If that fails, create a
* new IdTag: If the name is a valid system name, it will be used for the
* new IdTag. Otherwise, the makeSystemName method will attempt to turn it
* into a valid system name.
*
* @return Never null under normal circumstances
*/
@Override
@Nonnull
public IdTag provideIdTag(@Nonnull String name) throws IllegalArgumentException {
init();
return super.provideNamedBean(name);
}
/**
* Get an instance with the specified system and user names. Note that
* two calls with the same arguments will get the same instance; there is
* only one IdTag object representing a given physical light and therefore
* only one with a specific system or user name.
* <p>
* This will always return a valid object reference for a valid request; a
* new object will be created if necessary. In that case:
* <ul>
* <li>If a null reference is given for user name, no user name will be
* associated with the IdTag object created; a valid system name must be
* provided
* <li>If a null reference is given for the system name, a system name will
* _somehow_ be inferred from the user name. How this is done is system
* specific. Note: a future extension of this interface will add an
* exception to signal that this was not possible.
* <li>If both names are provided, the system name defines the hardware
* access of the desired turnout, and the user address is associated with
* it.
* </ul>
* Note that it is possible to make an inconsistent request if both
* addresses are provided, but the given values are associated with
* different objects. This is a problem, and we don't have a good solution
* except to issue warnings. This will mostly happen if you're creating
* IdTags when you should be looking them up.
*
* @return requested IdTag object (never null)
*/
@Override
@Nonnull
public IdTag newIdTag(@Nonnull String systemName, String userName) {
init();
return newNamedBean(systemName, userName);
}
@Override
public IdTag getByTagID(@Nonnull String tagID) {
init();
return getBySystemName(makeSystemName(tagID));
}
/**
* {@inheritDoc}
*/
@Override
public String getEntryToolTip() {
return "Enter a number from 1 to 9999"; // Basic number format help
}
@Override
@Nonnull
public String getBeanTypeHandled(boolean plural) {
return Bundle.getMessage(plural ? "BeanNameIdTags" : "BeanNameIdTag");
}
/**
* {@inheritDoc}
*/
@Override
public Class<IdTag> getNamedBeanClass() {
return IdTag.class;
}
private boolean stateSaved = false;
@Override
public void setStateStored(boolean state) {
stateSaved = state;
for (Manager<IdTag> mgr : getManagerList()) {
((IdTagManager) mgr).setStateStored(state);
}
}
@Override
public boolean isStateStored() {
stateSaved = true;
for (Manager<IdTag> mgr: getManagerList()) {
if(!((IdTagManager) mgr).isStateStored()) {
stateSaved = false;
break;
}
}
return stateSaved;
}
private boolean useFastClock = false;
@Override
public void setFastClockUsed(boolean fastClock) {
useFastClock = fastClock;
for (Manager<IdTag> mgr : getManagerList()) {
((IdTagManager) mgr).setFastClockUsed(fastClock);
}
}
@Override
public boolean isFastClockUsed() {
useFastClock = true;
for (Manager<IdTag> mgr: getManagerList()) {
if (!((IdTagManager) mgr).isFastClockUsed()) {
useFastClock = false;
break;
}
}
return useFastClock;
}
@Override
@Nonnull
public List<IdTag> getTagsForReporter(@Nonnull Reporter reporter, long threshold) {
init();
return new ArrayList<>();
}
}