Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / util / MultiValueMap.java
1 package jde.util;
2
3 import java.util.*;
4
5 /**
6  * A HashMap subclass which adds multiple values with the same key to
7  * a List of values.  Single values remain single keys, obviating the
8  * need to cart around single-element Lists.
9  *
10  * Copyright (C) 2001 Eric D. Friedman (eric@hfriedman.rdsl.lmi.net)
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Created: Tue Aug 14 19:36:50 2001
27  *
28  * @author Eric D. Friedman
29  * @version $Id: MultiValueMap.java,v 1.1 2001/08/15 06:31:27 eric Exp $
30  */
31
32 public class MultiValueMap extends HashMap {
33     public MultiValueMap () {
34         super();
35     }
36
37     /**
38      * Convenience method for retrieving the value for
39      * <code>key</code> as a List, even when there is only a single
40      * value for that key in the map.  The EMPTY_LIST is returned when
41      * no value is found for <code>key</code> making null checks
42      * unnecessary.
43      *
44      * @param key an <code>Object</code> value
45      * @return a <code>List</code> value
46      */
47     public List getAsList(Object key) {
48         if (containsKey(key)) {
49             Object o = get(key);
50             if (o instanceof List) {
51                 return (List)o;
52             } else {
53                 return Arrays.asList(new Object[] { o });
54             } // end of else
55         } else {
56             return Collections.EMPTY_LIST;
57         } // end of else
58     }
59
60     /**
61      * inserts value into the map for key as a single element or, if
62      * values already exist, as an entry in key's list.
63      *
64      * @param key an <code>Object</code> value
65      * @param value an <code>Object</code> value
66      * @return null
67      */
68     public Object put(Object key, Object value) {
69         if (containsKey(key)) {
70             Object other = get(key);
71             if (other instanceof List) {
72                 ((List)other).add(value);
73             } else {
74                 List l = new ArrayList();
75                 l.add(other);
76                 l.add(value);
77                 super.put(key, l);
78             } 
79         } else {
80             super.put(key,value);
81         }
82         return null;
83     }
84 }// MultiValueMap