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