FileDocCategorySizeDatePackage
BitFlags.javaAPI DocAzureus 3.0.3.46041Sat Mar 11 12:20:42 GMT 2006com.aelitis.azureus.core.peermanager.piecepicker.util

BitFlags

public class BitFlags extends Object implements Cloneable
author
MjrTom A fairly light-weight, versatile boolean array of bit flags with administrative fields and methods Originaly designed as a boolean array to correspond to the pieces in a torrent, for example to show which pieces are; downloading, high priority, rarest, available, or whatever. This class is subject to experimentation, although the important uses of the class must NOT be broken.

Fields Summary
public int
start
Index of first set bit
public int
end
Index of last set bit
public int
nbSet
how many bits are set
public final boolean[]
flags
The array of bit flags
Constructors Summary
public BitFlags(int count)

        start =count;
        end =0;
        nbSet =0;
		flags =new boolean[count];
	
public BitFlags(boolean[] _flags)

        start =_flags.length;
		flags	= _flags;
		for (int i=0;i<flags.length;i++){
			if ( flags[i]){
				nbSet++;
				if ( i < start ){
					start = i;
				}
				end	= i;
			}
		}
	
public BitFlags(BitFlags other)
clone constructor

        start =other.start;
        end =other.end;
        nbSet =other.nbSet;
        flags =(boolean[])other.flags.clone();
    
Methods Summary
public com.aelitis.azureus.core.peermanager.piecepicker.util.BitFlagsand(com.aelitis.azureus.core.peermanager.piecepicker.util.BitFlags other)
Experimental. Returns a new BitFlags with flags set as the logical AND of both BitFlags. The length of both must be the same.

param
other BitFlags to be ANDed with this BitFlags. Must not be null.
return
new BitFlags representing the logical AND of the two

		final BitFlags result =new BitFlags(flags.length);
		if (this.nbSet >0 &&other.nbSet >0)
		{
            // setup outer union bounds
			int i =this.start >other.start ?this.start :other.start;
			final int endI =this.end <other.end ?this.end :other.end;
            // find the first common set bit
			for (; i <=endI; i++)
			{
				if (this.flags[i] &&other.flags[i])
				{
                    result.flags[i] =true;
                    result.nbSet++;
                    result.start =i;
					break;
				}
			}
            // find any remaining common bits
			for (; i <=endI; i++)
			{
				if (this.flags[i] &&other.flags[i])
				{
                    result.flags[i] =true;
                    result.nbSet++;
                    result.end =i;
				}
			}
            if (result.end <result.start)
                result.end =result.start;
		}
		return result;
	
public voidclear()

		Arrays.fill(flags, false);
		start =flags.length;
		end =0;
		nbSet =0;
	
public java.lang.Objectclone()

        return new BitFlags(this);
    
public booleanequals(java.lang.Object o)

        if (o ==null ||!(o instanceof BitFlags))
            return false;
        final BitFlags other =(BitFlags) o;
        if (this.start !=other.start)
            return false;
        if (this.end !=other.end)
            return false;
        if (this.nbSet !=other.nbSet)
            return false;
        if (this.flags ==null &&other.flags ==null)
            return true;
        if (this.flags ==null ||other.flags ==null)
            return false;
        if (this.flags.length !=other.flags.length)
            return false;
        for (int i =0; i <this.flags.length; i++)
        {
            if (this.flags[i] ^ other.flags[i])
                return false;
        }

        return true;
    
public inthashCode()

        int result =HashCodeUtils.hashMore(0, flags);
        result =HashCodeUtils.hashMore(result, nbSet);
        result =HashCodeUtils.hashMore(result, end);
        return HashCodeUtils.hashMore(result, start);
    
public voidset(int i)
for setting a flag that is not known to be the first or last, or not

		if (!flags[i])
		{
			flags[i] =true;
			nbSet++;
			if (start >i)
				start =i;
			if (end <i)
				end =i;
		}
	
public voidsetAll()

		start =0;
		end =flags.length -1;
		Arrays.fill(flags, start, end, true);
		nbSet =flags.length;
	
public voidsetEnd(int i)
this is for setting a flag that is already known to be the last true flag

		flags[i] =true;
		nbSet++;
		end =i;
	
public voidsetOnly(int i)
clears the array then sets the given flag

        if (start <flags.length)
            Arrays.fill(flags, start, end, false);
		nbSet =1;
		start =i;
		end =i;
		flags[i] =true;
	
public voidsetStart(int i)
for setting a flag that is already known to be the first true flag

		flags[i] =true;
		nbSet++;
		start =i;
	
public intsize()
You can read flags.length instead (but please don't modify it)

return
the number of elements in this array

        return flags.length;