All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.brooklyn.util.math.BitList Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 1.1.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.brooklyn.util.math;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.primitives.Bytes;

/** represents an immutable ordered collection of bits with a known length
 * 

* when converting to and from bytes and larger numbers, this representation * uses the least-significant first convention both for bits and for bytes (little endian) *

* (i.e. least significant byte is first, as is the least significant bit; * ninth element in this list is the least significant bit in the second byte, * so a list {0,0,0,0,0,0,0,0,1,0} represents 256) **/ public class BitList { private final BitSet bits; protected final int length; protected BitList(BitSet bits, int length) { assert length >= bits.length(); this.bits = bits; this.length = length; } public static BitList newInstance(BitSet bits, int length) { return new BitList(bits, length); } public int length() { return length; } public boolean get(int index) { if (index<0 || index>=length) throw new IndexOutOfBoundsException("Index "+index+" in "+this); return bits.get(index); } public static BitList newInstance(byte ...bytes) { BitSet bits = new BitSet(); for (int i=0; i < bytes.length*8; i++) if ((bytes[i/8] & (1 << (i%8))) > 0) bits.set(i); return newInstance(bits, bytes.length*8); } /** as {@link #newInstance(byte...)}, but accepting ints for convenience; * only the least significant 8 bits of the parameters are considered */ public static BitList newInstanceFromBytes(int ...bytes) { BitSet bits = new BitSet(); for (int i=0; i < bytes.length*8; i++) if ((bytes[i/8] & (1 << (i%8))) > 0) bits.set(i); return newInstance(bits, bytes.length*8); } public static BitList newInstance(List l) { BitSet bs = new BitSet(); for (int i=0; i * NB this may be different to BitSet.valueOf available since java 7 (as late as that!) * which reverses the order of the bytes */ public byte[] asBytes() { byte[] bytes = new byte[(length+7)/8]; for (int i=0; i asList() { List list = new ArrayList(); for (int i=0; ilength) b2.clear(length, b2.length()); return newInstance(b2, length); } public BitList reversed() { BitSet b = new BitSet(); for (int from=bits.length()-1, to=length-bits.length(); from>=0; from--, to++) { if (get(from)) b.set(to); } return new BitList(b, length); } public int commonPrefixLength(BitList other) { int i=0; while (i asByteList() { return Bytes.asList(asBytes()); } /** returns value of this as a byte(ignoring any too-high bits) */ public byte byteValue() { return asBigInteger().byteValue(); } /** returns value of this as an integer (ignoring any too-high bits) */ public int intValue() { return asBigInteger().intValue(); } /** returns value of this as a long (ignoring any too-high bits) */ public long longValue() { return asBigInteger().longValue(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((bits == null) ? 0 : bits.hashCode()); result = prime * result + length; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; BitList other = (BitList) obj; if (bits == null) { if (other.bits != null) return false; } else if (!bits.equals(other.bits)) return false; if (length != other.length) return false; return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i=0; i0) sb.append(":"); //for readability sb.append(get(i) ? '1' : '0'); } return sb.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy