| 1 |
package ch.poole.openinghoursparser; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Locale; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
|
| 31 |
public class WeekDayRange extends Element { |
| 32 |
|
| 33 |
WeekDay startDay = null; |
| 34 |
WeekDay endDay = null; |
| 35 |
List<Nth> nths = null; |
| 36 |
int offset = 0; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public WeekDayRange() { |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public WeekDayRange(@NotNull WeekDayRange wdr) { |
| 51 |
startDay = wdr.startDay; |
| 52 |
endDay = wdr.endDay; |
| 53 |
nths = (wdr.nths != null ? Util.copyList(wdr.nths) : null); |
| 54 |
offset = wdr.offset; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public String toString() { |
| 59 |
StringBuilder b = new StringBuilder(); |
| 60 |
b.append(startDay); |
| 61 |
if (endDay != null) { |
| 62 |
b.append("-"); |
| 63 |
b.append(endDay); |
| 64 |
} else if (nths != null && !nths.isEmpty()) { |
| 65 |
b.append("["); |
| 66 |
for (Nth n : nths) { |
| 67 |
b.append(n.toString()); |
| 68 |
if (!n.equals(nths.get(nths.size() - 1))) { |
| 69 |
b.append(","); |
| 70 |
} |
| 71 |
} |
| 72 |
b.append("]"); |
| 73 |
if (offset != 0) { |
| 74 |
if (offset > 0) { |
| 75 |
b.append(" +"); |
| 76 |
} else { |
| 77 |
b.append(" -"); |
| 78 |
} |
| 79 |
b.append(String.format(Locale.US, "%d", Math.abs(offset))); |
| 80 |
b.append(" day"); |
| 81 |
if (Math.abs(offset) > 1) { |
| 82 |
b.append("s"); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
return b.toString(); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public String toDebugString() { |
| 91 |
return getClass().getSimpleName() + ":" + toString(); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public boolean equals(Object other) { |
| 96 |
if (this == other) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
if (other instanceof WeekDayRange) { |
| 100 |
WeekDayRange o = (WeekDayRange) other; |
| 101 |
return Util.equals(startDay, o.startDay) |
| 102 |
&& Util.equals(endDay, o.endDay) |
| 103 |
&& Util.equals(nths, o.nths) |
| 104 |
&& offset == o.offset; |
| 105 |
} |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public int hashCode() { |
| 111 |
int result = 1; |
| 112 |
result = 37 * result + (startDay == null ? 0 : startDay.hashCode()); |
| 113 |
result = 37 * result + (endDay == null ? 0 : endDay.hashCode()); |
| 114 |
result = 37 * result + (nths == null ? 0 : nths.hashCode()); |
| 115 |
result = 37 * result + offset; |
| 116 |
return result; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public WeekDay getStartDay() { |
| 123 |
return startDay; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public WeekDay getEndDay() { |
| 130 |
return endDay; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public List<Nth> getNths() { |
| 137 |
return nths; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public void setStartDay(WeekDay day) { |
| 146 |
this.startDay = day; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public void setStartDay(String day) { |
| 155 |
if (day == null || "".equals(day)) { |
| 156 |
this.startDay = null; |
| 157 |
return; |
| 158 |
} |
| 159 |
this.startDay = WeekDay.getValue(day); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void setEndDay(WeekDay day) { |
| 168 |
this.endDay = day; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public void setEndDay(String day) { |
| 177 |
this.endDay = WeekDay.getValue(day); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public void setNths(List<Nth> nths) { |
| 184 |
this.nths = nths; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public void add(Nth nth) { |
| 193 |
if (nths == null) { |
| 194 |
nths = new ArrayList<>(); |
| 195 |
} |
| 196 |
nths.add(nth); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public int getOffset() { |
| 203 |
return offset; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public void setOffset(int offset) { |
| 210 |
this.offset = offset; |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public WeekDayRange copy() { |
| 215 |
return new WeekDayRange(this); |
| 216 |
} |
| 217 |
} |