From b78295e8da96c3e5e76befeef9582ce5273dbb74 Mon Sep 17 00:00:00 2001
From: yeg <1347747024@qq.com>
Date: 星期二, 29 九月 2026 13:10:23 +0800
Subject: [PATCH] 按代码规范检查整改实体注释、Service Javadoc 与分层边界
---
battery-device-simulator/src/main/java/com/battery/device/simulator/InMemoryBtsSimulatorStateStore.java | 155 ++++-----------------------------------------------
1 files changed, 13 insertions(+), 142 deletions(-)
diff --git a/battery-device-simulator/src/main/java/com/battery/device/simulator/InMemoryBtsSimulatorStateStore.java b/battery-device-simulator/src/main/java/com/battery/device/simulator/InMemoryBtsSimulatorStateStore.java
index 5807015..f7903f7 100644
--- a/battery-device-simulator/src/main/java/com/battery/device/simulator/InMemoryBtsSimulatorStateStore.java
+++ b/battery-device-simulator/src/main/java/com/battery/device/simulator/InMemoryBtsSimulatorStateStore.java
@@ -1,167 +1,38 @@
package com.battery.device.simulator;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
-import com.battery.device.simulator.BtsSimulatorStateStore.CollectorHistoryAppend;
-import com.battery.device.simulator.BtsSimulatorStateStore.CollectorHistoryBatch;
-import com.battery.device.simulator.BtsSimulatorStateStore.CollectorMeasurementRecord;
-import com.battery.device.simulator.BtsSimulatorStateStore.BtsHistoryRecord;
-import com.battery.device.simulator.BtsSimulatorStateStore.BtsHistoryBatch;
-import com.battery.device.simulator.BtsSimulatorStateStore.RecentRequest;
-import com.battery.device.simulator.BtsSimulatorStateStore.SimulatorState;
-import com.battery.device.simulator.BtsSimulatorStateStore.BtsPlaybackState;
/** 测试和独立协议合同使用的内存状态存储。 */
public final class InMemoryBtsSimulatorStateStore implements BtsSimulatorStateStore
{
private SimulatorState state;
private List<RecentRequest> recentRequests = List.of();
- private final Map<String, ArrayList<CollectorMeasurementRecord>> collectorHistory = new LinkedHashMap<>();
- private final Map<String, ArrayList<BtsHistoryRecord>> btsHistory = new LinkedHashMap<>();
- private final Map<String, BtsPlaybackState> btsPlayback = new LinkedHashMap<>();
public InMemoryBtsSimulatorStateStore() { }
+ public InMemoryBtsSimulatorStateStore(SimulatorState state) { this.state = state; }
- public InMemoryBtsSimulatorStateStore(SimulatorState state)
- {
- this.state = state;
- }
-
+ /** 返回已有状态;尚未保存过任何状态时以 defaults 初始化并复用同一实例。 */
@Override
- public synchronized SimulatorState loadOrInitialize(SimulatorState defaultState)
+ public synchronized SimulatorState loadOrInitialize(SimulatorState defaults)
{
- if (state == null) state = defaultState;
+ if (state == null) state = defaults;
return state;
}
+ /** 保存状态引用,不做拷贝、校验或版本迁移。 */
@Override
- public synchronized void save(SimulatorState state)
- {
- this.state = state;
- }
-
+ 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;
- }
-
+ public synchronized List<RecentRequest> loadRecentRequests() { return recentRequests; }
+ /** 保存最近请求的不可变副本,调用方后续修改原列表不影响这里。 */
@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);
- }
-
+ public synchronized void saveRecentRequests(List<RecentRequest> requests) { recentRequests = List.copyOf(requests); }
+ /** 用 defaults 覆盖主状态并清空最近请求。 */
@Override
- public synchronized void saveAndDeleteCollectorHistory(SimulatorState state, String collectorCode)
+ public synchronized void reset(SimulatorState defaults)
{
- collectorHistory.remove(collectorCode);
- this.state = state;
- }
-
- @Override
- public synchronized BtsHistoryBatch loadBtsHistoryPage(String historyKey, long offset, int limit)
- {
- List<BtsHistoryRecord> history = btsHistory.getOrDefault(historyKey, new ArrayList<>());
- int from = (int) Math.min(offset, history.size());
- int count = Math.min(limit, history.size() - from);
- List<BtsHistoryRecord> records = new ArrayList<>(history.subList(from, from + count));
- long next = from + records.size();
- return new BtsHistoryBatch(offset, next, next < history.size(), history.size(), records);
- }
-
- @Override
- public synchronized void appendBtsHistory(String historyKey, BtsHistoryRecord record)
- {
- ArrayList<BtsHistoryRecord> history = btsHistory.computeIfAbsent(historyKey, key -> new ArrayList<>());
- history.add(record);
- if (history.size() > MAX_HISTORY_RECORDS) history.remove(0);
- }
-
- @Override
- public synchronized BtsPlaybackState loadBtsPlaybackState(String historyKey)
- {
- return btsPlayback.get(historyKey);
- }
-
- @Override
- public synchronized void saveBtsPlaybackState(String historyKey, BtsPlaybackState playback)
- {
- btsPlayback.put(historyKey, playback);
- }
-
- @Override
- public synchronized void deleteBtsPlaybackState(String historyKey)
- {
- btsPlayback.remove(historyKey);
- }
-
- @Override
- public synchronized void deleteBtsHistory(String historyKey)
- {
- btsHistory.remove(historyKey);
- }
-
- @Override
- public synchronized void saveBtsPlaybackAndAppendHistory(String historyKey, BtsPlaybackState playback,
- List<BtsHistoryRecord> records)
- {
- ArrayList<BtsHistoryRecord> history = btsHistory.computeIfAbsent(historyKey, key -> new ArrayList<>());
- history.addAll(records);
- int overflow = history.size() - (int) MAX_HISTORY_RECORDS;
- if (overflow > 0) history.subList(0, overflow).clear();
- btsPlayback.put(historyKey, playback);
- }
-
- @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;
+ state = defaults;
recentRequests = List.of();
- collectorHistory.clear();
- btsHistory.clear();
- btsPlayback.clear();
}
}
--
Gitblit v1.7.1