You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitField.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. /**
  17. * Manage operations dealing with bit-mapped fields.
  18. */
  19. @Internal(since="POI 3.15 beta 3")
  20. public class BitField
  21. {
  22. private final int _mask;
  23. private final int _shift_count;
  24. /**
  25. * Create a BitField instance
  26. *
  27. * @param mask the mask specifying which bits apply to this
  28. * BitField. Bits that are set in this mask are the
  29. * bits that this BitField operates on
  30. */
  31. public BitField(final int mask)
  32. {
  33. _mask = mask;
  34. int count = 0;
  35. int bit_pattern = mask;
  36. if (bit_pattern != 0)
  37. {
  38. while ((bit_pattern & 1) == 0)
  39. {
  40. count++;
  41. bit_pattern >>= 1;
  42. }
  43. }
  44. _shift_count = count;
  45. }
  46. /**
  47. * Obtain the value for the specified BitField, appropriately
  48. * shifted right. Many users of a BitField will want to treat the
  49. * specified bits as an int value, and will not want to be aware
  50. * that the value is stored as a BitField (and so shifted left so
  51. * many bits)
  52. *
  53. * @param holder the int data containing the bits we're interested
  54. * in
  55. *
  56. * @return the selected bits, shifted right appropriately
  57. */
  58. public int getValue(final int holder)
  59. {
  60. return getRawValue(holder) >>> _shift_count;
  61. }
  62. /**
  63. * Obtain the value for the specified BitField, appropriately
  64. * shifted right, as a short. Many users of a BitField will want
  65. * to treat the specified bits as an int value, and will not want
  66. * to be aware that the value is stored as a BitField (and so
  67. * shifted left so many bits)
  68. *
  69. * @param holder the short data containing the bits we're
  70. * interested in
  71. *
  72. * @return the selected bits, shifted right appropriately
  73. */
  74. public short getShortValue(final short holder)
  75. {
  76. return ( short ) getValue(holder);
  77. }
  78. /**
  79. * Obtain the value for the specified BitField, unshifted
  80. *
  81. * @param holder the int data containing the bits we're interested
  82. * in
  83. *
  84. * @return the selected bits
  85. */
  86. public int getRawValue(final int holder)
  87. {
  88. return (holder & _mask);
  89. }
  90. /**
  91. * Obtain the value for the specified BitField, unshifted
  92. *
  93. * @param holder the short data containing the bits we're
  94. * interested in
  95. *
  96. * @return the selected bits
  97. */
  98. public short getShortRawValue(final short holder)
  99. {
  100. return ( short ) getRawValue(holder);
  101. }
  102. /**
  103. * Is the field set or not? This is most commonly used for a
  104. * single-bit field, which is often used to represent a boolean
  105. * value; the results of using it for a multi-bit field is to
  106. * determine whether *any* of its bits are set
  107. *
  108. * @param holder the int data containing the bits we're interested in
  109. *
  110. * @return true if any of the bits are set, else false
  111. */
  112. public boolean isSet(final int holder)
  113. {
  114. return (holder & _mask) != 0;
  115. }
  116. /**
  117. * Are all of the bits set or not? This is a stricter test than
  118. * isSet, in that all of the bits in a multi-bit set must be set
  119. * for this method to return true
  120. *
  121. * @param holder the int data containing the bits we're interested in
  122. *
  123. * @return true if all of the bits are set, else false
  124. */
  125. public boolean isAllSet(final int holder)
  126. {
  127. return (holder & _mask) == _mask;
  128. }
  129. /**
  130. * Replace the bits with new values.
  131. *
  132. * @param holder the int data containing the bits we're interested in
  133. * @param value the new value for the specified bits
  134. *
  135. * @return the value of holder with the bits from the value
  136. * parameter replacing the old bits
  137. */
  138. public int setValue(final int holder, final int value)
  139. {
  140. return (holder & ~_mask) | ((value << _shift_count) & _mask);
  141. }
  142. /**
  143. * Replace the bits with new values.
  144. *
  145. * @param holder the short data containing the bits we're interested in
  146. * @param value the new value for the specified bits
  147. *
  148. * @return the value of holder with the bits from the value
  149. * parameter replacing the old bits
  150. */
  151. public short setShortValue(final short holder, final short value)
  152. {
  153. return ( short ) setValue(holder, value);
  154. }
  155. /**
  156. * Clear the bits.
  157. *
  158. * @param holder the int data containing the bits we're interested in
  159. *
  160. * @return the value of holder with the specified bits cleared
  161. * (set to 0)
  162. */
  163. public int clear(final int holder)
  164. {
  165. return holder & ~_mask;
  166. }
  167. /**
  168. * Clear the bits.
  169. *
  170. * @param holder the short data containing the bits we're
  171. * interested in
  172. *
  173. * @return the value of holder with the specified bits cleared
  174. * (set to 0)
  175. */
  176. public short clearShort(final short holder)
  177. {
  178. return ( short ) clear(holder);
  179. }
  180. /**
  181. * Clear the bits.
  182. *
  183. * @param holder the byte data containing the bits we're
  184. * interested in
  185. *
  186. * @return the value of holder with the specified bits cleared
  187. * (set to 0)
  188. */
  189. public byte clearByte(final byte holder)
  190. {
  191. return ( byte ) clear(holder);
  192. }
  193. /**
  194. * Set the bits.
  195. *
  196. * @param holder the int data containing the bits we're interested
  197. * in
  198. *
  199. * @return the value of holder with the specified bits set to 1
  200. */
  201. public int set(final int holder)
  202. {
  203. return holder | _mask;
  204. }
  205. /**
  206. * Set the bits.
  207. *
  208. * @param holder the short data containing the bits we're
  209. * interested in
  210. *
  211. * @return the value of holder with the specified bits set to 1
  212. */
  213. public short setShort(final short holder)
  214. {
  215. return ( short ) set(holder);
  216. }
  217. /**
  218. * Set the bits.
  219. *
  220. * @param holder the byte data containing the bits we're
  221. * interested in
  222. *
  223. * @return the value of holder with the specified bits set to 1
  224. */
  225. public byte setByte(final byte holder)
  226. {
  227. return ( byte ) set(holder);
  228. }
  229. /**
  230. * Set a boolean BitField
  231. *
  232. * @param holder the int data containing the bits we're interested
  233. * in
  234. * @param flag indicating whether to set or clear the bits
  235. *
  236. * @return the value of holder with the specified bits set or
  237. * cleared
  238. */
  239. public int setBoolean(final int holder, final boolean flag)
  240. {
  241. return flag ? set(holder)
  242. : clear(holder);
  243. }
  244. /**
  245. * Set a boolean BitField
  246. *
  247. * @param holder the short data containing the bits we're
  248. * interested in
  249. * @param flag indicating whether to set or clear the bits
  250. *
  251. * @return the value of holder with the specified bits set or
  252. * cleared
  253. */
  254. public short setShortBoolean(final short holder, final boolean flag)
  255. {
  256. return flag ? setShort(holder)
  257. : clearShort(holder);
  258. }
  259. /**
  260. * Set a boolean BitField
  261. *
  262. * @param holder the byte data containing the bits we're
  263. * interested in
  264. * @param flag indicating whether to set or clear the bits
  265. *
  266. * @return the value of holder with the specified bits set or
  267. * cleared
  268. */
  269. public byte setByteBoolean(final byte holder, final boolean flag)
  270. {
  271. return flag ? setByte(holder)
  272. : clearByte(holder);
  273. }
  274. public int getMask() {
  275. return _mask;
  276. }
  277. }