KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > HashMapList


1 /**
2  * com.mckoi.util.HashMapList 02 Oct 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.util.*;
28
29 /**
30  * A HashMap that maps from a source to a list of items for that source. This
31  * is useful as a searching mechanism where the list of searched items are
32  * catagorised in the mapped list.
33  *
34  * @author Tobias Downer
35  */

36
37 public class HashMapList {
38
39   private static final List EMPTY_LIST = Arrays.asList(new Object JavaDoc[0]);
40
41   private HashMap map;
42
43   /**
44    * Constructs the map.
45    */

46   public HashMapList() {
47     map = new HashMap();
48   }
49
50   /**
51    * Puts a value into the map list.
52    */

53   public void put(Object JavaDoc key, Object JavaDoc val) {
54     ArrayList list = (ArrayList) map.get(key);
55     if (list == null) {
56       list = new ArrayList();
57     }
58     list.add(val);
59     map.put(key, list);
60   }
61
62   /**
63    * Returns the list of values that are in the map under this key. Returns
64    * an empty list if no key map found.
65    */

66   public List get(Object JavaDoc key) {
67     ArrayList list = (ArrayList) map.get(key);
68     if (list != null) {
69       return list;
70     }
71     return EMPTY_LIST;
72   }
73
74   /**
75    * Removes the given value from the list with the given key.
76    */

77   public boolean remove(Object JavaDoc key, Object JavaDoc val) {
78     ArrayList list = (ArrayList) map.get(key);
79     if (list == null) {
80       return false;
81     }
82     boolean status = list.remove(val);
83     if (list.size() == 0) {
84       map.remove(key);
85     }
86     return status;
87   }
88
89   /**
90    * Clears the all the values for the given key. Returns the List of
91    * items that were stored under this key.
92    */

93   public List clear(Object JavaDoc key) {
94     ArrayList list = (ArrayList) map.remove(key);
95     if (list == null) {
96       return new ArrayList();
97     }
98     return list;
99   }
100
101   /**
102    * The Set of all keys.
103    */

104   public Set keySet() {
105     return map.keySet();
106   }
107
108   /**
109    * Returns true if the map contains the key.
110    */

111   public boolean containsKey(Object JavaDoc key) {
112     return map.containsKey(key);
113   }
114
115 }
116
Popular Tags