View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.util;
21  
22  import java.util.BitSet;
23  
24  
25  /**
26   * <code>BitSetUtilities</code> containes usefull clases to manipulate with
27   * <code>BitSet</code>.
28   *
29   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
30   * @version $Id: BitSetUtilities.java,v 1.4 2008-04-22 12:26:29 jbobnar Exp $
31   *
32   * @since Dec 22, 2003.
33   */
34  public final class BitSetUtilities
35  {
36  	/**
37  	 * Hidden.
38  	 */
39  	private BitSetUtilities()
40  	{
41  		super();
42  	}
43  
44  	/**
45  	 * Converts <code>long</code> value to <code>BitSet</code>.
46  	 *
47  	 * @param value the long value
48  	 *
49  	 * @return the <code>BitSet</code> corresponding to the value
50  	 */
51  	public static BitSet forLong(long value)
52  	{
53  		BitSet bs = new BitSet();
54  
55  		int i = 0;
56  
57  		while (value > 0) {
58  			bs.set(i++, (value & 1) > 0);
59  			value = value >> 1;
60  		}
61  
62  		return bs;
63  	}
64  
65  	/**
66  	 * Converts <code>BitSet</code> to <code>long</code> value if possible.
67  	 *
68  	 * @param value the <code>BitSet</code> object
69  	 *
70  	 * @return long representatnion of the bit set
71  	 *
72  	 * @throws NullPointerException DOCUMENT ME!
73  	 */
74  	public static long toLong(BitSet value)
75  	{
76  		if (value == null) {
77  			throw new NullPointerException("value");
78  		}
79  
80  		long longValue = 0;
81  
82  		// see BitSet.length() javadoc to understand "bitValue.length()-1" 
83  		for (int i = Math.min(value.length() - 1, 63); i >= 0; i--) {
84  			longValue <<= 1;
85  
86  			if (value.get(i)) {
87  				longValue++;
88  			}
89  		}
90  
91  		return longValue;
92  	}
93  
94  	/**
95  	 * Returns bit pattern by ignoring all zeros in the mask. If mask is less
96  	 * than 0, return value is equal to bits parameter.
97  	 * 
98  	 * <p>
99  	 * Example: <br>
100 	 * <code> mask = XX10101 bits = XX10010 getCompactValue() = XXXXX100
101 	 * </code>
102 	 * </p>
103 	 *
104 	 * @param bits bit pattern as long value
105 	 * @param mask mask which tells, which bits in pattern are visible, if less
106 	 *        than 0, maks is ignored
107 	 *
108 	 * @return long maksed representation of the bit pattern.
109 	 */
110 	public static long getMaskedValue(long bits, long mask)
111 	{
112 		if (mask < 0) {
113 			return bits;
114 		}
115 
116 		long vi = 0;
117 		long newBits = 0;
118 
119 		while (mask > 0) {
120 			if ((mask & 1) == 1) {
121 				newBits += ((bits & 1) << vi++);
122 			}
123 
124 			mask >>= 1;
125 			bits >>= 1;
126 		}
127 
128 		return newBits;
129 	}
130 }
131 
132 /* __oOo__ */