EnumIntBitmask.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.util;
  20. /**
  21. * @author HorridoJoho
  22. * @param <E> The enum type
  23. */
  24. public final class EnumIntBitmask<E extends Enum<E>> implements Cloneable
  25. {
  26. public static <E extends Enum<E>> int getAllBitmask(Class<E> enumClass)
  27. {
  28. int allBitmask = 0;
  29. E[] values = enumClass.getEnumConstants();
  30. if (values.length > 32)
  31. {
  32. throw new IllegalArgumentException("Enum too big for an integer bitmask.");
  33. }
  34. for (E value : values)
  35. {
  36. allBitmask |= 1 << value.ordinal();
  37. }
  38. return allBitmask;
  39. }
  40. private final Class<E> _enumClass;
  41. private int _bitmask;
  42. public EnumIntBitmask(Class<E> enumClass, boolean all)
  43. {
  44. _enumClass = enumClass;
  45. E[] values = _enumClass.getEnumConstants();
  46. if (values.length > 32)
  47. {
  48. throw new IllegalArgumentException("Enum too big for an integer bitmask.");
  49. }
  50. if (all)
  51. {
  52. setAll();
  53. }
  54. else
  55. {
  56. clear();
  57. }
  58. }
  59. public EnumIntBitmask(Class<E> enumClass, int bitmask)
  60. {
  61. _enumClass = enumClass;
  62. _bitmask = bitmask;
  63. }
  64. public void setAll()
  65. {
  66. set(_enumClass.getEnumConstants());
  67. }
  68. public void clear()
  69. {
  70. _bitmask = 0;
  71. }
  72. @SafeVarargs
  73. public final void set(E... many)
  74. {
  75. clear();
  76. for (E one : many)
  77. {
  78. _bitmask |= 1 << one.ordinal();
  79. }
  80. }
  81. @SafeVarargs
  82. public final void set(E first, E... more)
  83. {
  84. clear();
  85. add(first, more);
  86. }
  87. public void setBitmask(int bitmask)
  88. {
  89. _bitmask = bitmask;
  90. }
  91. @SafeVarargs
  92. public final void add(E first, E... more)
  93. {
  94. _bitmask |= 1 << first.ordinal();
  95. if (more != null)
  96. {
  97. for (E one : more)
  98. {
  99. _bitmask |= 1 << one.ordinal();
  100. }
  101. }
  102. }
  103. @SafeVarargs
  104. public final void remove(E first, E... more)
  105. {
  106. _bitmask &= ~(1 << first.ordinal());
  107. if (more != null)
  108. {
  109. for (E one : more)
  110. {
  111. _bitmask &= ~(1 << one.ordinal());
  112. }
  113. }
  114. }
  115. @SafeVarargs
  116. public final boolean has(E first, E... more)
  117. {
  118. if ((_bitmask & (1 << first.ordinal())) == 0)
  119. {
  120. return false;
  121. }
  122. for (E one : more)
  123. {
  124. if ((_bitmask & (1 << one.ordinal())) == 0)
  125. {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. @Override
  132. public EnumIntBitmask<E> clone()
  133. {
  134. return new EnumIntBitmask<>(_enumClass, _bitmask);
  135. }
  136. public int getBitmask()
  137. {
  138. return _bitmask;
  139. }
  140. }