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.element;
22  
23  import java.math.BigDecimal;
24  
25  import org.apache.commons.lang.builder.EqualsBuilder;
26  import org.apache.commons.lang.builder.HashCodeBuilder;
27  
28  import com.nickokiss.investor.calc.MathCalc;
29  import com.nickokiss.investor.fin.env.Env;
30  
31  /**
32   * 
33   * @author Tomasz Koscinski <tomasz.koscinski@nickokiss.com>
34   */
35  public class StreamElement implements FinElement {
36  
37    private MathCalc mathCalc = new MathCalc();
38    
39    private StreamElementType streamElementType;
40  
41    /**
42     * value of element
43     */
44    private BigDecimal value;
45  
46    /**
47     * time in years
48     */
49    private BigDecimal time;
50  
51    public StreamElement(BigDecimal value, BigDecimal time) {
52      setTime(time);
53      setValue(value);
54    }
55  
56    public StreamElement() {}
57  
58    public BigDecimal getValue() {
59      return value;
60    }
61  
62    public void setValue(BigDecimal value) {
63      this.value = value;
64      if (value.compareTo(mathCalc.ZERO) > 0) {
65        streamElementType = StreamElementType.BENEFIT;
66      } else if (value.compareTo(mathCalc.ZERO) < 0) {
67        streamElementType = StreamElementType.COST;
68      } else {
69        streamElementType = StreamElementType.NEUTRAL;
70      }
71    }
72  
73    public BigDecimal getTime() {
74      return time;
75    }
76  
77    public void setTime(BigDecimal time) {
78      this.time = time;
79    }
80  
81    public StreamElementType getType() {
82      return streamElementType;
83    }
84  
85    public int hashCode() {
86      return HashCodeBuilder.reflectionHashCode(this);
87    }
88  
89    @Override
90    public boolean equals(Object obj) {
91      return EqualsBuilder.reflectionEquals(this, obj);  
92    }
93  
94    @Override
95    public String toString() {
96      StringBuffer sb = new StringBuffer();
97      String valueStr = "null";
98      String timeStr = "null";
99      if (value != null) {
100       valueStr = value.toString();
101     }
102     if (time != null) {
103       timeStr = time.toString();
104     }
105     sb.append("(");
106     sb.append("value: ").append(valueStr);
107     sb.append(", ");
108     sb.append("time: ").append(timeStr);
109     sb.append(")");
110     return sb.toString();
111   }
112 
113   public void setTime(String value) {
114     setTime(new BigDecimal(value));
115   }
116 
117   public void setValue(String value) {
118     setValue(new BigDecimal(value));
119   }
120 
121   @Override
122   public BigDecimal getValue(Env env, BigDecimal time) {
123     return getValue().multiply(env.getDiscountFactor(time, getTime()));
124   }
125 
126 }