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.

NullMask.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2005 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.nio.ByteBuffer;
  24. /**
  25. * Bitmask that indicates whether or not each column in a row is null. Also
  26. * holds values of boolean columns.
  27. * @author Tim McCune
  28. */
  29. public class NullMask {
  30. /** num row columns */
  31. private final int _columnCount;
  32. /** The actual bitmask */
  33. private final byte[] _mask;
  34. /**
  35. * @param columnCount Number of columns in the row that this mask will be
  36. * used for
  37. */
  38. public NullMask(int columnCount) {
  39. _columnCount = columnCount;
  40. // we leave everything initially marked as null so that we don't need to
  41. // do anything for deleted columns (we only need to mark as non-null
  42. // valid columns for which we actually have values).
  43. _mask = new byte[(_columnCount + 7) / 8];
  44. }
  45. /**
  46. * Read a mask in from a buffer
  47. */
  48. public void read(ByteBuffer buffer) {
  49. buffer.get(_mask);
  50. }
  51. /**
  52. * Write a mask to a buffer
  53. */
  54. public void write(ByteBuffer buffer) {
  55. buffer.put(_mask);
  56. }
  57. /**
  58. * @param column column to test for {@code null}
  59. * @return Whether or not the value for that column is null. For boolean
  60. * columns, returns the actual value of the column (where
  61. * non-{@code null} == {@code true})
  62. */
  63. public boolean isNull(Column column) {
  64. int columnNumber = column.getColumnNumber();
  65. // if new columns were added to the table, old null masks may not include
  66. // them (meaning the field is null)
  67. if(columnNumber >= _columnCount) {
  68. // it's null
  69. return true;
  70. }
  71. return (_mask[byteIndex(columnNumber)] & bitMask(columnNumber)) == 0;
  72. }
  73. /**
  74. * Indicate that the column with the given number is not {@code null} (or a
  75. * boolean value is {@code true}).
  76. * @param column column to be marked non-{@code null}
  77. */
  78. public void markNotNull(Column column) {
  79. int columnNumber = column.getColumnNumber();
  80. int maskIndex = byteIndex(columnNumber);
  81. _mask[maskIndex] = (byte) (_mask[maskIndex] | bitMask(columnNumber));
  82. }
  83. /**
  84. * @return Size in bytes of this mask
  85. */
  86. public int byteSize() {
  87. return _mask.length;
  88. }
  89. private static int byteIndex(int columnNumber) {
  90. return columnNumber / 8;
  91. }
  92. private static byte bitMask(int columnNumber) {
  93. return (byte) (1 << (columnNumber % 8));
  94. }
  95. }