1 43 44 package org.jfree.date; 45 46 56 public class RelativeDayOfWeekRule extends AnnualDateRule { 57 58 59 private AnnualDateRule subrule; 60 61 64 private int dayOfWeek; 65 66 67 private int relative; 68 69 72 public RelativeDayOfWeekRule() { 73 this(new DayAndMonthRule(), SerialDate.MONDAY, SerialDate.FOLLOWING); 74 } 75 76 84 public RelativeDayOfWeekRule(final AnnualDateRule subrule, 85 final int dayOfWeek, final int relative) { 86 this.subrule = subrule; 87 this.dayOfWeek = dayOfWeek; 88 this.relative = relative; 89 } 90 91 97 public AnnualDateRule getSubrule() { 98 return this.subrule; 99 } 100 101 107 public void setSubrule(final AnnualDateRule subrule) { 108 this.subrule = subrule; 109 } 110 111 116 public int getDayOfWeek() { 117 return this.dayOfWeek; 118 } 119 120 126 public void setDayOfWeek(final int dayOfWeek) { 127 this.dayOfWeek = dayOfWeek; 128 } 129 130 137 public int getRelative() { 138 return this.relative; 139 } 140 141 148 public void setRelative(final int relative) { 149 this.relative = relative; 150 } 151 152 159 public Object clone() throws CloneNotSupportedException { 160 final RelativeDayOfWeekRule duplicate 161 = (RelativeDayOfWeekRule) super.clone(); 162 duplicate.subrule = (AnnualDateRule) duplicate.getSubrule().clone(); 163 return duplicate; 164 } 165 166 174 public SerialDate getDate(final int year) { 175 176 if ((year < SerialDate.MINIMUM_YEAR_SUPPORTED) 178 || (year > SerialDate.MAXIMUM_YEAR_SUPPORTED)) { 179 throw new IllegalArgumentException ( 180 "RelativeDayOfWeekRule.getDate(): year outside valid range."); 181 } 182 183 SerialDate result = null; 185 final SerialDate base = this.subrule.getDate(year); 186 187 if (base != null) { 188 switch (this.relative) { 189 case(SerialDate.PRECEDING): 190 result = SerialDate.getPreviousDayOfWeek(this.dayOfWeek, 191 base); 192 break; 193 case(SerialDate.NEAREST): 194 result = SerialDate.getNearestDayOfWeek(this.dayOfWeek, 195 base); 196 break; 197 case(SerialDate.FOLLOWING): 198 result = SerialDate.getFollowingDayOfWeek(this.dayOfWeek, 199 base); 200 break; 201 default: 202 break; 203 } 204 } 205 return result; 206 207 } 208 209 } 210 | Popular Tags |