You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by fr...@apache.org on 2002/02/16 02:32:48 UTC
cvs commit: jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections BucketMap.java
froehlich 02/02/15 17:32:48
Modified: src/java/org/apache/avalon/excalibur/collections
BucketMap.java
Log:
fixed new line format and added author.
Revision Changes Path
1.2 +1 -242 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections/BucketMap.java
Index: BucketMap.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections/BucketMap.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BucketMap.java 15 Feb 2002 20:12:10 -0000 1.1
+++ BucketMap.java 16 Feb 2002 01:32:48 -0000 1.2
@@ -1,242 +1 @@
-/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
- *
- * This software is published under the terms of the Apache Software License
- * version 1.1, a copy of which has been included with this distribution in
- * the LICENSE.txt file.
- */
-package org.apache.avalon.excalibur.collections;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Set;
-import java.util.HashSet;
-
-/**
- * A BucketMap is an efficient ThreadSafe implementation of a Map. The
- * map only supports get(), put(), and contains().
- *
- * @author <a href="bloritsch@apache.org">Berin Loritsch</a>
- * @version CVS $Revision: 1.1 $ $Date: 2002/02/15 20:12:10 $
- * @since 4.0
- */
-public final class BucketMap
-{
- private static final int DEFAULT_BUCKETS = 256;
- private final Node[] m_buckets;
- private final Object[] m_locks;
-
- /**
- */
- public BucketMap()
- {
- this( DEFAULT_BUCKETS );
- }
-
- public BucketMap( int numBuckets )
- {
- int size = ( numBuckets >= 16 ) ? numBuckets : 16;
- m_buckets = new Node[size];
- m_locks = new Object[size];
-
- for ( int i = 0; i < size; i++ )
- {
- m_locks[i] = new Object();
- }
- }
-
- private final int getHash( Object key )
- {
- final int hash = key.hashCode() % m_buckets.length;
- return (hash < 0) ? hash * -1 : hash;
- }
-
- /**
- * Add an object into the buffer.
- *
- * @throws BufferOverflowException if adding this element exceeds the
- * buffer's capacity.
- */
- public Set keySet()
- {
- Set keySet = new HashSet();
-
- for (int i = 0; i < m_buckets.length; i++ )
- {
- synchronized( m_locks[i] )
- {
- Node n = m_buckets[i];
-
- while( n != null )
- {
- keySet.add(n.key);
- n = n.next;
- }
- }
- }
-
- return keySet;
- }
-
- /**
- * Add an object into the buffer.
- *
- * @throws BufferOverflowException if adding this element exceeds the
- * buffer's capacity.
- */
- public void put( final Object key, final Object value )
- {
- if ( null == key || null == value )
- {
- return;
- }
-
- int hash = getHash( key );
-
- synchronized( m_locks[hash] )
- {
- Node n = m_buckets[hash];
-
- if ( n == null )
- {
- n = new Node();
- n.key = key;
- n.value = value;
- m_buckets[hash] = n;
- return;
- }
-
- while ( n.next != null )
- {
- if ( n.key.equals(key) )
- {
- n.value = value;
- return;
- }
-
- n = n.next;
- }
-
- Node newNode = new Node();
- newNode.key = key;
- newNode.value = value;
- n.next = newNode;
- }
- }
-
- /**
- * Add an object into the buffer.
- *
- * @throws BufferOverflowException if adding this element exceeds the
- * buffer's capacity.
- */
- public Object get( final Object key )
- {
- if ( null == key )
- {
- return null;
- }
-
- int hash = getHash( key );
-
- synchronized( m_locks[hash] )
- {
- Node n = m_buckets[hash];
-
- while ( n != null )
- {
- if ( n.key.equals(key) )
- {
- return n.value;
- }
-
- n = n.next;
- }
- }
-
- return null;
- }
-
- /**
- * Add an object into the buffer.
- *
- * @throws BufferOverflowException if adding this element exceeds the
- * buffer's capacity.
- */
- public boolean containsKey( final Object key )
- {
- if ( null == key )
- {
- return false;
- }
-
- int hash = getHash( key );
-
- synchronized( m_locks[hash] )
- {
- Node n = m_buckets[hash];
-
- while ( n != null )
- {
- if ( n.key.equals(key) )
- {
- return true;
- }
-
- n = n.next;
- }
- }
-
- return false;
- }
-
- /**
- * Removes the next object from the buffer.
- *
- * @throws BufferUnderflowException if the buffer is already empty
- */
- public Object remove( Object key )
- {
- if ( null == key )
- {
- return null;
- }
-
- int hash = getHash( key );
-
- synchronized( m_locks[hash] )
- {
- Node n = m_buckets[hash];
- Node prev = null;
-
- while ( n != null )
- {
- if ( n.key.equals( key ) )
- {
- if ( null == prev )
- {
- m_buckets[hash] = n.next;
- }
- else
- {
- prev.next = n.next;
- }
-
- return n.value;
- }
-
- prev = n;
- n = n.next;
- }
- }
-
- return null;
- }
-
- private final static class Node
- {
- protected Object key;
- protected Object value;
- protected Node next;
- }
-}
+/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.excalibur.collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
/**
* A BucketMap is an efficient ThreadSafe implementation of a Map. The
* map only supports get(), put(), and contains().
*
* @author <a href="bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="g-froehlich@gmx.de">Gerhard Froehlich</a>
* @version CVS $Revision: 1.2 $ $Date: 2002/02/16 01:32:48 $
* @since 4.0
*/
public final class BucketMap
{
private static final int DEFAULT_BUCKETS = 256;
private final Node[] m_buckets;
private final Object[] m_locks;
/**
*/
public BucketMap()
{
this( DEFAULT_BUCKETS );
}
public BucketMap( int numBuckets )
{
int size = ( numBuckets >= 16 ) ? numBuckets : 16;
m_buckets = new Node[size];
m_locks = new Object[size];
for ( int i = 0; i < size; i++ )
{
m_locks[i] = new Object();
}
}
private final int getHash( Object key )
{
final int hash = key.hashCode() % m_buckets.length;
return (hash < 0) ? hash * -1 : hash;
}
/**
* Add an object into the buffer.
*
* @throws BufferOverflowException if adding this element exceeds the
* buffer's capacity.
*/
public Set keySet()
{
Set keySet = new HashSet();
for (int i = 0; i < m_buckets.length; i++ )
{
synchronized( m_locks[i] )
{
Node n = m_buckets[i];
while( n != null )
{
keySet.add(n.key);
n = n.next;
}
}
}
return keySet;
}
/**
* Add an object into the buffer.
*
* @throws BufferOverflowException if adding this element exceeds the
* buffer's capacity.
*/
public void put( final Object key, final Object value )
{
if ( null == key || null == value )
{
return;
}
int hash = getHash( key );
synchronized( m_locks[hash] )
{
Node n = m_buckets[hash];
if ( n == null )
{
n = new Node();
n.key = key;
n.value = value;
m_buckets[hash] = n;
return;
}
while ( n.next != null )
{
if ( n.key.equals(key) )
{
n.value = value;
return;
}
n = n.next;
}
Node newNode = new Node();
newNode.key = key;
newNode.value = value;
n.next = newNode;
}
}
/**
* Add an object into the buffer.
*
* @throws BufferOverflowException if adding this element exceeds the
* buffer's capacity.
*/
public Object get( final Object key )
{
if ( null == key )
{
return null;
}
int hash = getHash( key );
synchronized( m_locks[hash] )
{
Node n = m_buckets[hash];
while ( n != null )
{
if ( n.key.equals(key) )
{
return n.value;
}
n = n.next;
}
}
return null;
}
/**
* Add an object into the buffer.
*
* @throws BufferOverflowException if adding this element exceeds the
* buffer's capacity.
*/
public boolean containsKey( final Object key )
{
if ( null == key )
{
return false;
}
int hash = getHash( key );
synchronized( m_locks[hash] )
{
Node n = m_buckets[hash];
while ( n != null )
{
if ( n.key.equals(key) )
{
return true;
}
n = n.next;
}
}
return false;
}
/**
* Removes the next object from the buffer.
*
* @throws BufferUnderflowException if the buffer is already empty
*/
public Object remove( Object key )
{
if ( null == key )
{
return null;
}
int hash = getHash( key );
synchronized( m_locks[hash] )
{
Node n = m_buckets[hash];
Node prev = null;
while ( n != null )
{
if ( n.key.equals( key ) )
{
if ( null == prev )
{
m_buckets[hash] = n.next;
}
else
{
prev.next = n.next;
}
return n.value;
}
prev = n;
n = n.next;
}
}
return null;
}
private final static class Node
{
protected Object key;
protected Object value;
protected Node next;
}
}
\ No newline at end of file
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>