org.brocalc.domain.ProductBrokerageNode.java Source code

Java tutorial

Introduction

Here is the source code for org.brocalc.domain.ProductBrokerageNode.java

Source

/*******************************************************************************
 * Copyright (c) 2013 51zero Ltd.
 *
 * This file is part of the Brokerage Calculation Library (brocalc).
 *
 * brocalc is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * brocalc is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even he implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with brocalc.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package org.brocalc.domain;

import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.concurrent.Immutable;

import com.google.common.collect.ImmutableMap;

@Immutable
public final class ProductBrokerageNode implements BrokerageScheduleNode {

    private final ImmutableMap<Product, BrokerageScheduleComponent> productMap;

    public ProductBrokerageNode(Map<Product, BrokerageScheduleComponent> productMap) {
        this.productMap = ImmutableMap.copyOf(productMap);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ProductNode[");
        for (Entry<Product, BrokerageScheduleComponent> entry : productMap.entrySet()) {
            builder.append("{").append(entry.getKey()).append(" -> ").append(entry.getValue()).append("}");
        }
        builder.append("]");
        return builder.toString();
    }

    @Override
    public BrokerageScheduleComponent getRelevantChild(TradeInfo tradeInfo) throws IllegalArgumentException {

        BrokerageScheduleComponent component = productMap.get(tradeInfo.getProduct());

        if (component == null) {
            throw new IllegalArgumentException("No route specified for product: " + tradeInfo.getProduct());
        }

        return component;
    }

}