Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. public boolean isAvailable(int dayIndex) {
  2. int nDex = this.getNumberWith1InBinaryForIndex(dayIndex);
  3. return (availableDex & nDex) != 0;
  4. }
  5.  
  6. /**
  7. * @param dayIndex [0..6], 0 - monday, ..., 6 - sunday
  8. * @param isAvailable boolean value
  9. */
  10. public void setAvailableDay(int dayIndex, boolean isAvailable) {
  11.  
  12. boolean isAvailableInitially = this.isAvailable(dayIndex);
  13.  
  14. if (isAvailableInitially == isAvailable) {
  15. return;
  16. }
  17.  
  18. if (isAvailableInitially) {
  19. // we should turn off
  20. availableDex = availableDex & this.getNumberWith0InBinaryForIndex(dayIndex);
  21. } else {
  22. // we should turn on
  23. availableDex = availableDex | this.getNumberWith1InBinaryForIndex(dayIndex);
  24. }
  25. }
  26.  
  27. private int getNumberWith1InBinaryForIndex(int dayIndex) {
  28. switch (dayIndex) {
  29. case 0: // mon
  30. return 64; // 1000000
  31. case 1: // tue
  32. return 32; // 100000
  33. case 2: // wed
  34. return 16; // 10000
  35. case 3: // thu
  36. return 8; // 1000
  37. case 4: // fri
  38. return 4; // 100
  39. case 5: // sat
  40. return 2; // 10
  41. case 6: // sun
  42. return 1; // 1
  43. }
  44. throw new IllegalArgumentException("Day index should be in [0..6] range");
  45. }
  46.  
  47. private int getNumberWith0InBinaryForIndex(int dayIndex) {
  48. switch (dayIndex) {
  49. case 0: // mon
  50. return 63; // 0111111
  51. case 1: // tue
  52. return 95; // 1011111
  53. case 2: // wed
  54. return 111; // 1101111
  55. case 3: // thu
  56. return 119; // 1110111
  57. case 4: // fri
  58. return 123; // 1111011
  59. case 5: // sat
  60. return 125; // 1111101
  61. case 6: // sun
  62. return 126; // 1111110
  63. }
  64. throw new IllegalArgumentException("Day index should be in [0..6] range");
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement