KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > LazyMap


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.util.collection;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  * LazyMap.
32  *
33  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
34  * @version $Revision: 1958 $
35  */

36 public class LazyMap implements Map JavaDoc
37 {
38    /** The delegate map */
39    private Map JavaDoc delegate = Collections.EMPTY_MAP;
40    
41    public void clear()
42    {
43       delegate = Collections.EMPTY_MAP;
44    }
45
46    public boolean containsKey(Object JavaDoc key)
47    {
48       return delegate.containsKey(key);
49    }
50
51    public boolean containsValue(Object JavaDoc value)
52    {
53       return delegate.containsValue(value);
54    }
55
56    public Set JavaDoc entrySet()
57    {
58       return delegate.entrySet();
59    }
60
61    public Object JavaDoc get(Object JavaDoc key)
62    {
63       return delegate.get(key);
64    }
65
66    public boolean isEmpty()
67    {
68       return delegate.isEmpty();
69    }
70
71    public Set JavaDoc keySet()
72    {
73       return delegate.keySet();
74    }
75
76    public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
77    {
78       if (delegate == Collections.EMPTY_MAP)
79       {
80          delegate = Collections.singletonMap(key, value);
81          return null;
82       }
83       else
84       {
85          if (delegate instanceof HashMap JavaDoc == false)
86             delegate = new HashMap JavaDoc(delegate);
87          return delegate.put(key, value);
88       }
89    }
90
91    public void putAll(Map JavaDoc t)
92    {
93       if (delegate instanceof HashMap JavaDoc == false)
94          delegate = new HashMap JavaDoc(delegate);
95       delegate.putAll(t);
96    }
97
98    public Object JavaDoc remove(Object JavaDoc key)
99    {
100       if (delegate instanceof HashMap JavaDoc == false)
101          delegate = new HashMap JavaDoc(delegate);
102       return delegate.remove(key);
103    }
104
105    public int size()
106    {
107       return delegate.size();
108    }
109
110    public Collection JavaDoc values()
111    {
112       return delegate.values();
113    }
114 }
115
Popular Tags