1   /**
2    * Investor, the open-source investment library
3    *
4    * (C) Copyright 2008, by individual contributors as indicated by the @author tag.
5    *
6    * This library is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   */
21  package com.nickokiss.investor.fin.env;
22  
23  import java.math.BigDecimal;
24  
25  import com.nickokiss.investor.calc.MathCalc;
26  import com.nickokiss.investor.fin.element.StreamElement;
27  
28  /**
29   * 
30   * @author Tomasz Koscinski <tomasz.koscinski@nickokiss.com>
31   */
32  public class Env {
33  
34    private MathCalc mathCalc = new MathCalc();
35    
36    private InterestRateStrategy interestRateStrategy;
37    private CompoundingStrategy compoundingStrategy;
38    
39    public InterestRateStrategy getInterestRateStrategy() {
40      return interestRateStrategy;
41    }
42    public void setInterestRateStrategy(InterestRateStrategy interestRateStrategy) {
43      this.interestRateStrategy = interestRateStrategy;
44    }
45    public CompoundingStrategy getCompoundingStrategy() {
46      return compoundingStrategy;
47    }
48    public void setCompoundingStrategy(CompoundingStrategy compoundingStrategy) {
49      this.compoundingStrategy = compoundingStrategy;
50    }
51    public BigDecimal getValue(StreamElement streamElement, BigDecimal time) {
52      BigDecimal elementValue = streamElement.getValue();
53      BigDecimal discountFactor = getDiscountFactor(time, streamElement.getTime());
54      return elementValue.multiply(discountFactor);
55    }
56    public BigDecimal getForwardRate(BigDecimal startTime, BigDecimal endTime) {
57      return compoundingStrategy.getForwardRate(startTime, endTime, interestRateStrategy);
58    }
59  
60    public BigDecimal getDiscountFactor(BigDecimal startTime, BigDecimal endTime) {
61      return compoundingStrategy.getDiscountFactor(startTime, endTime, interestRateStrategy);
62    }
63    public BigDecimal getDurationMultiplicand(BigDecimal time) {
64      BigDecimal spotRate = compoundingStrategy.getForwardRate(mathCalc.ZERO, time, interestRateStrategy);
65      return compoundingStrategy.getDurationMultiplicand(time, spotRate);
66    }
67    public BigDecimal getForwardRate(String startTime, String endTime) {
68      return getForwardRate(new BigDecimal(startTime), new BigDecimal(endTime));
69    }
70    
71  }