
org.opentripplanner.util.BitSetUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see . */
package org.opentripplanner.util;
/**
* A compact bit set utility class. It rely on the client to store the bit set himself (either as a
* byte, short, int or long, depending on the amount of bits needed), to prevent having to store a
* reference and create another heavy object instance such as with the BitSet class.
*
* Usage is rather simple:
*
*
*
* short flags; // or int, long, byte
*
* public boolean isFooBar() {
* return BitSetUtils.get(flags, FOOBAR_INDEX);
* }
*
* public void setFooBar(boolean foobar) {
* flags = BitSetUtils.set(flags, FOOBAR_INDEX, foobar);
* }
*
*
*
* @author laurent
*
*/
public final class BitSetUtils {
public final static boolean get(byte bitset, int index) {
return (bitset & (1 << index)) != 0;
}
public final static byte set(byte bitset, int index, boolean value) {
if (value)
bitset |= (1 << index);
else
bitset &= ~(1 << index);
return bitset;
}
public final static boolean get(short bitset, int index) {
return (bitset & (1 << index)) != 0;
}
public final static short set(short bitset, int index, boolean value) {
if (value)
bitset |= (1 << index);
else
bitset &= ~(1 << index);
return bitset;
}
public final static boolean get(int bitset, int index) {
return (bitset & (1 << index)) != 0;
}
public final static int set(int bitset, int index, boolean value) {
if (value)
bitset |= (1 << index);
else
bitset &= ~(1 << index);
return bitset;
}
public final static boolean get(long bitset, int index) {
return (bitset & (1L << index)) != 0;
}
public final static long set(long bitset, int index, boolean value) {
if (value)
bitset |= (1L << index);
else
bitset &= ~(1L << index);
return bitset;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy