bloritsch 02/02/27 09:16:43
Modified: src/java/org/apache/avalon/excalibur/collections
BucketMap.java
src/test/org/apache/avalon/excalibur/collections/test
BucketMapTestCase.java
Log:
fix carriage return issues, and add implementations for the rest of the Map
interface
Revision Changes Path
1.12 +77 -5
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.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- BucketMap.java 27 Feb 2002 16:07:23 -0000 1.11
+++ BucketMap.java 27 Feb 2002 17:16:42 -0000 1.12
@@ -19,7 +19,7 @@
*
* @author <a href="[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="[EMAIL PROTECTED]">Gerhard Froehlich</a>
- * @version CVS $Revision: 1.11 $ $Date: 2002/02/27 16:07:23 $
+ * @version CVS $Revision: 1.12 $ $Date: 2002/02/27 17:16:42 $
* @since 4.0
*/
public final class BucketMap implements Map
@@ -237,7 +237,30 @@
*/
public boolean containsValue( final Object value )
{
- throw new UnsupportedOperationException("BucketMap does not support
containsValue()");
+ if ( null == value )
+ {
+ return false;
+ }
+
+ for (int i = 0; i < m_buckets.length; i++ )
+ {
+ synchronized( m_locks[i] )
+ {
+ Node n = m_buckets[i];
+
+ while ( n != null )
+ {
+ if ( n.value.equals(value) )
+ {
+ return true;
+ }
+
+ n = n.next;
+ }
+ }
+ }
+
+ return false;
}
/**
@@ -248,7 +271,23 @@
*/
public Collection values()
{
- throw new UnsupportedOperationException("BucketMap does not support
values()");
+ Set valueSet = new HashSet();
+
+ for (int i = 0; i < m_buckets.length; i++ )
+ {
+ synchronized( m_locks[i] )
+ {
+ Node n = m_buckets[i];
+
+ while( n != null )
+ {
+ valueSet.add( n.value );
+ n = n.next;
+ }
+ }
+ }
+
+ return valueSet;
}
/**
@@ -259,7 +298,23 @@
*/
public Set entrySet()
{
- throw new UnsupportedOperationException("BucketMap does not support
entrySet()");
+ Set entrySet = new HashSet();
+
+ for (int i = 0; i < m_buckets.length; i++ )
+ {
+ synchronized( m_locks[i] )
+ {
+ Node n = m_buckets[i];
+
+ while( n != null )
+ {
+ entrySet.add( n );
+ n = n.next;
+ }
+ }
+ }
+
+ return entrySet;
}
/**
@@ -340,10 +395,27 @@
}
}
- private final static class Node
+ private final static class Node implements Map.Entry
{
protected Object key;
protected Object value;
protected Node next;
+
+ public Object getKey()
+ {
+ return key;
+ }
+
+ public Object getValue()
+ {
+ return value;
+ }
+
+ public Object setValue( Object val )
+ {
+ Object retVal = value;
+ value = val;
+ return retVal;
+ }
}
}
1.3 +176 -176
jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/collections/test/BucketMapTestCase.java
Index: BucketMapTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/collections/test/BucketMapTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BucketMapTestCase.java 27 Feb 2002 16:07:23 -0000 1.2
+++ BucketMapTestCase.java 27 Feb 2002 17:16:42 -0000 1.3
@@ -1,176 +1,176 @@
-/*
- * 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.test;
-
-import junit.framework.TestCase;
-import org.apache.avalon.excalibur.collections.BucketMap;
-
-/**
- *
- * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a>
- */
-public final class BucketMapTestCase
- extends TestCase
-{
-
- private static class TestInteger
- {
- int i;
-
- public TestInteger ( int i )
- {
- this.i = i ;
- }
-
- public boolean equals (Object o)
- {
- return this == o;
- }
-
- public int hashCode ()
- {
- return i;
- }
-
- public String toString ()
- {
- return "TestInteger " + i + " @" + System.identityHashCode(this);
- }
- }
-
- private final static TestInteger VAL1 = new TestInteger( 5 );
- private final static TestInteger VAL2 = new TestInteger( 5 );
- private final static TestInteger VAL3 = new TestInteger( 5 );
- private final static TestInteger VAL4 = new TestInteger( 5 );
- private final static TestInteger VAL5 = new TestInteger( 5 );
- private final static TestInteger VAL6 = new TestInteger( 5 );
- private final static TestInteger VAL7 = new TestInteger( 5 );
-
- public BucketMapTestCase()
- {
- this("Bucket Map Test Case");
- }
-
- public BucketMapTestCase( String name )
- {
- super( name );
- }
-
-
- public void testBucket()
- {
- final BucketMap map = new BucketMap();
-
- map.put( VAL1, VAL1 );
- assertTrue( map.size() == 1 );
- assertTrue( VAL1 == map.get( VAL1 ) );
-
- map.put( VAL2, VAL2 );
- assertTrue( map.size() == 2 );
- assertTrue( VAL1 == map.get( VAL1 ) );
- assertTrue( VAL2 == map.get( VAL2 ) );
-
- map.put( VAL3, VAL3 );
- assertTrue( map.size() == 3 );
- assertTrue( VAL1 == map.get( VAL1 ) );
- assertTrue( VAL2 == map.get( VAL2 ) );
- assertTrue( VAL3 == map.get( VAL3 ) );
-
- map.put( VAL4, VAL4 );
- assertTrue( map.size() == 4 );
- assertTrue( VAL1 == map.get( VAL1 ) );
- assertTrue( VAL2 == map.get( VAL2 ) );
- assertTrue( VAL3 == map.get( VAL3 ) );
- assertTrue( VAL4 == map.get( VAL4 ) );
-
- map.put( VAL5, VAL5 );
- assertTrue( map.size() == 5 );
- assertTrue( VAL1 == map.get( VAL1 ) );
- assertTrue( VAL2 == map.get( VAL2 ) );
- assertTrue( VAL3 == map.get( VAL3 ) );
- assertTrue( VAL4 == map.get( VAL4 ) );
- assertTrue( VAL5 == map.get( VAL5 ) );
-
- map.put( VAL6, VAL6 );
- assertTrue( map.size() == 6 );
- assertTrue( VAL1 == map.get( VAL1 ) );
- assertTrue( VAL2 == map.get( VAL2 ) );
- assertTrue( VAL3 == map.get( VAL3 ) );
- assertTrue( VAL4 == map.get( VAL4 ) );
- assertTrue( VAL5 == map.get( VAL5 ) );
- assertTrue( VAL6 == map.get( VAL6 ) );
-
- map.put( VAL7, VAL7 );
- assertTrue( map.size() == 7 );
- assertTrue( VAL1 == map.get( VAL1 ) );
- assertTrue( VAL2 == map.get( VAL2 ) );
- assertTrue( VAL3 == map.get( VAL3 ) );
- assertTrue( VAL4 == map.get( VAL4 ) );
- assertTrue( VAL5 == map.get( VAL5 ) );
- assertTrue( VAL6 == map.get( VAL6 ) );
- assertTrue( VAL7 == map.get( VAL7 ) );
-
- map.remove( VAL1 );
- assertTrue( map.size() == 6 );
- assertTrue( map.get( VAL1 ) == null );
-
- map.remove( VAL7 );
- assertTrue( map.size() == 5 );
- assertTrue( map.get( VAL7 ) == null );
-
- map.remove( VAL4 );
- assertTrue( map.size() == 4 );
- assertTrue( map.get( VAL4 ) == null );
- }
-
- public void testReplace1()
- {
- final BucketMap map = new BucketMap();
-
- map.put( VAL1, VAL1 );
- map.put( VAL1, VAL2 );
- assertTrue( map.size() == 1 );
- assertTrue( map.get( VAL1 ) == VAL2 );
- }
-
- public void testReplace2()
- {
- final BucketMap map = new BucketMap();
-
- map.put( VAL1, VAL1 );
- map.put( VAL2, VAL2 );
- map.put( VAL1, VAL3 );
- assertTrue( map.size() == 2 );
- assertTrue( map.get( VAL1 ) == VAL3 );
- }
-
- public void testReplace3()
- {
- final BucketMap map = new BucketMap();
-
- map.put( VAL1, VAL1 );
- map.put( VAL2, VAL2 );
- map.put( VAL3, VAL3 );
- map.put( VAL3, VAL4 );
- assertTrue( map.size() == 3 );
- assertTrue( map.get( VAL3 ) == VAL4 );
- }
-
- public void testReplace4()
- {
- final BucketMap map = new BucketMap();
-
- map.put( VAL1, VAL1 );
- map.put( VAL2, VAL2 );
- map.put( VAL3, VAL3 );
- map.put( VAL4, VAL4 );
- map.put( VAL3, VAL5 );
- assertTrue( map.size() == 4 );
- assertTrue( map.get( VAL3 ) == VAL5 );
- }
-}
+/*
+ * 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.test;
+
+import junit.framework.TestCase;
+import org.apache.avalon.excalibur.collections.BucketMap;
+
+/**
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a>
+ */
+public final class BucketMapTestCase
+ extends TestCase
+{
+
+ private static class TestInteger
+ {
+ int i;
+
+ public TestInteger ( int i )
+ {
+ this.i = i ;
+ }
+
+ public boolean equals (Object o)
+ {
+ return this == o;
+ }
+
+ public int hashCode ()
+ {
+ return i;
+ }
+
+ public String toString ()
+ {
+ return "TestInteger " + i + " @" + System.identityHashCode(this);
+ }
+ }
+
+ private final static TestInteger VAL1 = new TestInteger( 5 );
+ private final static TestInteger VAL2 = new TestInteger( 5 );
+ private final static TestInteger VAL3 = new TestInteger( 5 );
+ private final static TestInteger VAL4 = new TestInteger( 5 );
+ private final static TestInteger VAL5 = new TestInteger( 5 );
+ private final static TestInteger VAL6 = new TestInteger( 5 );
+ private final static TestInteger VAL7 = new TestInteger( 5 );
+
+ public BucketMapTestCase()
+ {
+ this("Bucket Map Test Case");
+ }
+
+ public BucketMapTestCase( String name )
+ {
+ super( name );
+ }
+
+
+ public void testBucket()
+ {
+ final BucketMap map = new BucketMap();
+
+ map.put( VAL1, VAL1 );
+ assertTrue( map.size() == 1 );
+ assertTrue( VAL1 == map.get( VAL1 ) );
+
+ map.put( VAL2, VAL2 );
+ assertTrue( map.size() == 2 );
+ assertTrue( VAL1 == map.get( VAL1 ) );
+ assertTrue( VAL2 == map.get( VAL2 ) );
+
+ map.put( VAL3, VAL3 );
+ assertTrue( map.size() == 3 );
+ assertTrue( VAL1 == map.get( VAL1 ) );
+ assertTrue( VAL2 == map.get( VAL2 ) );
+ assertTrue( VAL3 == map.get( VAL3 ) );
+
+ map.put( VAL4, VAL4 );
+ assertTrue( map.size() == 4 );
+ assertTrue( VAL1 == map.get( VAL1 ) );
+ assertTrue( VAL2 == map.get( VAL2 ) );
+ assertTrue( VAL3 == map.get( VAL3 ) );
+ assertTrue( VAL4 == map.get( VAL4 ) );
+
+ map.put( VAL5, VAL5 );
+ assertTrue( map.size() == 5 );
+ assertTrue( VAL1 == map.get( VAL1 ) );
+ assertTrue( VAL2 == map.get( VAL2 ) );
+ assertTrue( VAL3 == map.get( VAL3 ) );
+ assertTrue( VAL4 == map.get( VAL4 ) );
+ assertTrue( VAL5 == map.get( VAL5 ) );
+
+ map.put( VAL6, VAL6 );
+ assertTrue( map.size() == 6 );
+ assertTrue( VAL1 == map.get( VAL1 ) );
+ assertTrue( VAL2 == map.get( VAL2 ) );
+ assertTrue( VAL3 == map.get( VAL3 ) );
+ assertTrue( VAL4 == map.get( VAL4 ) );
+ assertTrue( VAL5 == map.get( VAL5 ) );
+ assertTrue( VAL6 == map.get( VAL6 ) );
+
+ map.put( VAL7, VAL7 );
+ assertTrue( map.size() == 7 );
+ assertTrue( VAL1 == map.get( VAL1 ) );
+ assertTrue( VAL2 == map.get( VAL2 ) );
+ assertTrue( VAL3 == map.get( VAL3 ) );
+ assertTrue( VAL4 == map.get( VAL4 ) );
+ assertTrue( VAL5 == map.get( VAL5 ) );
+ assertTrue( VAL6 == map.get( VAL6 ) );
+ assertTrue( VAL7 == map.get( VAL7 ) );
+
+ map.remove( VAL1 );
+ assertTrue( map.size() == 6 );
+ assertTrue( map.get( VAL1 ) == null );
+
+ map.remove( VAL7 );
+ assertTrue( map.size() == 5 );
+ assertTrue( map.get( VAL7 ) == null );
+
+ map.remove( VAL4 );
+ assertTrue( map.size() == 4 );
+ assertTrue( map.get( VAL4 ) == null );
+ }
+
+ public void testReplace1()
+ {
+ final BucketMap map = new BucketMap();
+
+ map.put( VAL1, VAL1 );
+ map.put( VAL1, VAL2 );
+ assertTrue( map.size() == 1 );
+ assertTrue( map.get( VAL1 ) == VAL2 );
+ }
+
+ public void testReplace2()
+ {
+ final BucketMap map = new BucketMap();
+
+ map.put( VAL1, VAL1 );
+ map.put( VAL2, VAL2 );
+ map.put( VAL1, VAL3 );
+ assertTrue( map.size() == 2 );
+ assertTrue( map.get( VAL1 ) == VAL3 );
+ }
+
+ public void testReplace3()
+ {
+ final BucketMap map = new BucketMap();
+
+ map.put( VAL1, VAL1 );
+ map.put( VAL2, VAL2 );
+ map.put( VAL3, VAL3 );
+ map.put( VAL3, VAL4 );
+ assertTrue( map.size() == 3 );
+ assertTrue( map.get( VAL3 ) == VAL4 );
+ }
+
+ public void testReplace4()
+ {
+ final BucketMap map = new BucketMap();
+
+ map.put( VAL1, VAL1 );
+ map.put( VAL2, VAL2 );
+ map.put( VAL3, VAL3 );
+ map.put( VAL4, VAL4 );
+ map.put( VAL3, VAL5 );
+ assertTrue( map.size() == 4 );
+ assertTrue( map.get( VAL3 ) == VAL5 );
+ }
+}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>