package com.battery.device.simulator;
|
|
import java.util.List;
|
|
/** 测试和独立协议合同使用的内存状态存储。 */
|
public final class InMemoryBtsSimulatorStateStore implements BtsSimulatorStateStore
|
{
|
private SimulatorState state;
|
private List<RecentRequest> recentRequests = List.of();
|
|
public InMemoryBtsSimulatorStateStore() { }
|
public InMemoryBtsSimulatorStateStore(SimulatorState state) { this.state = state; }
|
|
/** 返回已有状态;尚未保存过任何状态时以 defaults 初始化并复用同一实例。 */
|
@Override
|
public synchronized SimulatorState loadOrInitialize(SimulatorState defaults)
|
{
|
if (state == null) state = defaults;
|
return state;
|
}
|
|
/** 保存状态引用,不做拷贝、校验或版本迁移。 */
|
@Override
|
public synchronized void save(SimulatorState state) { this.state = state; }
|
/** 返回最近请求列表;尚未保存过时为空列表。 */
|
@Override
|
public synchronized List<RecentRequest> loadRecentRequests() { return recentRequests; }
|
/** 保存最近请求的不可变副本,调用方后续修改原列表不影响这里。 */
|
@Override
|
public synchronized void saveRecentRequests(List<RecentRequest> requests) { recentRequests = List.copyOf(requests); }
|
/** 用 defaults 覆盖主状态并清空最近请求。 */
|
@Override
|
public synchronized void reset(SimulatorState defaults)
|
{
|
state = defaults;
|
recentRequests = List.of();
|
}
|
}
|