1
|
package data;
|
2
|
|
3
|
import java.time.DayOfWeek;
|
4
|
import java.time.LocalDate;
|
5
|
import java.time.Month;
|
6
|
import java.time.temporal.ChronoUnit;
|
7
|
import java.time.temporal.WeekFields;
|
8
|
import java.util.Arrays;
|
9
|
import java.util.Locale;
|
10
|
import java.util.Objects;
|
11
|
|
12
|
/**
|
13
|
*
|
14
|
* Trida uchovavajici informace a umoznujici praci s bitmapou
|
15
|
*
|
16
|
* @author Daniel Stus, Marek Sobota, Lukas Scurko, Jan Jirman
|
17
|
* @version 1.0
|
18
|
*
|
19
|
*/
|
20
|
public class Bitmap implements Comparable<Bitmap>{
|
21
|
|
22
|
//-------------- Atributy --------------/
|
23
|
|
24
|
/** Atribut - Datum zacatku obdobi */
|
25
|
private LocalDate dateFrom;
|
26
|
|
27
|
/** Atribut - Datum konce obdobi */
|
28
|
private LocalDate dateTo;
|
29
|
|
30
|
/** Atribut s cislem linky pro kterou je generovana bitmapa */
|
31
|
private String lineNumber;
|
32
|
|
33
|
/** Atribut s cislem spoje pro kterou je generovana bitmapa */
|
34
|
private String joinNumber;
|
35
|
|
36
|
/** Atribut s verzi linky */
|
37
|
private String lineVersion;
|
38
|
|
39
|
/** Atribut s casovym kodem */
|
40
|
private String timeCode;
|
41
|
|
42
|
/** List uchovavajici seznam pevnych kodu pro spoj */
|
43
|
private String hardCodes;
|
44
|
|
45
|
/** Pole uchovavajici bitmapu */
|
46
|
private int[] bitmap;
|
47
|
|
48
|
/** Aktualni dny velikonocnich svatku */
|
49
|
private LocalDate goodFriday, easterMonday;
|
50
|
|
51
|
|
52
|
|
53
|
//-------------- Konstruktor -------------/
|
54
|
|
55
|
/**
|
56
|
* Konstruktor incializuje a nastavuje důležité parametry tridy (@code Bitmap)
|
57
|
*
|
58
|
* @param dateFrom Datum zacatku obdobi
|
59
|
* @param dateTo Datum konce obdobi
|
60
|
* @param lineNumber Cislo linky
|
61
|
* @param joinNumber Cislo spoje
|
62
|
* @param timeCode Casovy kod bitmapy
|
63
|
* @param hardCodes Retezec s pevnymi kody bitmapy
|
64
|
* @param lineVersion Verze linky
|
65
|
*/
|
66
|
public Bitmap(LocalDate dateFrom, LocalDate dateTo, String lineNumber, String joinNumber, String timeCode, String hardCodes, String lineVersion) {
|
67
|
this.dateFrom = dateFrom;
|
68
|
this.dateTo = dateTo;
|
69
|
this.lineNumber = lineNumber;
|
70
|
this.joinNumber = joinNumber;
|
71
|
this.timeCode = timeCode;
|
72
|
this.hardCodes = hardCodes;
|
73
|
this.lineVersion = lineVersion;
|
74
|
this.bitmap = new int[(int) getDateRange()];
|
75
|
getEasterHolidays(dateFrom.getYear());
|
76
|
}
|
77
|
|
78
|
|
79
|
//----------------- Metody ----------------/
|
80
|
|
81
|
/**
|
82
|
* Metoda vraci pocet dni uzivatelem zvoleneho obdobi
|
83
|
*
|
84
|
* @return Pocet dni v uzivatelem zvolenem obdobi (rozmezi)
|
85
|
*/
|
86
|
public long getDateRange() {
|
87
|
LocalDate from = this.dateFrom;
|
88
|
LocalDate to = this.dateTo;
|
89
|
|
90
|
long noOfDaysBetween = ChronoUnit.DAYS.between(from, to);
|
91
|
return noOfDaysBetween+1;
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Metoda vraci pozici dne v bitmape
|
96
|
*
|
97
|
* @param date Den pro ktery pozadujeme vratit pozici
|
98
|
* @return Pozice zadaneho dne v bitmape
|
99
|
*/
|
100
|
public long getDatePosition(LocalDate date) {
|
101
|
LocalDate from = this.dateFrom;
|
102
|
|
103
|
//Re #7443
|
104
|
return ChronoUnit.DAYS.between(from, date);
|
105
|
}
|
106
|
|
107
|
|
108
|
/**
|
109
|
* Metoda nastavy pracovni dny bitmapy na hodnotu parametru {@code value}
|
110
|
*
|
111
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
112
|
*/
|
113
|
public void setWorkdays(int value) {
|
114
|
value = validateValue(value);
|
115
|
LocalDate currentDay = this.dateFrom;
|
116
|
|
117
|
for (int i = 0; i < this.bitmap.length; i++) {
|
118
|
if (currentDay.getDayOfWeek() != DayOfWeek.SATURDAY && currentDay.getDayOfWeek() != DayOfWeek.SUNDAY && !isBankHoliday(currentDay)) {
|
119
|
this.bitmap[i] = value;
|
120
|
}
|
121
|
|
122
|
currentDay = currentDay.plusDays(1);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
|
127
|
/**
|
128
|
* Metoda nastavy pracovni dny bitmapy v zvolenem obdobi na hodnotu parametru {@code value}
|
129
|
*
|
130
|
* @param from Pocatecni den obdobi
|
131
|
* @param to Konecny den obdobi
|
132
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
133
|
*/
|
134
|
public void setWorkdaysInRange(LocalDate from, LocalDate to, int value) {
|
135
|
value = validateValue(value);
|
136
|
LocalDate currentDay = this.dateFrom;
|
137
|
|
138
|
for (int i = 0; i < this.bitmap.length; i++) {
|
139
|
if (!currentDay.isBefore(from) && !currentDay.isAfter(to) && !isBankHoliday(currentDay)) {
|
140
|
if (currentDay.getDayOfWeek() != DayOfWeek.SATURDAY && currentDay.getDayOfWeek() != DayOfWeek.SUNDAY) {
|
141
|
this.bitmap[i] = value;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
currentDay = currentDay.plusDays(1);
|
146
|
}
|
147
|
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* Metoda nastavy vsechny dny (krome soboty) bitmapy na hodnotu parametru {@code value}
|
152
|
*
|
153
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
154
|
*/
|
155
|
public void setSundaysAndHolidays(int value) {
|
156
|
value = validateValue(value);
|
157
|
LocalDate currentDay = this.dateFrom;
|
158
|
|
159
|
for (int i = 0; i < this.bitmap.length; i++) {
|
160
|
if (currentDay.getDayOfWeek() != DayOfWeek.SATURDAY) {
|
161
|
this.bitmap[i] = value;
|
162
|
}
|
163
|
|
164
|
currentDay = currentDay.plusDays(1);
|
165
|
}
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Metoda nastavi vsechny dny (krome soboty) bitmapy ve zvolenem obdobi na hodnotu parametru {@code value}
|
170
|
*
|
171
|
* @param from Pocatecni den obdobi
|
172
|
* @param to Konecny den obdobi
|
173
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
174
|
*/
|
175
|
public void setSundaysAndHolidays(LocalDate from, LocalDate to, int value) {
|
176
|
value = validateValue(value);
|
177
|
LocalDate currentDay = this.dateFrom;
|
178
|
|
179
|
for (int i = 0; i < this.bitmap.length; i++) {
|
180
|
if (!currentDay.isBefore(from) && !currentDay.isAfter(to) && !isBankHoliday(currentDay)) {
|
181
|
if (currentDay.getDayOfWeek() != DayOfWeek.SATURDAY) {
|
182
|
this.bitmap[i] = value;
|
183
|
}
|
184
|
}
|
185
|
|
186
|
currentDay = currentDay.plusDays(1);
|
187
|
}
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Nastavi den v bitmape na prislusnou hodnotu predanou parametrem {@code value}
|
192
|
*
|
193
|
* @param day Den, pro ktery se ma hodnota v bitmape nastavit
|
194
|
* @param value Hodnota na kterou se ma bitmapa nastavit
|
195
|
*/
|
196
|
public void setDay(LocalDate day, int value) {
|
197
|
value = validateValue(value);
|
198
|
int dayPosition = (int) getDatePosition(day);
|
199
|
|
200
|
bitmap[dayPosition] = value;
|
201
|
}
|
202
|
|
203
|
|
204
|
/**
|
205
|
* Nastavi pouze jeden den v bitmapě na hodnotu 1 - jede
|
206
|
* vsechny ostatni dny nastavi na 0
|
207
|
*
|
208
|
* @param day Den, pro který se má hodnota v bitmapě nastavit
|
209
|
*/
|
210
|
public void setOneDayOnly(LocalDate day) {
|
211
|
fillWithZeros();
|
212
|
int dayPosition = (int) getDatePosition(day);
|
213
|
|
214
|
bitmap[dayPosition] = 1;
|
215
|
}
|
216
|
|
217
|
|
218
|
/**
|
219
|
* Nastavi dny (vsechny krome svatku) ve zvolenem obdobi na hodnotu {@code value}
|
220
|
*
|
221
|
* @param from Pocatecni den obdobi
|
222
|
* @param to Konecny den obdobi
|
223
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
224
|
*/
|
225
|
public void setDaysInRange(LocalDate from, LocalDate to, int value) {
|
226
|
value = validateValue(value);
|
227
|
LocalDate currentDay = this.dateFrom;
|
228
|
|
229
|
for (int i = 0; i < this.bitmap.length; i++) {
|
230
|
if (!currentDay.isBefore(from) && !currentDay.isAfter(to) && !isBankHoliday(currentDay)) {
|
231
|
this.bitmap[i] = value;
|
232
|
}
|
233
|
|
234
|
currentDay = currentDay.plusDays(1);
|
235
|
}
|
236
|
|
237
|
}
|
238
|
|
239
|
|
240
|
/**
|
241
|
* Nastavi vsechny dny ve zvolenem obdobi na hodnotu 0
|
242
|
*
|
243
|
* @param from Pocatecni den obdobi
|
244
|
* @param to Konecny den obdobi
|
245
|
*/
|
246
|
public void unsetDaysInRange(LocalDate from, LocalDate to) {
|
247
|
LocalDate currentDay = this.dateFrom;
|
248
|
|
249
|
for (int i = 0; i < this.bitmap.length; i++) {
|
250
|
if (!currentDay.isBefore(from) && !currentDay.isAfter(to)) {
|
251
|
this.bitmap[i] = 0;
|
252
|
}
|
253
|
|
254
|
currentDay = currentDay.plusDays(1);
|
255
|
}
|
256
|
|
257
|
}
|
258
|
|
259
|
|
260
|
/**
|
261
|
* Nastavi vsechny zvolene dny v tydny parametrem {@code dayOfWeek} na hodnotu {@code value}
|
262
|
*
|
263
|
* @param dayOfWeek Den v tydnu (po - ne) pro ktery se maji hodnoty nastavit
|
264
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
265
|
*/
|
266
|
public void setDaysInWeek(DayOfWeek dayOfWeek, int value) {
|
267
|
value = validateValue(value);
|
268
|
LocalDate currentDay = this.dateFrom;
|
269
|
|
270
|
for (int i = 0; i < this.bitmap.length; i++) {
|
271
|
if (currentDay.getDayOfWeek() == dayOfWeek) {
|
272
|
this.bitmap[i] = value;
|
273
|
}
|
274
|
|
275
|
currentDay = currentDay.plusDays(1);
|
276
|
}
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Nastavi vsechny zvolene dny v tydny parametrem {@code dayOfWeek} ve zvolenem obdobi na hodnotu {@code value}
|
281
|
*
|
282
|
* @param from Pocatecni den obdobi
|
283
|
* @param to Konecny den obdobi
|
284
|
* @param dayOfWeek Den v tydnu (po - ne) pro ktery se maji hodnoty nastavit
|
285
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
286
|
*/
|
287
|
public void setDaysInWeek(LocalDate from, LocalDate to, DayOfWeek dayOfWeek, int value) {
|
288
|
value = validateValue(value);
|
289
|
LocalDate currentDay = this.dateFrom;
|
290
|
|
291
|
for (int i = 0; i < this.bitmap.length; i++) {
|
292
|
if (!currentDay.isBefore(from) && !currentDay.isAfter(to) && !isBankHoliday(currentDay))
|
293
|
if (currentDay.getDayOfWeek() == dayOfWeek) {
|
294
|
this.bitmap[i] = value;
|
295
|
}
|
296
|
currentDay = currentDay.plusDays(1);
|
297
|
}
|
298
|
}
|
299
|
|
300
|
|
301
|
/**
|
302
|
* Nastavi vsechny sude/liche tydny v bitmape na hodnotu {@code value}
|
303
|
*
|
304
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
305
|
* @param remainder urcuje sude/liche tydny (zbytek po deleni 2); 1 -> liche tydny; 0 -> sude tydny
|
306
|
*/
|
307
|
public void setWeeks(int value, int remainder) {
|
308
|
value = validateValue(value);
|
309
|
LocalDate currentDay = this.dateFrom;
|
310
|
WeekFields weekFields = WeekFields.of(Locale.getDefault());
|
311
|
|
312
|
for (int i = 0; i < this.bitmap.length; i++) {
|
313
|
if ((currentDay.get(weekFields.weekOfWeekBasedYear())%2)==remainder) {
|
314
|
this.bitmap[i] = value;
|
315
|
}
|
316
|
|
317
|
currentDay = currentDay.plusDays(1);
|
318
|
|
319
|
}
|
320
|
}
|
321
|
|
322
|
|
323
|
/**
|
324
|
* Nastavi vsechny sude/liche tydny v bitmape ve zvolenem obdobi na hodnotu {@code value}
|
325
|
*
|
326
|
* @param from Pocatecni den obdobi
|
327
|
* @param to Konecny den obdobi
|
328
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
329
|
* @param remainder urcuje sude/liche tydny (zbytek po deleni 2); 1 -> liche tydny; 0 -> sude tydny
|
330
|
*/
|
331
|
public void setWeeksInRange(LocalDate from, LocalDate to, int value, int remainder) {
|
332
|
|
333
|
value = validateValue(value);
|
334
|
LocalDate currentDay = this.dateFrom;
|
335
|
WeekFields weekFields = WeekFields.of(Locale.getDefault());
|
336
|
|
337
|
for (int i = 0; i < this.bitmap.length; i++) {
|
338
|
if (!currentDay.isBefore(from) && !currentDay.isAfter(to)) {
|
339
|
if ((currentDay.get(weekFields.weekOfWeekBasedYear())%2)==remainder) {
|
340
|
this.bitmap[i] = value;
|
341
|
}
|
342
|
}
|
343
|
|
344
|
currentDay = currentDay.plusDays(1);
|
345
|
}
|
346
|
|
347
|
}
|
348
|
|
349
|
|
350
|
/**
|
351
|
* Nastavi vsechny dny svatku v bitmape na hodnotu {@code value}
|
352
|
*
|
353
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
354
|
*/
|
355
|
public void setHolidays(int value) {
|
356
|
value = validateValue(value);
|
357
|
LocalDate currentDay = this.dateFrom;
|
358
|
|
359
|
for (int i = 0; i < this.bitmap.length; i++) {
|
360
|
if (isBankHoliday(currentDay)) {
|
361
|
this.bitmap[i] = value;
|
362
|
}
|
363
|
|
364
|
currentDay = currentDay.plusDays(1);
|
365
|
}
|
366
|
}
|
367
|
|
368
|
|
369
|
/**
|
370
|
* Nastavy vsechny dny odpovidajici pevnym kodum ve zvolenem obdobi na hodnotu {@code value}
|
371
|
*
|
372
|
* @param from Pocatecni den obdobi
|
373
|
* @param to Konecny den obdobi
|
374
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
375
|
*/
|
376
|
public void setHardCodeDays(LocalDate from, LocalDate to, int value) {
|
377
|
|
378
|
value = validateValue(value);
|
379
|
char[] hardCodesChar = hardCodes.toCharArray();
|
380
|
|
381
|
for (int i = 0; i < hardCodesChar.length; i++) {
|
382
|
LocalDate currentDay = this.dateFrom;
|
383
|
for (int j = 0; j < this.bitmap.length; j++) {
|
384
|
if (isHardCodeDay(currentDay, hardCodesChar[i]) && !currentDay.isBefore(from) && !currentDay.isAfter(to)) {
|
385
|
this.bitmap[j] = value;
|
386
|
}
|
387
|
|
388
|
currentDay = currentDay.plusDays(1);
|
389
|
}
|
390
|
}
|
391
|
|
392
|
}
|
393
|
|
394
|
/**
|
395
|
* Nastavy vsechny dny odpovidajici pevnym kodum v sudych/lichych tydnech na hodnotu {@code value}
|
396
|
*
|
397
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
398
|
* @param remainder urcuje sude/liche tydny (zbytek po deleni 2); 1 -> liche tydny; 0 -> sude tydny
|
399
|
*/
|
400
|
public void setHardCodeWeeks(int value, int remainder) {
|
401
|
|
402
|
value = validateValue(value);
|
403
|
WeekFields weekFields = WeekFields.of(Locale.getDefault());
|
404
|
char[] hardCodesChar = hardCodes.toCharArray();
|
405
|
|
406
|
for (int i = 0; i < hardCodesChar.length; i++) {
|
407
|
LocalDate currentDay = this.dateFrom;
|
408
|
for (int j = 0; j < this.bitmap.length; j++) {
|
409
|
if ((currentDay.get(weekFields.weekOfWeekBasedYear()) % 2) == remainder && isHardCodeDay(currentDay, hardCodesChar[i])) {
|
410
|
this.bitmap[j] = value;
|
411
|
}
|
412
|
|
413
|
currentDay = currentDay.plusDays(1);
|
414
|
}
|
415
|
}
|
416
|
|
417
|
}
|
418
|
|
419
|
/**
|
420
|
* Nastavy vsechny dny odpovidajici pevnym kodum v sudych/lichych tydnech ve zvolenem obdobi na hodnotu {@code value}
|
421
|
*
|
422
|
* @param from Pocatecni den obdobi
|
423
|
* @param to Konecny den obdobi
|
424
|
* @param value hodnota na kterou se maji dny v bitmape nastavit
|
425
|
* @param remainder urcuje sude/liche tydny (zbytek po deleni 2); 1 -> liche tydny; 0 -> sude tydny
|
426
|
*/
|
427
|
public void setHardCodeWeeksInRange(LocalDate from, LocalDate to, int value, int remainder) {
|
428
|
|
429
|
value = validateValue(value);
|
430
|
WeekFields weekFields = WeekFields.of(Locale.getDefault());
|
431
|
char[] hardCodesChar = hardCodes.toCharArray();
|
432
|
|
433
|
for (int i = 0; i < hardCodesChar.length; i++) {
|
434
|
LocalDate currentDay = this.dateFrom;
|
435
|
for (int j = 0; j < this.bitmap.length; j++) {
|
436
|
if (!currentDay.isBefore(from) && !currentDay.isAfter(to)) {
|
437
|
if ((currentDay.get(weekFields.weekOfWeekBasedYear()) % 2) == remainder && isHardCodeDay(currentDay, hardCodesChar[i])) {
|
438
|
this.bitmap[j] = value;
|
439
|
}
|
440
|
}
|
441
|
|
442
|
currentDay = currentDay.plusDays(1);
|
443
|
}
|
444
|
}
|
445
|
|
446
|
}
|
447
|
|
448
|
/**
|
449
|
* Metoda overi jestli predany den je soucasti jednoho z pevnych kodu
|
450
|
*
|
451
|
* @param day den pro overeni pevnych kodu
|
452
|
* @param hardCode pevny kod pro overeni
|
453
|
* @return Vrati {@code true} pokud den spada do nektereho z pevnych kodu, v opacnem pripade vraci {@code false}
|
454
|
*/
|
455
|
public boolean isHardCodeDay(LocalDate day, char hardCode) {
|
456
|
|
457
|
switch (hardCode) {
|
458
|
case 'X': if(day.getDayOfWeek() != DayOfWeek.SATURDAY && day.getDayOfWeek() != DayOfWeek.SUNDAY && !isBankHoliday(day)) return true ; break;
|
459
|
case '+': if (day.getDayOfWeek() != DayOfWeek.SATURDAY) return true; break;
|
460
|
case '1': if (day.getDayOfWeek() == DayOfWeek.MONDAY) return true; break;
|
461
|
case '2': if (day.getDayOfWeek() == DayOfWeek.TUESDAY) return true; break;
|
462
|
case '3': if (day.getDayOfWeek() == DayOfWeek.WEDNESDAY) return true; break;
|
463
|
case '4': if (day.getDayOfWeek() == DayOfWeek.THURSDAY) return true; break;
|
464
|
case '5': if (day.getDayOfWeek() == DayOfWeek.FRIDAY) return true; break;
|
465
|
case '6': if (day.getDayOfWeek() == DayOfWeek.SATURDAY) return true; break;
|
466
|
case '7': if (day.getDayOfWeek() == DayOfWeek.SUNDAY) return true; break;
|
467
|
default: return false;
|
468
|
}
|
469
|
|
470
|
return false;
|
471
|
}
|
472
|
|
473
|
|
474
|
/**
|
475
|
* Metoda naplní (přemaže) bitmapu nulami
|
476
|
*/
|
477
|
public void fillWithZeros() {
|
478
|
Arrays.fill(this.bitmap, 0);
|
479
|
}
|
480
|
|
481
|
/**
|
482
|
* Metoda naplní bitmapu (všechny dny) jedničkami
|
483
|
*/
|
484
|
public void fillWithOnes() {
|
485
|
Arrays.fill(this.bitmap, 1);
|
486
|
}
|
487
|
|
488
|
|
489
|
/**
|
490
|
* Metoda overuje spravnost zadane hodnoty
|
491
|
* - do bitmapy lze zadat pouze 1 nebo 0
|
492
|
* - v pripade ze uzivatel zada cislo vetsi nez jedna metoda vrati jednotku
|
493
|
* - v pripade ze uzivatel zada cislo mensi nez nula metoda vrati nulu
|
494
|
*
|
495
|
* @param value Uzivatelem zadana hodnota
|
496
|
* @return Nova (spravna hodnota) ukladajici se do bitmapy
|
497
|
*/
|
498
|
public static int validateValue(int value) {
|
499
|
if (value < 0) value = 0;
|
500
|
else if (value > 0) value = 1;
|
501
|
else return value;
|
502
|
return value;
|
503
|
}
|
504
|
|
505
|
/**
|
506
|
* Metoda overi zda je zadany den statnim svatkem
|
507
|
*
|
508
|
* @param date den pro overeni statniho svatku
|
509
|
* @return {@code true} pokud je v zadany den statni svatek, {@code false} pokud neni v zadany den statni svatek
|
510
|
*/
|
511
|
public boolean isBankHoliday(LocalDate date) {
|
512
|
|
513
|
Month month = date.getMonth();
|
514
|
int dayOfMonth = date.getDayOfMonth();
|
515
|
|
516
|
if (date.getDayOfYear() == 1) getEasterHolidays(date.getYear());
|
517
|
|
518
|
// 1. ledna - Den obnovy samostatného českého státu | Nový rok
|
519
|
if(month == Month.JANUARY && dayOfMonth == 1) {
|
520
|
return true;
|
521
|
}
|
522
|
|
523
|
// Velikonoční pondělí
|
524
|
if(month == easterMonday.getMonth() && dayOfMonth == easterMonday.getDayOfMonth()){
|
525
|
return true;
|
526
|
}
|
527
|
|
528
|
// Velký pátek
|
529
|
if(month == goodFriday.getMonth() && dayOfMonth == goodFriday.getDayOfMonth()){
|
530
|
return true;
|
531
|
}
|
532
|
|
533
|
// 1. května - Svátek práce
|
534
|
if(month == Month.MAY && dayOfMonth == 1) {
|
535
|
return true;
|
536
|
}
|
537
|
|
538
|
// 8. května - Den vítězství
|
539
|
if(month == Month.MAY && dayOfMonth == 8) {
|
540
|
return true;
|
541
|
}
|
542
|
|
543
|
// 5. července - Den slovanských věrozvěstů Cyrila a Metoděje
|
544
|
if(month == Month.JULY && dayOfMonth == 5) {
|
545
|
return true;
|
546
|
}
|
547
|
|
548
|
// 6. července - Den upálení mistra Jana Husa
|
549
|
if(month == Month.JULY && dayOfMonth == 6) {
|
550
|
return true;
|
551
|
}
|
552
|
|
553
|
// 28. září – Den české státnosti
|
554
|
if(month == Month.SEPTEMBER && dayOfMonth == 28) {
|
555
|
return true;
|
556
|
}
|
557
|
|
558
|
// 28. října – Den vzniku samostatného československého státu
|
559
|
if(month == Month.OCTOBER && dayOfMonth == 28) {
|
560
|
return true;
|
561
|
}
|
562
|
|
563
|
// 17. listopadu – Den boje za svobodu a demokracii
|
564
|
if(month == Month.NOVEMBER && dayOfMonth == 17) {
|
565
|
return true;
|
566
|
}
|
567
|
|
568
|
// 24. prosince – Štědrý den
|
569
|
if(month == Month.DECEMBER && dayOfMonth == 24) {
|
570
|
return true;
|
571
|
}
|
572
|
|
573
|
// 25. prosince – 1. svátek vánoční
|
574
|
if(month == Month.DECEMBER && dayOfMonth == 25) {
|
575
|
return true;
|
576
|
}
|
577
|
|
578
|
// 26. prosince – 2. svátek vánoční
|
579
|
if(month == Month.DECEMBER && dayOfMonth == 26) {
|
580
|
return true;
|
581
|
}
|
582
|
|
583
|
// Není svátek
|
584
|
return false;
|
585
|
|
586
|
}
|
587
|
|
588
|
/**
|
589
|
* Metoda vypocita a nastavi bitmape na zaklade roku aktualni dny velikonocnich svatku
|
590
|
*
|
591
|
* @param year Rok pro vypocet velikonocnich svatku
|
592
|
*/
|
593
|
public void getEasterHolidays(int year) {
|
594
|
int a = year % 19;
|
595
|
int b = year / 100;
|
596
|
int c = year % 100;
|
597
|
int d = b / 4;
|
598
|
int e = b % 4;
|
599
|
int f = (b + 8) / 25;
|
600
|
int g = (b - f + 1) / 3;
|
601
|
int h = (19 * a + b - d - g + 15) % 30;
|
602
|
int i = c / 4;
|
603
|
int k = c % 4;
|
604
|
int L = (32 + 2 * e + 2 * i - h - k) % 7;
|
605
|
int m = (a + 11 * h + 22 * L) / 451;
|
606
|
int n = h + L - 7 * m + 114;
|
607
|
int month = n / 31;
|
608
|
int day = (n % 31) + 1;
|
609
|
|
610
|
//Velikonoční pondělí --> Velikonoční neděle + 1
|
611
|
LocalDate easterMonday = LocalDate.of(year, month, day);
|
612
|
easterMonday = easterMonday.plusDays(1);
|
613
|
//Velký pátek --> Velikonoční neděle - 2
|
614
|
LocalDate goodFriday = LocalDate.of(year, month, day);
|
615
|
goodFriday = goodFriday.minusDays(2);
|
616
|
|
617
|
this.easterMonday = easterMonday;
|
618
|
this.goodFriday = goodFriday;
|
619
|
}
|
620
|
|
621
|
/**
|
622
|
* Vrati bitmapu ve formatu retezce
|
623
|
*
|
624
|
* @return bitmapa ve formatu retezce
|
625
|
*/
|
626
|
public String getBitmapString() {
|
627
|
return Arrays.toString(this.bitmap).replaceAll("\\[|\\]|,|\\s", "");
|
628
|
}
|
629
|
|
630
|
/**
|
631
|
* Vrati pocatecni den obdobi bitmapy
|
632
|
*
|
633
|
* @return pocatecni den obdobi bitmapy
|
634
|
*/
|
635
|
public LocalDate getDateFrom() {
|
636
|
return dateFrom;
|
637
|
}
|
638
|
|
639
|
/**
|
640
|
* Nastavi pocatecni den obdobi na hodnotu {@code dateFrom}
|
641
|
*
|
642
|
* @param dateFrom pocatecni den obdobi bitmapy
|
643
|
*/
|
644
|
public void setDateFrom(LocalDate dateFrom) {
|
645
|
this.dateFrom = dateFrom;
|
646
|
}
|
647
|
|
648
|
|
649
|
/**
|
650
|
* Vrati koncovy den obdobi bitmapy
|
651
|
*
|
652
|
* @return koncovy den obdobi bitmapy
|
653
|
*/
|
654
|
public LocalDate getDateTo() {
|
655
|
return dateTo;
|
656
|
}
|
657
|
|
658
|
|
659
|
/**
|
660
|
* Nastavi koncovy den obdobi na hodnotu {@code dateFrom}
|
661
|
*
|
662
|
* @param dateTo koncovy den obdobi bitmapy
|
663
|
*/
|
664
|
public void setDateTo(LocalDate dateTo) {
|
665
|
this.dateTo = dateTo;
|
666
|
}
|
667
|
|
668
|
/**
|
669
|
* Vrati retezec s cislem linky generovane bitmapy
|
670
|
*
|
671
|
* @return retezec s cislem linky generovane bitmapy
|
672
|
*/
|
673
|
public String getLineNumber() {
|
674
|
return lineNumber;
|
675
|
}
|
676
|
|
677
|
/**
|
678
|
* Nastavi hodnotu atributu retezece s cislem linky generovane bitmapy na {@code lineNumber}
|
679
|
*
|
680
|
* @param lineNumber retezece s cislem linky pro nastaveni bitmapy
|
681
|
*/
|
682
|
public void setLineNumber(String lineNumber) {
|
683
|
this.lineNumber = lineNumber;
|
684
|
}
|
685
|
|
686
|
/**
|
687
|
* Vrati pole bitovych map
|
688
|
*
|
689
|
* @return pole bitovych map
|
690
|
*/
|
691
|
public int[] getBitmap() {
|
692
|
return bitmap;
|
693
|
}
|
694
|
|
695
|
/**
|
696
|
* Nastavi pole bitovych map na hodnotu {@code bitmap}
|
697
|
*
|
698
|
* @param bitmap pole bitovych map pro nastaveni bitmapy
|
699
|
*/
|
700
|
public void setBitmap(int[] bitmap) {
|
701
|
this.bitmap = bitmap;
|
702
|
}
|
703
|
|
704
|
/**
|
705
|
* Vrati retezec s cislem spoje generovane bitmapy
|
706
|
*
|
707
|
* @return retezec s cislem spoje generovane bitmapy
|
708
|
*/
|
709
|
public String getJoinNumber() {
|
710
|
return joinNumber;
|
711
|
}
|
712
|
|
713
|
/**
|
714
|
* Nastavi hodnotu atributu retezece s cislem spoje generovane bitmapy na {@code joinNumber}
|
715
|
*
|
716
|
* @param joinNumber retezece s cislem spoje pro nastaveni bitmapy
|
717
|
*/
|
718
|
public void setJoinNumber(String joinNumber) {
|
719
|
this.joinNumber = joinNumber;
|
720
|
}
|
721
|
|
722
|
/**
|
723
|
* Vrati retezec s cislem verze linky generovane bitmapy
|
724
|
*
|
725
|
* @return retezec s cislem verze linky generovane bitmapy
|
726
|
*/
|
727
|
public String getLineVersion() {
|
728
|
return lineVersion;
|
729
|
}
|
730
|
|
731
|
/**
|
732
|
* Nastavi hodnotu atributu retezece s verzi linky kodu generovane bitmapy na {@code lineVersion}
|
733
|
*
|
734
|
* @param lineVersion retezece s cislem verze linky pro nastaveni bitmapy
|
735
|
*/
|
736
|
public void setLineVersion(String lineVersion) {
|
737
|
this.lineVersion = lineVersion;
|
738
|
}
|
739
|
|
740
|
/**
|
741
|
* Vrati retezec s cislem ceasoveho kodu generovane bitmapy
|
742
|
*
|
743
|
* @return etezece s cislem casoveho kodu generovane bitmapy
|
744
|
*/
|
745
|
public String getTimeCode() {
|
746
|
return timeCode;
|
747
|
}
|
748
|
|
749
|
/**
|
750
|
* Nastavi hodnotu atributu retezece s cislem casoveho kodu generovane bitmapy na {@code timeCode}
|
751
|
*
|
752
|
* @param timeCode retezece s cislem casoveho kodu pro nastaveni bitmapy
|
753
|
*/
|
754
|
public void setTimeCode(String timeCode) {
|
755
|
this.timeCode = timeCode;
|
756
|
}
|
757
|
|
758
|
/**
|
759
|
* Nastavi hodnotu atributu tridy s pevnymi kody
|
760
|
*
|
761
|
* @return retezec s pevnymi kody
|
762
|
*/
|
763
|
public String getHardCodes() {
|
764
|
return hardCodes;
|
765
|
}
|
766
|
|
767
|
/**
|
768
|
* Nastavi hodnotu atributu tridy s pevnymi kody na {@code hardCodes}
|
769
|
*
|
770
|
* @param hardCodes retezec s pevnymi kody bitmapy
|
771
|
*/
|
772
|
public void setHardCodes(String hardCodes) {
|
773
|
this.hardCodes = hardCodes;
|
774
|
}
|
775
|
|
776
|
/**
|
777
|
* Vrati retezec bitmapy ve formatu [XXXXXXXXXXX]
|
778
|
*
|
779
|
* @return bitmapa jako retezec ve formatu [XXXXXXXXXXX]
|
780
|
*/
|
781
|
public String bitmapToString() { return Arrays.toString(this.bitmap); }
|
782
|
|
783
|
|
784
|
/**
|
785
|
* Vrati retezec s udajemi atributu tridy ve format pro vypis
|
786
|
* format: cisloLinkyVerzeLinky-CasovyKod-PevneKody;;Bitovamapa
|
787
|
*
|
788
|
* @return retezec s udajemi atributu tridy
|
789
|
*/
|
790
|
@Override
|
791
|
public String toString() {
|
792
|
return lineNumber+lineVersion+"-"+timeCode+"-"+hardCodes+";;"+getBitmapString();
|
793
|
}
|
794
|
|
795
|
|
796
|
/**
|
797
|
*
|
798
|
* hlavni metoda pro ozkouseni funkcnosti {@code Bitmap}
|
799
|
*-issue #7374
|
800
|
*
|
801
|
* @param args parametry programu
|
802
|
*/
|
803
|
public static void main(String[] args) {
|
804
|
|
805
|
LocalDate dateFrom = LocalDate.of(2019, 5, 1);
|
806
|
LocalDate dateTo = LocalDate.of(2019, 5, 31);
|
807
|
|
808
|
|
809
|
|
810
|
Bitmap bitmap = new Bitmap(dateFrom, dateTo, "143440","1","34", "5", "1");
|
811
|
|
812
|
|
813
|
// ------------- KONTROLNÍ VÝPISY -----------------
|
814
|
System.out.println("------------- KONTROLNÍ VÝPISY -----------------");
|
815
|
System.out.println("Linka: "+ bitmap.getLineNumber());
|
816
|
System.out.println("Datum: od: " + bitmap.getDateFrom() + " | do: " + bitmap.getDateTo());
|
817
|
System.out.println("První den je: "+bitmap.getDateFrom().getDayOfWeek());
|
818
|
System.out.println("Počet dní v rozmezí: " + bitmap.getDateRange());
|
819
|
System.out.println(bitmap.bitmapToString());
|
820
|
|
821
|
//Jede pouze v pracovní dny
|
822
|
bitmap.setWorkdays(1);
|
823
|
System.out.println("\nJede v pracovní dny:");
|
824
|
System.out.println(bitmap.bitmapToString());
|
825
|
|
826
|
bitmap.fillWithZeros();
|
827
|
|
828
|
bitmap.setSundaysAndHolidays(1);
|
829
|
System.out.println(bitmap.bitmapToString());
|
830
|
|
831
|
// Kontrola přemazání
|
832
|
System.out.println("\nKontrola přemazání:");
|
833
|
bitmap.fillWithZeros();
|
834
|
System.out.println(bitmap.bitmapToString());
|
835
|
|
836
|
//Nastavit pouze Pondělky - atribut 2
|
837
|
System.out.println("\nJede každé pondělí:");
|
838
|
bitmap.setDaysInWeek(DayOfWeek.MONDAY, 1);
|
839
|
System.out.println(bitmap.bitmapToString());
|
840
|
|
841
|
//přemazání
|
842
|
bitmap.fillWithZeros();
|
843
|
|
844
|
System.out.println("\nJede liché týdny od 1. 5. - 13. 5. :");
|
845
|
LocalDate dateTo2 = LocalDate.of(2019, 5, 13);
|
846
|
bitmap.setWeeksInRange(dateFrom, dateTo2, 1, 1);
|
847
|
System.out.println(bitmap.bitmapToString());
|
848
|
|
849
|
//Nastavit liché týdny (zbytek(remainder) 1 - liché)
|
850
|
System.out.println("\nLiché týdny:");
|
851
|
bitmap.setWeeks(1, 1);
|
852
|
System.out.println(bitmap.bitmapToString());
|
853
|
|
854
|
//přemazání
|
855
|
bitmap.fillWithZeros();
|
856
|
|
857
|
//Nastavit podle časového kódu
|
858
|
System.out.println("\nNastavení podle pevných kódů:");
|
859
|
bitmap.setHardCodeDays(dateFrom, dateTo2, 1);
|
860
|
System.out.println(bitmap.bitmapToString());
|
861
|
|
862
|
//Velikonoce pro svátky
|
863
|
System.out.println("\nVelikonoční pondělí: " + bitmap.easterMonday);
|
864
|
System.out.println("Velký pátek: " + bitmap.goodFriday);
|
865
|
|
866
|
|
867
|
|
868
|
|
869
|
|
870
|
}
|
871
|
|
872
|
|
873
|
/**
|
874
|
* Porovnava bitmapy podle jejich atributu pro serazeni podle hodnot
|
875
|
* @param o bitmapa k porovnani se stavajici
|
876
|
* @return 0 - kdyz jsou bitmapy stejne
|
877
|
* 1 - kdyz je bitmapa "vetsi"
|
878
|
* -1 - kdyz je bitmapa "mensi"
|
879
|
*/
|
880
|
@Override
|
881
|
public int compareTo(Bitmap o) {
|
882
|
|
883
|
if (this.lineNumber.equals(o.lineNumber)) {
|
884
|
if(this.lineVersion.equals(o.lineVersion)) {
|
885
|
if (this.timeCode.equals(o.timeCode)) {
|
886
|
if (this.hardCodes.equals(o.hardCodes)) {
|
887
|
if(Arrays.equals(this.bitmap, o.bitmap)) return 0;
|
888
|
else return -1;
|
889
|
}
|
890
|
else if (this.hardCodes.equals("") && !o.hardCodes.equals(""))
|
891
|
return -Integer.parseInt(o.hardCodes);
|
892
|
else if (!this.hardCodes.equals("") && !o.hardCodes.equals(""))
|
893
|
return Integer.parseInt(this.hardCodes);
|
894
|
else return Integer.parseInt(this.hardCodes) - Integer.parseInt(o.hardCodes);
|
895
|
}
|
896
|
else if (this.timeCode.equals("") && !o.timeCode.equals(""))
|
897
|
return -Integer.parseInt(o.timeCode);
|
898
|
else if (!this.timeCode.equals("") && o.timeCode.equals(""))
|
899
|
return Integer.parseInt(this.timeCode);
|
900
|
else return Integer.parseInt(this.timeCode) - Integer.parseInt(o.timeCode);
|
901
|
}
|
902
|
else return Integer.parseInt(this.lineVersion) - Integer.parseInt(o.lineVersion);
|
903
|
}
|
904
|
else return Integer.parseInt(this.lineNumber) - Integer.parseInt(o.lineNumber);
|
905
|
}
|
906
|
|
907
|
/**
|
908
|
* Porovna objekt s bitmapou a rozhodne zda se jedna o stejnou ci ne
|
909
|
* @param obj objekt k porovnani s bitmapou
|
910
|
* @return true - kdyz je cela bitmapa stejna
|
911
|
* false - v opacnem pripade
|
912
|
*/
|
913
|
@Override
|
914
|
public boolean equals(Object obj) {
|
915
|
if (this == obj) return true;
|
916
|
if (obj == null) return false;
|
917
|
if(getClass() != obj.getClass()) return false;
|
918
|
Bitmap o = (Bitmap) obj;
|
919
|
return Objects.equals(lineNumber, o.lineNumber) && Objects.equals(lineVersion, o.lineVersion) &&
|
920
|
Objects.equals(timeCode, o.timeCode) && Objects.equals(hardCodes, o.hardCodes) &&
|
921
|
Objects.equals(bitmap, o.bitmap);
|
922
|
}
|
923
|
|
924
|
|
925
|
}
|
926
|
|
927
|
|