Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean isAvailable(int dayIndex) {
- int nDex = this.getNumberWith1InBinaryForIndex(dayIndex);
- return (availableDex & nDex) != 0;
- }
- /**
- * @param dayIndex [0..6], 0 - monday, ..., 6 - sunday
- * @param isAvailable boolean value
- */
- public void setAvailableDay(int dayIndex, boolean isAvailable) {
- boolean isAvailableInitially = this.isAvailable(dayIndex);
- if (isAvailableInitially == isAvailable) {
- return;
- }
- if (isAvailableInitially) {
- // we should turn off
- availableDex = availableDex & this.getNumberWith0InBinaryForIndex(dayIndex);
- } else {
- // we should turn on
- availableDex = availableDex | this.getNumberWith1InBinaryForIndex(dayIndex);
- }
- }
- private int getNumberWith1InBinaryForIndex(int dayIndex) {
- switch (dayIndex) {
- case 0: // mon
- return 64; // 1000000
- case 1: // tue
- return 32; // 100000
- case 2: // wed
- return 16; // 10000
- case 3: // thu
- return 8; // 1000
- case 4: // fri
- return 4; // 100
- case 5: // sat
- return 2; // 10
- case 6: // sun
- return 1; // 1
- }
- throw new IllegalArgumentException("Day index should be in [0..6] range");
- }
- private int getNumberWith0InBinaryForIndex(int dayIndex) {
- switch (dayIndex) {
- case 0: // mon
- return 63; // 0111111
- case 1: // tue
- return 95; // 1011111
- case 2: // wed
- return 111; // 1101111
- case 3: // thu
- return 119; // 1110111
- case 4: // fri
- return 123; // 1111011
- case 5: // sat
- return 125; // 1111101
- case 6: // sun
- return 126; // 1111110
- }
- throw new IllegalArgumentException("Day index should be in [0..6] range");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement