KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > LinkedMap


1 /*
2   Copyright (C) 2001 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import org.apache.log4j.Logger;
23
24
25 /**
26  * Use only put(Object,Object) and get(Object).
27  */

28 public class LinkedMap extends HashMap JavaDoc {
29     static Logger logger = Logger.getLogger("util.map");
30
31     Map JavaDoc next;
32     public LinkedMap() {
33     }
34     public LinkedMap(Map JavaDoc next) {
35         this.next = next;
36     }
37     public Object JavaDoc get(Object JavaDoc key) {
38         if (super.containsKey(key)) {
39             return super.get(key);
40         } else {
41             Object JavaDoc result = null;
42             if (next!=null) {
43                 result = next.get(key);
44                 logger.debug(key+" not found, trying next -> "+result);
45                 put(key,result);
46             }
47             return result;
48         }
49     }
50     public boolean containsKey(Object JavaDoc key) {
51         if (super.containsKey(key)) {
52             return true;
53         } else {
54             return next!=null && next.containsKey(key);
55         }
56     }
57 }
58
Popular Tags