001: /*
002: Copyright (c) 2005 Health Market Science, Inc.
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: USA
018:
019: You can contact Health Market Science at info@healthmarketscience.com
020: or at the following address:
021:
022: Health Market Science
023: 2700 Horizon Drive
024: Suite 200
025: King of Prussia, PA 19406
026: */
027:
028: package com.healthmarketscience.jackcess;
029:
030: import java.nio.ByteBuffer;
031:
032: /**
033: * Bitmask that indicates whether or not each column in a row is null. Also
034: * holds values of boolean columns.
035: * @author Tim McCune
036: */
037: public class NullMask {
038:
039: /** The actual bitmask */
040: private byte[] _mask;
041:
042: /**
043: * @param columnCount Number of columns in the row that this mask will be
044: * used for
045: */
046: public NullMask(int columnCount) {
047: // we leave everything initially marked as null so that we don't need to
048: // do anything for deleted columns (we only need to mark as non-null
049: // valid columns for which we actually have values).
050: _mask = new byte[(columnCount + 7) / 8];
051: }
052:
053: /**
054: * Read a mask in from a buffer
055: */
056: public void read(ByteBuffer buffer) {
057: buffer.get(_mask);
058: }
059:
060: /**
061: * Write a mask to a buffer
062: */
063: public void write(ByteBuffer buffer) {
064: buffer.put(_mask);
065: }
066:
067: /**
068: * @param column column to test for {@code null}
069: * @return Whether or not the value for that column is null. For boolean
070: * columns, returns the actual value of the column (where
071: * non-{@code null} == {@code true})
072: */
073: public boolean isNull(Column column) {
074: int columnNumber = column.getColumnNumber();
075: int maskIndex = columnNumber / 8;
076: // if new columns were added to the table, old null masks may not include
077: // them (meaning the field is null)
078: if (maskIndex >= _mask.length) {
079: // it's null
080: return true;
081: }
082: return (_mask[maskIndex] & (byte) (1 << (columnNumber % 8))) == 0;
083: }
084:
085: /**
086: * Indicate that the column with the given number is not {@code null} (or a
087: * boolean value is {@code true}).
088: * @param column column to be marked non-{@code null}
089: */
090: public void markNotNull(Column column) {
091: int columnNumber = column.getColumnNumber();
092: int maskIndex = columnNumber / 8;
093: _mask[maskIndex] = (byte) (_mask[maskIndex] | (byte) (1 << (columnNumber % 8)));
094: }
095:
096: /**
097: * @return Size in bytes of this mask
098: */
099: public int byteSize() {
100: return _mask.length;
101: }
102:
103: }
|