| 1 | package net.sourceforge.finmodel.rules; |
| 2 | |
| 3 | import java.math.BigDecimal; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Calendar; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import org.jfin.date.Frequency; |
| 9 | import org.jfin.date.Period; |
| 10 | import org.jfin.date.ScheduleException; |
| 11 | import org.jfin.date.ScheduleGenerator; |
| 12 | import org.jfin.date.StubType; |
| 13 | |
| 14 | public class PeriodicRule extends RuleImpl implements Rule { |
| 15 | public static final long serialVersionUID = 1L; |
| 16 | private String freq; |
| 17 | private Frequency frequency; |
| 18 | private StubType stubType = StubType.SHORT_FIRST; |
| 19 | private Period period; |
| 20 | private List<Calendar> schedule; |
| 21 | |
| 22 | public PeriodicRule() { |
| 23 | super(); |
| 24 | } |
| 25 | |
| 26 | public void setFrequency(Frequency frequency) { |
| 27 | Frequency oldFreq = this.frequency; |
| 28 | this.frequency = frequency; |
| 29 | if (oldFreq != frequency) { |
| 30 | notifyScheduleChange(); |
| 31 | schedule = null; |
| 32 | } |
| 33 | |
| 34 | this.freq = frequency.getTenorDescriptor(); |
| 35 | } |
| 36 | |
| 37 | public Frequency getFrequency() { |
| 38 | return frequency; |
| 39 | } |
| 40 | |
| 41 | public void setAmount(BigDecimal amount) { |
| 42 | this.amount = amount; |
| 43 | } |
| 44 | |
| 45 | public BigDecimal getAmount() { |
| 46 | return this.amount; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param freq the freq to set |
| 51 | */ |
| 52 | protected void setFreq(String freq) { |
| 53 | this.freq = freq; |
| 54 | setFrequency( Frequency.valueOf(freq) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return the freq |
| 59 | */ |
| 60 | protected String getFreq() { |
| 61 | return freq; |
| 62 | } |
| 63 | public void setStubType(StubType stubType) { |
| 64 | StubType oldStubType = this.stubType; |
| 65 | this.stubType = stubType; |
| 66 | if (oldStubType != stubType) { |
| 67 | notifyScheduleChange(); |
| 68 | schedule = null; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public StubType getStubType() { |
| 73 | return stubType; |
| 74 | } |
| 75 | |
| 76 | public void setPeriod(Period period) { |
| 77 | this.period = period; |
| 78 | notifyScheduleChange(); |
| 79 | schedule = null; |
| 80 | } |
| 81 | |
| 82 | public Period getPeriod() { |
| 83 | return this.period; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns a list of days for which this rule can generate a Transaction. |
| 88 | */ |
| 89 | public List<Calendar> getSchedule() throws ScheduleException { |
| 90 | if (schedule == null) { |
| 91 | List<Period> periods = ScheduleGenerator.generateSchedule(period.getStartCalendar(), period.getEndCalendar(), |
| 92 | getFrequency(), getStubType()); |
| 93 | schedule = new ArrayList<Calendar>(); |
| 94 | for (Period period: periods) { |
| 95 | schedule.add(period.getStartCalendar()); |
| 96 | } |
| 97 | } |
| 98 | return schedule; |
| 99 | } |
| 100 | } |