001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.pd;
031:
032: /**
033: * AbstractBitFlags provides access to an integer containing bit wise flags. The
034: * concrete value is provided by an assoiated PDF object along with access
035: * method to the value within the object.
036: */
037: abstract public class AbstractBitFlags {
038: public AbstractBitFlags() {
039: super ();
040: }
041:
042: /**
043: * Checks if all the bits set in the bit mask are also set in the underlying
044: * integer. A clear bit == 0 and a set bit == 1.
045: *
046: * @param bitMask
047: * a integer containing the bit mask to test
048: *
049: * @return true if all bits in the mask are set in the underlying integer
050: */
051: public boolean isSetAnd(int bitMask) {
052: return (getValue() & bitMask) == bitMask;
053: }
054:
055: /**
056: * Checks if one of the bits set in the bit mask are also set in the
057: * underlying integer. A clear bit == 0 and a set bit == 1.
058: *
059: * @param bitMask
060: * a integer containing the bit mask
061: *
062: * @return true if one of the bits in the mask are set in the underlying
063: * integer
064: */
065: public boolean isSetOr(int bitMask) {
066: return (getValue() & bitMask) != 0;
067: }
068:
069: /**
070: * This method is used to write back the changes made to the bit flags.
071: *
072: * @param newValue
073: * the whole integer containing all bit flags
074: */
075: abstract protected void setValue(int newValue);
076:
077: /**
078: * By implementing this method the subclass provides the integer which
079: * contains the bit flags.
080: *
081: * @return the integer containing the bit flags.
082: */
083: abstract protected int getValue();
084:
085: protected void clear(int bitMask) {
086: setValue(getValue() & (-1 ^ bitMask));
087: }
088:
089: /**
090: * All bits in the underlying integer masked by the bit mask are set
091: * acording to the flag. If the flag is true, the bits are set to 1
092: * otherwise the bits are set to 0.
093: *
094: * @param bitMask
095: * @param flag
096: */
097: protected void set(int bitMask, boolean flag) {
098: if (flag) {
099: signal(bitMask);
100: } else {
101: clear(bitMask);
102: }
103: }
104:
105: protected void signal(int bitMask) {
106: setValue(getValue() | bitMask);
107: }
108: }
|