-
Notifications
You must be signed in to change notification settings - Fork 4
/
WaitForCardRemovalStateAdapter.java
106 lines (98 loc) · 3.31 KB
/
WaitForCardRemovalStateAdapter.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
/* **************************************************************************************
* Copyright (c) 2021 Calypso Networks Association https://calypsonet.org/
*
* See the NOTICE file(s) distributed with this work for additional information
* regarding copyright ownership.
*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
************************************************************************************** */
package org.eclipse.keyple.core.service;
import java.util.concurrent.ExecutorService;
import org.eclipse.keypop.reader.ObservableCardReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Wait for card removal state implementation.
*
* <p>The state in which the card is still present and awaiting removal.
*
* <ul>
* <li>Upon CARD_REMOVED event, the machine changes state for WAIT_FOR_CARD_INSERTION or
* WAIT_FOR_CARD_DETECTION according to the {@link ObservableCardReader.DetectionMode}
* setting.
* <li>Upon STOP_DETECT event, the machine changes state for WAIT_FOR_CARD_DETECTION.
* </ul>
*
* @since 2.0.0
*/
final class WaitForCardRemovalStateAdapter extends AbstractObservableStateAdapter {
private static final Logger logger =
LoggerFactory.getLogger(WaitForCardRemovalStateAdapter.class);
/**
* Creates an instance.
*
* @param reader The observable local reader adapter.
* @since 2.0.0
*/
WaitForCardRemovalStateAdapter(ObservableLocalReaderAdapter reader) {
this(reader, null, null);
}
/**
* Creates an instance.
*
* @param reader The observable local reader adapter.
* @param monitoringJob The monitoring job.
* @param executorService The executor service to use.
* @since 2.0.0
*/
WaitForCardRemovalStateAdapter(
ObservableLocalReaderAdapter reader,
AbstractMonitoringJobAdapter monitoringJob,
ExecutorService executorService) {
super(MonitoringState.WAIT_FOR_CARD_REMOVAL, reader, monitoringJob, executorService);
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
void onEvent(ObservableLocalReaderAdapter.InternalEvent event) {
if (logger.isTraceEnabled()) {
logger.trace(
"Internal event [{}] received for reader [{}] in current state [{}]",
event,
getReader().getName(),
getMonitoringState());
}
/*
* Process InternalEvent
*/
switch (event) {
case CARD_REMOVED:
// the card has been removed, we close all channels and return to
// the currentState of waiting
// for insertion
// We notify the application of the CARD_REMOVED event.
getReader().processCardRemoved();
if (getReader().getDetectionMode() == ObservableCardReader.DetectionMode.REPEATING) {
switchState(MonitoringState.WAIT_FOR_CARD_INSERTION);
} else {
switchState(MonitoringState.WAIT_FOR_START_DETECTION);
}
break;
case STOP_DETECT:
getReader().processCardRemoved();
switchState(MonitoringState.WAIT_FOR_START_DETECTION);
break;
default:
if (logger.isTraceEnabled()) {
logger.trace("Event ignored");
}
break;
}
}
}