Guest User

Untitled

a guest
Jul 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package com.cyrillemartraire.monoids;
  2.  
  3. import static java.lang.Double.doubleToLongBits;
  4. import static java.util.Arrays.asList;
  5. import static org.junit.Assert.assertEquals;
  6.  
  7. import java.time.LocalDate;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.Collections;
  11. import java.util.Currency;
  12. import java.util.Iterator;
  13. import java.util.List;
  14.  
  15. import org.junit.Test;
  16.  
  17. public class CashflowSequenceTest {
  18.  
  19. @Test
  20. public void test() {
  21. final LocalDate expiry = LocalDate.parse("2018-07-19");
  22. final LocalDate month1 = LocalDate.parse("2018-06-19");
  23. final LocalDate month2 = LocalDate.parse("2018-05-19");
  24. final Currency ccy = Currency.getInstance("EUR");
  25. CashflowSequence reimbursement = new CashflowSequence(new Cashflow(10000, ccy, expiry));
  26. CashflowSequence interests = new CashflowSequence(new Cashflow(120, ccy, month1),
  27. new Cashflow(120, ccy, month2), new Cashflow(120, ccy, expiry));
  28.  
  29. CashflowSequence expected = new CashflowSequence(new Cashflow(120, ccy, month1), new Cashflow(120, ccy, month2),
  30. new Cashflow(120, ccy, expiry), new Cashflow(10000, ccy, expiry));
  31. assertEquals(expected, reimbursement.add(interests));
  32. }
  33.  
  34. /** A sequence of ordered cashflows with an addition operation */
  35. public static class CashflowSequence implements Iterable<Cashflow> {
  36. private final List<Cashflow> cashflows;
  37.  
  38. public final static CashflowSequence EMPTY = new CashflowSequence();
  39.  
  40. public final CashflowSequence add(CashflowSequence... sequences) {
  41. return add(Arrays.asList(sequences));
  42. }
  43.  
  44. public final CashflowSequence add(Iterable<CashflowSequence> cashFlows) {
  45. final List<Cashflow> all = new ArrayList<>(this.cashflows);
  46. for (CashflowSequence seq : cashFlows) {
  47. all.addAll(seq.cashflows);
  48. }
  49. return new CashflowSequence(all);
  50. }
  51.  
  52. public CashflowSequence(Cashflow... cashflows) {
  53. this(asList(cashflows));
  54. }
  55.  
  56. public CashflowSequence(List<Cashflow> cashflows) {
  57. this.cashflows = cashflows;
  58. Collections.sort(this.cashflows);
  59. }
  60.  
  61. /* You may also consider extending with more behavior, e.g.
  62. * // random access
  63. * boolean isEmpty()
  64. * int size()
  65. * Cashflow get(int index)
  66. *
  67. * // space vector
  68. * CashflowSequence multiply(double factor)
  69. * CashflowSequence opposite()
  70. *
  71. * // dates convenience
  72. * List<Date> allDates()
  73. * Date firstDate()
  74. * Date lastDate()
  75. * Range<Date> getDateRange()
  76. * CashflowSequence truncateWithin(Range<Date> range)
  77. */
  78.  
  79. @Override
  80. public Iterator<Cashflow> iterator() {
  81. return cashflows.iterator();
  82. }
  83.  
  84. @Override
  85. public int hashCode() {
  86. return cashflows.hashCode();
  87. }
  88.  
  89. @Override
  90. public boolean equals(Object o) {
  91. CashflowSequence other = (CashflowSequence) o;
  92. return cashflows.equals(other.cashflows);
  93. }
  94.  
  95. @Override
  96. public String toString() {
  97. return cashflows.toString();
  98. }
  99.  
  100. }
  101.  
  102. public static class Cashflow implements Comparable<Cashflow> {
  103. private final double amount;
  104. private final Currency currency;
  105. private final LocalDate date;
  106.  
  107. public Cashflow(double amount, Currency currency, LocalDate date) {
  108. this.amount = amount;
  109. this.currency = currency;
  110. this.date = date;
  111. }
  112.  
  113. public Cashflow add(Cashflow other) {
  114. if (!date.equals(other.date)) {
  115. throw new IllegalArgumentException("Can only add at same date: " + date + "<>" + other.date);
  116. }
  117. if (!currency.equals(other.currency)) {
  118. throw new IllegalArgumentException("Can only add same currencies " + currency + "<>" + other.currency);
  119. }
  120. return new Cashflow(amount + other.amount, currency, date);
  121. }
  122.  
  123. @Override
  124. public int hashCode() {
  125. return (int) (31 ^ doubleToLongBits(amount) + currency.hashCode() ^ date.hashCode());
  126. }
  127.  
  128. @Override
  129. public boolean equals(Object o) {
  130. Cashflow other = (Cashflow) o;
  131. return doubleToLongBits(amount) == doubleToLongBits(other.amount) && currency.equals(other.currency)
  132. && date.equals(other.date);
  133. }
  134.  
  135. @Override
  136. public int compareTo(Cashflow o) {
  137. if (date.equals(o.date)) {
  138. return (int) (amount - o.amount);
  139. }
  140. return date.compareTo(o.date);
  141. }
  142.  
  143. @Override
  144. public String toString() {
  145. return amount + " " + currency + " on " + date;
  146. }
  147.  
  148. }
  149.  
  150. }
Add Comment
Please, Sign In to add comment