| 1 |
package ch.poole.openinghoursparser; |
| 2 |
|
| 3 |
import org.jetbrains.annotations.NotNull; |
| 4 |
import org.jetbrains.annotations.Nullable; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class DateRange extends Element { |
| 29 |
|
| 30 |
DateWithOffset startDate = null; |
| 31 |
DateWithOffset endDate = null; |
| 32 |
int interval = 0; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public DateRange() { |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public DateRange(@NotNull DateRange dr) { |
| 47 |
startDate = dr.startDate != null ? dr.startDate.copy() : null; |
| 48 |
endDate = dr.endDate != null ? dr.endDate.copy() : null; |
| 49 |
interval = dr.interval; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public String toString() { |
| 54 |
checkStartDate(); |
| 55 |
StringBuilder b = new StringBuilder(); |
| 56 |
b.append(startDate.toString()); |
| 57 |
|
| 58 |
if (endDate != null && !endDate.isUndefined()) { |
| 59 |
b.append("-"); |
| 60 |
b.append(endDate.toString()); |
| 61 |
} |
| 62 |
if (interval > 0) { |
| 63 |
b.append("/"); |
| 64 |
b.append(interval); |
| 65 |
} |
| 66 |
return b.toString(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private void checkStartDate() { |
| 73 |
if (startDate == null) { |
| 74 |
throw new IllegalStateException("range must have a start date"); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public String toDebugString() { |
| 80 |
checkStartDate(); |
| 81 |
StringBuilder b = new StringBuilder(); |
| 82 |
b.append(getClass().getSimpleName() + ":"); |
| 83 |
b.append(startDate.toDebugString()); |
| 84 |
|
| 85 |
if (endDate != null && !endDate.isUndefined()) { |
| 86 |
b.append("-"); |
| 87 |
b.append(endDate.toDebugString()); |
| 88 |
} |
| 89 |
if (interval > 0) { |
| 90 |
b.append("/"); |
| 91 |
b.append(interval); |
| 92 |
} |
| 93 |
return b.toString(); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public boolean equals(Object other) { |
| 98 |
if (this == other) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
if (other instanceof DateRange) { |
| 102 |
DateRange o = (DateRange) other; |
| 103 |
return Util.equals(startDate, o.startDate) && Util.equals(endDate, o.endDate) && interval == o.interval; |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public int hashCode() { |
| 110 |
int result = 1; |
| 111 |
result = 37 * result + (startDate == null ? 0 : startDate.hashCode()); |
| 112 |
result = 37 * result + (endDate == null ? 0 : endDate.hashCode()); |
| 113 |
result = 37 * result + interval; |
| 114 |
return result; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
@NotNull |
| 123 |
public DateWithOffset getStartDate() { |
| 124 |
checkStartDate(); |
| 125 |
return startDate; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
@Nullable |
| 134 |
public DateWithOffset getEndDate() { |
| 135 |
return endDate; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public int getInterval() { |
| 146 |
return interval; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public void setStartDate(@NotNull DateWithOffset startDate) { |
| 155 |
if (startDate == null) { |
| 156 |
throw new NullPointerException("startDate"); |
| 157 |
} |
| 158 |
this.startDate = startDate; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public void setEndDate(@Nullable DateWithOffset endDate) { |
| 167 |
this.endDate = endDate; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public void setInterval(int interval) { |
| 178 |
this.interval = interval; |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public DateRange copy() { |
| 183 |
return new DateRange(this); |
| 184 |
} |
| 185 |
} |