org.brocalc.calculator.BaseSingleBrokerageSpec.java Source code

Java tutorial

Introduction

Here is the source code for org.brocalc.calculator.BaseSingleBrokerageSpec.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.calculator;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

import org.apache.commons.lang.Validate;
import org.joda.time.LocalDate;

import org.brocalc.calculator.BaseSingleBrokerageSpec.BrokerageScheduleParsingResult.ParsingResult;
import org.brocalc.domain.Broker;
import org.brocalc.domain.BrokerageAmount;
import org.brocalc.domain.Currency;
import org.brocalc.domain.TradeInfo;

/**
 * Encapsulates a brokerage schedule for a single broker.
 * Saves you the bother of having to call a broker "TestBroker" or something
 * like that when you're only testing trades against a single brokerage schedule.
 */
public class BaseSingleBrokerageSpec {

    public static class BrokerageScheduleParsingResult {
        private final ParsingResult parsingResult;
        private final String parsingError;

        public enum ParsingResult {
            SUCCESS, FAILURE
        }

        public BrokerageScheduleParsingResult(ParsingResult parsingResult) {
            this.parsingResult = parsingResult;
            this.parsingError = null;
        }

        public BrokerageScheduleParsingResult(ParsingResult parsingResult, String parsingError) {
            this.parsingResult = parsingResult;
            this.parsingError = parsingError;
        }

        public ParsingResult getParsingResult() {
            return parsingResult;
        }

        public String getParsingError() {
            return parsingError;
        }

    }

    private BrokerageScheduleFactory brokerageScheduleFactory = new BrokerageScheduleFactory();
    private BrokerageSchedule brokerageSchedule;

    public BrokerageResult calculateBrokerageForTrade(String currency, String usdNotional) throws ParseException {
        return calculateBrokerageForTrade(currency, usdNotional, null, null);
    }

    public BrokerageResult calculateBrokerageForTrade(String currency, String usdNotional, String tradeDate,
            String settlementDate) throws ParseException {
        NumberFormat format = NumberFormat.getNumberInstance(Locale.US);
        BigDecimal usdAmount = new BigDecimal(format.parse(usdNotional).doubleValue());
        TradeInfo trade = mock(TradeInfo.class);
        when(trade.getCurrency()).thenReturn(Currency.getInstance(currency));
        when(trade.getUsdAmount()).thenReturn(usdAmount);
        if (tradeDate != null) {
            when(trade.getTradeDate()).thenReturn(new LocalDate(tradeDate));
        }
        if (settlementDate != null) {
            when(trade.getSettlementDate()).thenReturn(new LocalDate(settlementDate));
        }
        BrokerageAmount brokerage = brokerageSchedule.calculateBrokerage(trade);
        return BrokerageResult.fromBrokerageAmount(brokerage);
    }

    public BrokerageScheduleParsingResult setBrokerageSchedule(String scheduleText) {
        Validate.notNull(scheduleText);

        Broker broker = new Broker("TestBroker");
        try {
            BrokerageSchedule brokerageSchedule = brokerageScheduleFactory.createBrokerageSchedule(broker,
                    scheduleText);
            this.brokerageSchedule = brokerageSchedule;
            return new BrokerageScheduleParsingResult(ParsingResult.SUCCESS);
        } catch (RuntimeException e) {
            this.brokerageSchedule = null;
            return new BrokerageScheduleParsingResult(ParsingResult.FAILURE, e.getMessage());
        }

    }

}