01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.core.util;
15:
16: import java.util.HashMap;
17: import java.util.Iterator;
18: import java.util.LinkedList;
19: import java.util.List;
20: import java.util.ListIterator;
21: import java.util.Map;
22:
23: /**
24: *
25: * @author jmarranz
26: */
27: public class MapListImpl {
28: public Map map = new HashMap();
29:
30: /**
31: * Creates a new instance of EventTargetListenerList
32: */
33: public MapListImpl() {
34: }
35:
36: public boolean isEmpty() {
37: return map.isEmpty();
38: }
39:
40: public int add(Object key, Object value) {
41: List list = (List) map.get(key);
42: if (list == null) {
43: list = new LinkedList();
44: map.put(key, list);
45: }
46: list.add(value);
47: return list.size() - 1;
48: }
49:
50: public LinkedList get(Object key) {
51: return (LinkedList) map.get(key);
52: }
53:
54: public LinkedList getAllValues() {
55: if (isEmpty())
56: return null;
57:
58: LinkedList res = new LinkedList();
59: for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
60: Map.Entry entry = (Map.Entry) it.next();
61: LinkedList list = (LinkedList) entry.getValue(); // No puede ser null
62: res.addAll(list);
63: }
64: return res;
65: }
66:
67: public Object remove(Object key, Object value) {
68: List list = (List) map.get(key);
69: if (list == null)
70: return null;
71: for (ListIterator it = list.listIterator(); it.hasNext();) {
72: Object currValue = it.next();
73: if ((currValue == value)
74: || ((value != null) && value.equals(currValue))) {
75: it.remove();
76: if (list.isEmpty())
77: map.remove(key);
78: return currValue;
79: }
80: }
81: return null;
82: }
83:
84: public void removeAll() {
85: map.clear();
86: }
87: }
|