directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wesmck...@apache.org
Subject svn commit: rev 6338 - incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn
Date Wed, 28 Jan 2004 21:16:04 GMT
Author: wesmckean
Date: Wed Jan 28 13:16:04 2004
New Revision: 6338

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BitString.java
Log:
initial load of BitString class

Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BitString.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BitString.java
Wed Jan 28 13:16:04 2004
@@ -0,0 +1,262 @@
+/*
+
+ ============================================================================
+                   The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of  source code must  retain the above copyright  notice,
+    this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation
+    and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+    include  the following  acknowledgment:  "This product includes  software
+    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
+    Alternately, this  acknowledgment may  appear in the software itself,  if
+    and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve" 
+    and "Apache Software Foundation"  must not be used to endorse or promote
+    products derived  from this  software without  prior written
+    permission. For written permission, please contact apache@apache.org.
+
+ 5. Products  derived from this software may not  be called "Apache", nor may
+    "Apache" appear  in their name,  without prior written permission  of the
+    Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
+ APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
+ ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
+ (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software  consists of voluntary contributions made  by many individuals
+ on  behalf of the Apache Software  Foundation. For more  information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+
+package org.apache.snickers.asn;
+
+import org.apache.commons.codec.binary.Binary;
+
+/**
+ * @author Wes McKean
+ *
+ * Encapsulates the basic functionality of a bit string
+ */
+public class BitString {
+
+    /** The bits that make up this bit string */
+    int[] bits = null;
+    
+    /** The number of bits */
+    int numbits;
+
+    private static final int[] bitflags = {
+        (1 << 0 ),
+        (1 << 1 ),
+        (1 << 2 ),
+        (1 << 3 ),
+        (1 << 4 ),
+        (1 << 5 ),
+        (1 << 6 ),
+        (1 << 7 ),
+        (1 << 8 ),
+        (1 << 9 ),
+        (1 << 10 ),
+        (1 << 11 ),
+        (1 << 12 ),
+        (1 << 13 ),
+        (1 << 14 ),
+        (1 << 15 ),
+        (1 << 16 ),
+        (1 << 17 ),
+        (1 << 18 ),
+        (1 << 19 ),
+        (1 << 20 ),
+        (1 << 21 ),
+        (1 << 22 ),
+        (1 << 23 ),
+        (1 << 24 ),
+        (1 << 25 ),
+        (1 << 26 ),
+        (1 << 27 ),
+        (1 << 28 ),
+        (1 << 29 ),
+        (1 << 30 ),
+        (1 << 31 )
+    };
+    
+	/**
+	 * The basic constructor for a BitString
+	 */
+	public BitString( int[] bits, int numbits ) {
+		super();
+        
+        this.bits = new int[ bits.length ];
+        System.arraycopy( bits, 0, this.bits, 0, bits.length );
+        this.numbits = numbits;
+	}
+
+    /**
+     * Performs a binary and on the passed in BitString
+     * 
+     * @param value the BitString to and against this one
+     * @return a new BitString of the combined values.  Please
+     *         note that the result will have the length of
+     *         the largest BitString.
+     */
+    public BitString and( BitString value ) {
+        BitString result = null;
+        
+        int min = ((bits.length < value.bits.length) ? bits.length : value.bits.length);
+        int length = ((bits.length > value.bits.length) ? bits.length : value.bits.length);
+        int[] l_bits = new int[ length ];
+        java.util.Arrays.fill( l_bits, 0 );
+        
+        for( int i = 0; i < min; i++ ) {
+            l_bits[i] = (bits[i] & value.bits[i]);
+        }
+        
+        int l_numbits = getLength() > value.getLength() ? getLength() : value.getLength();
+        result = new BitString( l_bits, l_numbits );
+        return result;
+    }
+    
+    /**
+     * Performs a binary or on the passed in BitString
+     * 
+     * @param value the BitString to or against this one
+     * @return a new BitString of the combined values.  Please
+     *         note that the result will have the length of
+     *         the largest BitString.
+     */
+    public BitString or( BitString value ) {
+        BitString result = null;
+        
+        int min = ((bits.length < value.bits.length) ? bits.length : value.bits.length);
+        int length = ((bits.length > value.bits.length) ? bits.length : value.bits.length);
+        int[] l_bits = new int[ length ];
+        java.util.Arrays.fill( l_bits, 0 );
+        
+        for( int i = 0; i < min; i++ ) {
+            l_bits[i] = (bits[i] | value.bits[i]);
+        }
+        
+        int l_numbits = getLength() > value.getLength() ? getLength() : value.getLength();
+        result = new BitString( l_bits, l_numbits );
+        return result;
+    }
+
+    /**
+     * Performs a binary xor on the passed in BitString
+     * 
+     * @param value the BitString to xor against this one
+     * @return a new BitString of the combined values.  Please
+     *         note that the result will have the length of
+     *         the largest BitString.
+     */
+    public BitString xor( BitString value ) {
+        BitString result = null;
+        
+        int min = ((bits.length < value.bits.length) ? bits.length : value.bits.length);
+        int length = ((bits.length > value.bits.length) ? bits.length : value.bits.length);
+        int[] l_bits = new int[ length ];
+        java.util.Arrays.fill( l_bits, 0 );
+        
+        for( int i = 0; i < min; i++ ) {
+            l_bits[i] = (bits[i] ^ value.bits[i]);
+        }
+        
+        int l_numbits = getLength() > value.getLength() ? getLength() : value.getLength();
+        result = new BitString( l_bits, l_numbits );
+        return result;
+    }
+    
+    /**
+     * Return the number of bits in this bit string
+     * 
+     * @return an integer count of the number of bits
+     */
+    public int getLength() {
+        return numbits;
+    }
+    
+    /**
+     * Method used to test the value of a specific bit
+     * @param i the index of the bit to test
+     * @return true if the bit is on, else false
+     */
+    public boolean isSet( int i ) {
+        boolean result = false;
+        
+        // Calculate the byte offset
+        if( i >= numbits ) {
+            throw new IndexOutOfBoundsException();
+        }
+        
+        // Calculate the offset to get the actual byte
+        int index = i / 32;
+        int bit = i % 32;
+        
+        int aByte = (int)bits[index];
+        result = ((aByte & bitflags[bit]) != 0 );
+        
+        return result;
+    }
+    
+	/* (non-Javadoc)
+	 * @see java.lang.Object#equals(java.lang.Object)
+	 */
+	public boolean equals(Object obj) {
+        boolean result = false;
+        
+        if( obj instanceof BitString ) {
+            if( obj == this ) {
+                result = true;
+            }
+            else {
+                BitString bObj = (BitString)obj;
+                
+                if( bObj.getLength() == getLength() ) {
+                    result = true;
+                    for( int i = 0; result && i < getLength(); i++ ) {
+                        result = (isSet( i ) == bObj.isSet(i));
+                    }
+                }
+            }
+        }
+        return result;
+	}
+
+	/* (non-Javadoc)
+	 * @see java.lang.Object#toString()
+	 */
+	public String toString() {
+		StringBuffer sb = new StringBuffer();
+        
+        for( int i = numbits-1; i >= 0; i-- ) {
+            if( isSet(i) ) {
+                sb.append("1");
+            }
+            else {
+                sb.append("0");
+            }
+        }
+        
+        return sb.toString();
+	}
+}

Mime
View raw message