package com.red.circle.order.app.command.pay.app.strategy.commodity;

import com.red.circle.tool.core.exception.NotFoundStrategyException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Service;

/**
 * 商品发送策略工厂.
 *
 * @author pengliang on 2020/7/23
 */
@Service
public class FactoryForCommodityStrategy {

  private final Map<String, CommodityStrategy> strategyCurrentMap = new ConcurrentHashMap<>();

  public FactoryForCommodityStrategy(Map<String, CommodityStrategy> strategyMap) {
    strategyCurrentMap.putAll(strategyMap);
  }

  public CommodityStrategy getStrategy(String strategyName) {
    CommodityStrategy strategy = strategyCurrentMap.get(strategyName);
    if (strategy == null) {
      throw new NotFoundStrategyException("no strategy defined", strategyName);
    }
    return strategy;
  }
}
