Added MultiMap and MultiHashMap implementation submitted by Chris Berry together with a JUnit test case
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130502 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
40bdd8fe80
commit
71a401b27b
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* 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 file.
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
/** see MultiMap for details of an important semantic difference
|
||||
* between this and a typical HashMap
|
||||
*
|
||||
* @author Christopher Berry
|
||||
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
|
||||
*/
|
||||
public class MultiHashMap extends HashMap implements MultiMap
|
||||
{
|
||||
//----------------- Data
|
||||
private static int sCount = 0;
|
||||
private String mName = null;
|
||||
|
||||
public MultiHashMap()
|
||||
{
|
||||
super();
|
||||
setName();
|
||||
}
|
||||
|
||||
public MultiHashMap( int initialCapacity )
|
||||
{
|
||||
super( initialCapacity );
|
||||
setName();
|
||||
}
|
||||
|
||||
public MultiHashMap(int initialCapacity, float loadFactor )
|
||||
{
|
||||
super( initialCapacity, loadFactor);
|
||||
setName();
|
||||
}
|
||||
|
||||
public MultiHashMap( Map mapToCopy )
|
||||
{
|
||||
super( mapToCopy );
|
||||
}
|
||||
|
||||
private void setName()
|
||||
{
|
||||
sCount++;
|
||||
mName = "MultiMap-" + sCount;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{ return mName; }
|
||||
|
||||
public Object put( Object key, Object value )
|
||||
{
|
||||
// NOTE:: put might be called during deserialization !!!!!!
|
||||
// so we must provide a hook to handle this case
|
||||
// This means that we cannot make MultiMaps of ArrayLists !!!
|
||||
|
||||
if ( value instanceof ArrayList ) {
|
||||
return ( super.put( key, value ) );
|
||||
}
|
||||
|
||||
ArrayList keyList = (ArrayList)(super.get( key ));
|
||||
if ( keyList == null ) {
|
||||
keyList = new ArrayList(10);
|
||||
|
||||
super.put( key, keyList );
|
||||
}
|
||||
|
||||
boolean results = keyList.add( value );
|
||||
|
||||
return ( results ? value : null );
|
||||
}
|
||||
|
||||
public boolean containsValue( Object value )
|
||||
{
|
||||
Set pairs = super.entrySet();
|
||||
|
||||
if ( pairs == null )
|
||||
return false;
|
||||
|
||||
Iterator pairsIterator = pairs.iterator();
|
||||
while ( pairsIterator.hasNext() ) {
|
||||
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
|
||||
ArrayList list = (ArrayList)(keyValuePair.getValue());
|
||||
if( list.contains( value ) )
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object remove( Object key, Object item )
|
||||
{
|
||||
ArrayList valuesForKey = (ArrayList) super.get( key );
|
||||
|
||||
if ( valuesForKey == null )
|
||||
return null;
|
||||
|
||||
valuesForKey.remove( item );
|
||||
return item;
|
||||
}
|
||||
|
||||
public void clear()
|
||||
{
|
||||
Set pairs = super.entrySet();
|
||||
Iterator pairsIterator = pairs.iterator();
|
||||
while ( pairsIterator.hasNext() ) {
|
||||
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
|
||||
ArrayList list = (ArrayList)(keyValuePair.getValue());
|
||||
list.clear();
|
||||
}
|
||||
super.clear();
|
||||
}
|
||||
|
||||
public void putAll( Map mapToPut )
|
||||
{
|
||||
super.putAll( mapToPut );
|
||||
}
|
||||
|
||||
public Collection values()
|
||||
{
|
||||
ArrayList returnList = new ArrayList( super.size() );
|
||||
|
||||
Set pairs = super.entrySet();
|
||||
Iterator pairsIterator = pairs.iterator();
|
||||
while ( pairsIterator.hasNext() ) {
|
||||
Map.Entry keyValuePair = (Map.Entry)(pairsIterator.next());
|
||||
ArrayList list = (ArrayList)(keyValuePair.getValue());
|
||||
|
||||
Object[] values = list.toArray();
|
||||
for( int ii=0; ii < values.length; ii++ ) {
|
||||
boolean successfulAdd = returnList.add( values[ii] );
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
// FIXME:: do we need to implement this??
|
||||
// public boolean equals( Object obj ) {}
|
||||
|
||||
// --------------- From Cloneable
|
||||
public Object clone()
|
||||
{
|
||||
MultiHashMap obj = (MultiHashMap)(super.clone());
|
||||
obj.mName = mName;
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* 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 file.
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This is simply a Map with slightly different semantics.
|
||||
* Instead of returning an Object, it returns a Collection.
|
||||
* So for example, you can put( key, new Integer(1) );
|
||||
* and then a Object get( key ); will return you a Collection
|
||||
* instead of an Integer.
|
||||
* Thus, this is simply a tag interface.
|
||||
*
|
||||
* @author Christopher Berry
|
||||
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
|
||||
*/
|
||||
public interface MultiMap extends Map {
|
||||
|
||||
public Object remove( Object key, Object item );
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.12 2001/09/17 16:43:49 jstrachan Exp $
|
||||
* $Revision: 1.12 $
|
||||
* $Date: 2001/09/17 16:43:49 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.13 2001/09/18 10:41:39 jstrachan Exp $
|
||||
* $Revision: 1.13 $
|
||||
* $Date: 2001/09/18 10:41:39 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -66,7 +66,7 @@ import junit.framework.*;
|
|||
/**
|
||||
* Entry point for all Collections tests.
|
||||
* @author Rodney Waldhoff
|
||||
* @version $Id: TestAll.java,v 1.12 2001/09/17 16:43:49 jstrachan Exp $
|
||||
* @version $Id: TestAll.java,v 1.13 2001/09/18 10:41:39 jstrachan Exp $
|
||||
*/
|
||||
public class TestAll extends TestCase {
|
||||
public TestAll(String testName) {
|
||||
|
@ -90,6 +90,7 @@ public class TestAll extends TestCase {
|
|||
suite.addTest(TestFastTreeMap1.suite());
|
||||
suite.addTest(TestHashBag.suite());
|
||||
suite.addTest(TestHashMap.suite());
|
||||
suite.addTest(TestMultiHashMap.suite());
|
||||
suite.addTest(TestSequencedHashMap.suite());
|
||||
suite.addTest(TestSingletonIterator.suite());
|
||||
suite.addTest(TestTreeBag.suite());
|
||||
|
|
|
@ -0,0 +1,226 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMultiHashMap.java,v 1.1 2001/09/18 10:41:39 jstrachan Exp $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2001/09/18 10:41:39 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, 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 acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", 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 names without prior written
|
||||
* permission of the Apache Group.
|
||||
*
|
||||
* 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 (INCLUDING, 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.commons.collections;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
|
||||
/**
|
||||
* Unit Tests for <code>MultiHashMap</code>.
|
||||
*
|
||||
*/
|
||||
public class TestMultiHashMap extends TestMap
|
||||
{
|
||||
public TestMultiHashMap(String testName)
|
||||
{
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite(TestMultiHashMap.class);
|
||||
}
|
||||
|
||||
public static void main(String args[])
|
||||
{
|
||||
String[] testCaseName = { TestMultiHashMap.class.getName() };
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
public Map makeMap() {
|
||||
return new MultiHashMap();
|
||||
}
|
||||
|
||||
//----------------------------
|
||||
// Tests
|
||||
//----------------------------
|
||||
public void testPutNGet()
|
||||
{
|
||||
MultiHashMap map = new MultiHashMap();
|
||||
loadMap( map );
|
||||
checkMap( map );
|
||||
|
||||
assertTrue( map.get(new Integer(99)) == null );
|
||||
|
||||
map.clear();
|
||||
assertTrue( map.size() == 0 );
|
||||
}
|
||||
|
||||
public void testContainsValue()
|
||||
{
|
||||
MultiHashMap map = new MultiHashMap();
|
||||
loadMap( map );
|
||||
|
||||
assertTrue( map.containsValue( "uno" ) );
|
||||
assertTrue( map.containsValue( "quatro" ) );
|
||||
assertTrue( map.containsValue( "two" ) );
|
||||
|
||||
assertTrue( ! map.containsValue( "uggaBugga" ) );
|
||||
|
||||
map.clear();
|
||||
}
|
||||
|
||||
public void testValues()
|
||||
{
|
||||
MultiHashMap map = new MultiHashMap();
|
||||
loadMap( map );
|
||||
|
||||
Collection vals = map.values();
|
||||
assertTrue( vals.size() == getFullSize() );
|
||||
|
||||
map.clear();
|
||||
}
|
||||
|
||||
|
||||
static private class MapPair
|
||||
{
|
||||
MapPair( int key, String val )
|
||||
{
|
||||
mKey = new Integer( key );
|
||||
mValue = val;
|
||||
}
|
||||
|
||||
Integer mKey = null;
|
||||
String mValue = null;
|
||||
}
|
||||
|
||||
static private MapPair[][] sMapPairs =
|
||||
{
|
||||
{new MapPair(0,"zero")},
|
||||
{new MapPair(1,"one"), new MapPair(1,"ONE"), new MapPair(1,"uno")},
|
||||
{new MapPair(2,"two"), new MapPair(2,"two") },
|
||||
{new MapPair(3,"three"), new MapPair(3,"THREE"), new MapPair(3,"tres")},
|
||||
{new MapPair(4,"four"), new MapPair(4,"quatro")}
|
||||
};
|
||||
|
||||
private void loadMap( MultiHashMap map )
|
||||
{
|
||||
// Set up so that we load the keys "randomly"
|
||||
// (i.e. we don't want to load int row-order, so that all like keys
|
||||
// load together. We want to mix it up...)
|
||||
|
||||
int numRows = sMapPairs.length;
|
||||
int maxCols = 0;
|
||||
for( int ii=0; ii < sMapPairs.length; ii++ ){
|
||||
if ( sMapPairs[ii].length > maxCols )
|
||||
maxCols = sMapPairs[ii].length;
|
||||
}
|
||||
for( int ii=0; ii < maxCols; ii++ ){
|
||||
for( int jj=0; jj < numRows; jj++ ){
|
||||
if ( ii < sMapPairs[jj].length ) {
|
||||
map.put( sMapPairs[jj][ii].mKey, sMapPairs[jj][ii].mValue);
|
||||
//---------------------------------------------------------
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue( map.size() == sMapPairs.length );
|
||||
}
|
||||
|
||||
private void checkMap( MultiHashMap map )
|
||||
{
|
||||
for( int ii=0; ii < sMapPairs.length; ii++ ){
|
||||
checkKeyList( map, ii );
|
||||
}
|
||||
}
|
||||
|
||||
private void checkKeyList( MultiHashMap map, int index )
|
||||
{
|
||||
assertTrue( index < sMapPairs.length );
|
||||
Integer key = sMapPairs[index][0].mKey ;
|
||||
|
||||
Object obj = map.get( key );
|
||||
//--------------------------
|
||||
|
||||
assertTrue( obj != null );
|
||||
assertTrue( obj instanceof Collection );
|
||||
Collection keyList = (Collection)obj;
|
||||
|
||||
assertTrue( keyList.size() == sMapPairs[index].length );
|
||||
Iterator iter = keyList.iterator();
|
||||
while ( iter.hasNext() ) {
|
||||
Object oval = iter.next();
|
||||
assertTrue( oval != null );
|
||||
assertTrue( oval instanceof String );
|
||||
String val = (String)oval;
|
||||
boolean foundIt = false;
|
||||
for( int ii=0; ii < sMapPairs[index].length; ii++ ){
|
||||
if( val.equals( sMapPairs[index][ii].mValue ) )
|
||||
foundIt = true;
|
||||
}
|
||||
assertTrue( foundIt );
|
||||
}
|
||||
}
|
||||
|
||||
public int getFullSize()
|
||||
{
|
||||
int len = 0;
|
||||
for( int ii=0; ii < sMapPairs.length; ii++ ){
|
||||
len += sMapPairs[ii].length;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue