KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MultiMap


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17 import java.io.Serializable JavaDoc;
18
19 import org.compiere.util.*;
20
21 /**
22  * MultiMap allows multiple keys with their values.
23  * It accepts null values as keys and values.
24  * (implemented as two array lists)
25  *
26  * @author Jorg Janke
27  * @version $Id: MultiMap.java,v 1.4 2002/08/12 01:55:12 danb Exp $
28  */

29 public final class MultiMap implements Map, Serializable JavaDoc
30 {
31     /**
32      * Constructor with 10 initial Capacity (same as ArrayList)
33      */

34     public MultiMap()
35     {
36         this(10);
37     } // MultiMap
38

39     /**
40      * Constructor
41      */

42     public MultiMap(int initialCapacity)
43     {
44         m_keys = new ArrayList(initialCapacity);
45         m_values = new ArrayList(initialCapacity);
46     } // MultiMap
47

48     private ArrayList m_keys = null;
49     private ArrayList m_values = null;
50
51     /**
52      * Return number of elements
53      */

54     public int size()
55     {
56         return m_keys.size();
57     } // size
58

59     /**
60      * Is Empty
61      */

62     public boolean isEmpty()
63     {
64         return (m_keys.size() == 0);
65     } // isEmpty
66

67     /**
68      * Contains Key
69      */

70     public boolean containsKey(Object JavaDoc key)
71     {
72         return m_keys.contains(key);
73     } // containsKey
74

75     /**
76      * Contains Value
77      */

78     public boolean containsValue(Object JavaDoc value)
79     {
80         return m_values.contains(value);
81     } // containsKey
82

83     /**
84      * Return ArrayList of Values of Key
85      */

86     public Object JavaDoc get(Object JavaDoc key)
87     {
88         return getValues(key);
89     } // get
90

91     /**
92      * Return ArrayList of Values of Key
93      */

94     public ArrayList getValues (Object JavaDoc key)
95     {
96         ArrayList list = new ArrayList();
97         // We don't have it
98
if (!m_keys.contains(key))
99             return list;
100         // go through keys
101
int size = m_keys.size();
102         for (int i = 0; i < size; i++)
103         {
104             if (m_keys.get(i).equals(key))
105                 if (!list.contains(m_values.get(i)))
106                     list.add(m_values.get(i));
107         }
108         return list;
109     } // getValues
110

111     /**
112      * Return ArrayList of Keys with Value
113      */

114     public ArrayList getKeys (Object JavaDoc value)
115     {
116         ArrayList list = new ArrayList();
117         // We don't have it
118
if (!m_values.contains(value))
119             return list;
120         // go through keys
121
int size = m_values.size();
122         for (int i = 0; i < size; i++)
123         {
124             if (m_values.get(i).equals(value))
125                 if (!list.contains(m_keys.get(i)))
126                     list.add(m_keys.get(i));
127         }
128         return list;
129     } // getKeys
130

131     /**
132      * Put Key & Value
133      * @return always null
134      */

135     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
136     {
137         m_keys.add(key);
138         m_values.add(value);
139         return null;
140     } // put
141

142     /**
143      * Remove key
144      */

145     public Object JavaDoc remove(Object JavaDoc key)
146     {
147         throw new java.lang.UnsupportedOperationException JavaDoc("Method remove() not implemented.");
148     } // remove
149

150     /**
151      * Put all
152      */

153     public void putAll(Map t)
154     {
155         throw new java.lang.UnsupportedOperationException JavaDoc("Method putAll() not implemented.");
156     } // putAll
157

158     /**
159      * Clear content
160      */

161     public void clear()
162     {
163         m_keys.clear();
164         m_values.clear();
165     } // clear
166

167     /**
168      * Return HashSet of Keys
169      */

170     public Set keySet()
171     {
172         HashSet keys = new HashSet(m_keys);
173         return keys;
174     } // keySet
175

176     /**
177      * Return Collection of values
178      */

179     public Collection values()
180     {
181         return m_values;
182     }
183
184     /**
185      *
186      */

187     public Set entrySet()
188     {
189         throw new java.lang.UnsupportedOperationException JavaDoc("Method entrySet() not implemented.");
190     }
191
192     /**
193      *
194      */

195     public boolean equals(Object JavaDoc o)
196     {
197         throw new java.lang.UnsupportedOperationException JavaDoc("Method equals() not implemented.");
198     }
199
200     /*************************************************************************/
201
202     /**
203      * Returns class name and number of entries
204      */

205     public String JavaDoc toString()
206     {
207         return "MultiMap #" + m_keys.size();
208     }
209
210     /**
211      * dump all keys - values to log
212      */

213     public void printToLog()
214     {
215         int ll = 7; // log level
216
Log.trace(ll-1, "MultiMap.printToLog");
217
218         int size = m_keys.size();
219         for (int i = 0; i < size; i++)
220         {
221             Object JavaDoc k = m_keys.get(i);
222             Object JavaDoc v = m_values.get(i);
223             Log.trace(ll, k==null ? "null" : k.toString(), v==null ? "null" : v.toString());
224         }
225     } // printToLog
226

227 } // MultiMap
228
Popular Tags