BrightSide Workbench Full Report + Source Code
DepreciationMap.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.financials.model.asset;
19 
20 import java.util.Date;
21 import java.util.TreeMap;
22 import org.amic.util.date.DateRange;
23 
28 public class DepreciationMap extends TreeMap<Date, DepreciationEntry> {
29 
30  public double getToDepreciate(double investment, Date initialDate, Date finalDate) {
31  double toDepreciate = 0.0;
32  Date initialPeriod = null;
33  DepreciationEntry de = null;
34  for(Date finalPeriod : keySet()) {
35  if(finalPeriod.after(finalDate)) {
36  break;
37  }
38  if(de != null && initialPeriod != null) {
39  Date range[] = DateRange.getFinalRange(initialDate, finalDate, initialPeriod, finalPeriod);
40  if(range != null) {
41  toDepreciate += daysBetween(range[0], range[1]) * (investment * (de.getPercent() / 365.0 / 100.0));
42  }
43 // if(initialDate == null || initialPeriod.after(initialDate)) {
44 // toDepreciate += daysBetween(initialPeriod, finalPeriod) * (investment * (de.getPercent() / 365.0 / 100.0));
45 // } else if((initialPeriod.before(initialDate) || initialPeriod.equals(initialDate)) && finalPeriod.after(initialDate)) {
46 // toDepreciate += daysBetween(initialDate, finalPeriod) * (investment * (de.getPercent() / 365.0 / 100.0));
47 // }
48  }
49  de = get(finalPeriod);
50  initialPeriod = finalPeriod;
51  }
52  if(de != null && initialPeriod != null) {
53  Date range[] = DateRange.getFinalRange(initialDate, finalDate, initialPeriod, finalDate);
54  if(range != null) {
55  toDepreciate += daysBetween(range[0], range[1]) * (investment * (de.getPercent() / 365.0 / 100.0));
56  }
57 // if(initialDate == null || initialPeriod.after(initialDate)) {
58 // toDepreciate += daysBetween(initialPeriod, finalDate) * (investment * (de.getPercent() / 365.0 / 100.0));
59 // } else if((initialPeriod.before(initialDate) || initialPeriod.equals(initialDate)) && finalDate.after(initialDate)) {
60 // toDepreciate += daysBetween(initialDate, finalDate) * (investment * (de.getPercent() / 365.0 / 100.0));
61 // }
62  }
63  return Math.abs(toDepreciate) <= Math.abs(investment) ? toDepreciate : investment;
64  }
65 
66  static final double ONE_HOUR = 60 * 60 * 1000L;
67  public static double daysBetween(Date d1, Date d2){
68  return ( (d2.getTime() - d1.getTime() + ONE_HOUR) /
69  (ONE_HOUR * 24));
70  }
71 
72 }
double getToDepreciate(double investment, Date initialDate, Date finalDate)