package top.baogutang.strategy.models;

import top.baogutang.strategy.commons.enums.Timeframe;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MultiSymbolMarketDataHub {
    private final Map<String, MarketDataCache> caches = new ConcurrentHashMap<String, MarketDataCache>();
    private final int maxBars;

    public MultiSymbolMarketDataHub(int maxBars) {
        this.maxBars = maxBars;
    }

    private MarketDataCache ensure(String symbol) {
        MarketDataCache c = caches.get(symbol);
        if (c == null) {
            c = new MarketDataCache(maxBars);
            MarketDataCache old = caches.putIfAbsent(symbol, c);
            if (old != null) c = old;
        }
        return c;
    }

    public void upsert(String symbol, Timeframe tf, Kline k) {
        ensure(symbol).upsert(tf, k);
    }

    public MarketDataCache raw(String symbol) {
        return ensure(symbol);
    }
}
