KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > LazyHashtable


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 /** Hashtable implementation that allows delayed construction
24  * of expensive objects
25  *
26  * All operations that need access to the full list of objects
27  * will call initAll() first. Get and put are cheap.
28  *
29  * @since Ant 1.6
30  */

31 public class LazyHashtable extends Hashtable JavaDoc {
32     // CheckStyle:VisibilityModifier OFF - bc
33
protected boolean initAllDone = false;
34     // CheckStyle:VisibilityModifier OFF - bc
35

36     /** No arg constructor. */
37     public LazyHashtable() {
38         super();
39     }
40
41     /** Used to be part of init. It must be done once - but
42      * we delay it until we do need _all_ tasks. Otherwise we
43      * just get the tasks that we need, and avoid costly init.
44      */

45     protected void initAll() {
46         if (initAllDone) {
47             return;
48         }
49         initAllDone = true;
50     }
51
52
53     /**
54      * Get a enumeration over the elements.
55      * @return an enumeration.
56      */

57     public Enumeration JavaDoc elements() {
58         initAll();
59         return super.elements();
60     }
61
62     /**
63      * Check if the table is empty.
64      * @return true if it is.
65      */

66     public boolean isEmpty() {
67         initAll();
68         return super.isEmpty();
69     }
70
71     /**
72      * Get the size of the table.
73      * @return the size.
74      */

75     public int size() {
76         initAll();
77         return super.size();
78     }
79
80     /**
81      * Check if the table contains a particular value.
82      * @param value the value to look for.
83      * @return true if the table contains the value.
84      */

85     public boolean contains(Object JavaDoc value) {
86         initAll();
87         return super.contains(value);
88     }
89
90     /**
91      * Check if the table contains a particular key.
92      * @param value the key to look for.
93      * @return true if the table contains key.
94      */

95     public boolean containsKey(Object JavaDoc value) {
96         initAll();
97         return super.containsKey(value);
98     }
99
100     /**
101      * Delegates to {@link #contains contains}.
102      * @param value the value to look for.
103      * @return true if the table contains the value.
104      */

105     public boolean containsValue(Object JavaDoc value) {
106         return contains(value);
107     }
108
109     /**
110      * Get an enumeration over the keys.
111      * @return an enumeration.
112      */

113     public Enumeration JavaDoc keys() {
114         initAll();
115         return super.keys();
116     }
117
118     // XXX Unfortunately JDK1.2 adds entrySet(), keySet(), values() -
119
// implementing this requires a small hack, we can add it later.
120
}
121
Popular Tags