yeg
2026-08-30 b985e6140e7f395ead7dc55e41e6b16d97f9f02b
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
package com.ruoyi.device.simulator;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorHistoryAppend;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorHistoryBatch;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorMeasurementRecord;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.RecentRequest;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.SimulatorState;
 
/** 测试和独立协议合同使用的内存状态存储。 */
public final class InMemoryBtsSimulatorStateStore implements BtsSimulatorStateStore
{
    private SimulatorState state;
    private List<RecentRequest> recentRequests = List.of();
    private final Map<String, ArrayList<CollectorMeasurementRecord>> collectorHistory = new LinkedHashMap<>();
 
    public InMemoryBtsSimulatorStateStore() { }
 
    public InMemoryBtsSimulatorStateStore(SimulatorState state)
    {
        this.state = state;
    }
 
    @Override
    public synchronized SimulatorState loadOrInitialize(SimulatorState defaultState)
    {
        if (state == null) state = defaultState;
        return state;
    }
 
    @Override
    public synchronized void save(SimulatorState state)
    {
        this.state = state;
    }
 
    @Override
    public synchronized void save(SimulatorState state, List<CollectorHistoryAppend> historyAppends)
    {
        for (CollectorHistoryAppend append : historyAppends)
        {
            ArrayList<CollectorMeasurementRecord> history = collectorHistory.computeIfAbsent(
                    append.collectorCode(), key -> new ArrayList<>());
            history.addAll(append.records());
            int overflow = history.size() - (int) MAX_HISTORY_RECORDS;
            if (overflow > 0) history.subList(0, overflow).clear();
        }
        this.state = state;
    }
 
    @Override
    public synchronized CollectorHistoryBatch loadCollectorHistoryPage(String collectorCode, long offset,
            int limit, boolean reverse)
    {
        if (offset < 0) throw new IllegalArgumentException("offset must not be negative");
        if (limit < 1 || limit > MAX_HISTORY_BATCH_SIZE)
            throw new IllegalArgumentException("limit must be between 1 and " + MAX_HISTORY_BATCH_SIZE);
        List<CollectorMeasurementRecord> history = collectorHistory.getOrDefault(collectorCode,
                new ArrayList<>());
        long total = history.size();
        int from = (int) Math.min(offset, total);
        int count = Math.min(limit, history.size() - from);
        List<CollectorMeasurementRecord> records = new ArrayList<>(count);
        if (reverse)
        {
            for (int index = history.size() - 1 - from; index >= history.size() - from - count; index--)
                records.add(history.get(index));
        }
        else
        {
            records.addAll(history.subList(from, from + count));
        }
        long nextOffset = from + records.size();
        return new CollectorHistoryBatch(offset, nextOffset, nextOffset < total, total, records);
    }
 
    @Override
    public synchronized void saveAndDeleteCollectorHistory(SimulatorState state, String collectorCode)
    {
        collectorHistory.remove(collectorCode);
        this.state = state;
    }
 
    @Override
    public synchronized List<RecentRequest> loadRecentRequests()
    {
        return recentRequests;
    }
 
    @Override
    public synchronized void saveRecentRequests(List<RecentRequest> requests)
    {
        recentRequests = List.copyOf(requests);
    }
 
    @Override
    public synchronized void reset(SimulatorState defaultState)
    {
        state = defaultState;
        recentRequests = List.of();
        collectorHistory.clear();
    }
}