Return-Path: Delivered-To: apmail-jakarta-avalon-cvs-archive@apache.org Received: (qmail 51494 invoked from network); 16 Feb 2002 12:12:24 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 16 Feb 2002 12:12:24 -0000 Received: (qmail 27256 invoked by uid 97); 16 Feb 2002 12:12:25 -0000 Delivered-To: qmlist-jakarta-archive-avalon-cvs@jakarta.apache.org Received: (qmail 27203 invoked by uid 97); 16 Feb 2002 12:12:24 -0000 Mailing-List: contact avalon-cvs-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list avalon-cvs@jakarta.apache.org Received: (qmail 27185 invoked by uid 97); 16 Feb 2002 12:12:23 -0000 Date: 16 Feb 2002 12:12:21 -0000 Message-ID: <20020216121221.99322.qmail@icarus.apache.org> From: froehlich@apache.org To: jakarta-avalon-excalibur-cvs@apache.org Subject: cvs commit: jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/collections BucketMap.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N froehlich 02/02/16 04:12:21 Modified: src/java/org/apache/avalon/excalibur/collections BucketMap.java Log: added size() method. Revision Changes Path 1.4 +1 -1 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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- BucketMap.java 16 Feb 2002 01:34:28 -0000 1.3 +++ BucketMap.java 16 Feb 2002 12:12:21 -0000 1.4 @@ -1 +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 Berin Loritsch * @author Gerhard Froehlich * @version CVS $Revision: 1.3 $ $Date: 2002/02/16 01:34:28 $ * @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 +/* * 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 Berin Loritsch * @author Gerhard Froehlich * @version CVS $Revision: 1.4 $ $Date: 2002/02/16 12:12:21 $ * @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; } /** * Returns the current number of key, value pairs. * * @throws BufferOverflowException if adding this element exceeds the * buffer's capacity. */ public int size() { int cnt = 0; for (int i = 0; i < m_buckets.length; i++ ) { synchronized( m_locks[i] ) { Node n = m_buckets[i]; while( n != null ) { cnt++; n = n.next; } } } return cnt; } /** * 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: For additional commands, e-mail: