001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util;
019:
020: import java.util.Hashtable;
021: import java.util.Enumeration;
022:
023: /** Hashtable implementation that allows delayed construction
024: * of expensive objects
025: *
026: * All operations that need access to the full list of objects
027: * will call initAll() first. Get and put are cheap.
028: *
029: * @since Ant 1.6
030: */
031: public class LazyHashtable extends Hashtable {
032: // CheckStyle:VisibilityModifier OFF - bc
033: protected boolean initAllDone = false;
034:
035: // CheckStyle:VisibilityModifier OFF - bc
036:
037: /** No arg constructor. */
038: public LazyHashtable() {
039: super ();
040: }
041:
042: /** Used to be part of init. It must be done once - but
043: * we delay it until we do need _all_ tasks. Otherwise we
044: * just get the tasks that we need, and avoid costly init.
045: */
046: protected void initAll() {
047: if (initAllDone) {
048: return;
049: }
050: initAllDone = true;
051: }
052:
053: /**
054: * Get a enumeration over the elements.
055: * @return an enumeration.
056: */
057: public Enumeration elements() {
058: initAll();
059: return super .elements();
060: }
061:
062: /**
063: * Check if the table is empty.
064: * @return true if it is.
065: */
066: public boolean isEmpty() {
067: initAll();
068: return super .isEmpty();
069: }
070:
071: /**
072: * Get the size of the table.
073: * @return the size.
074: */
075: public int size() {
076: initAll();
077: return super .size();
078: }
079:
080: /**
081: * Check if the table contains a particular value.
082: * @param value the value to look for.
083: * @return true if the table contains the value.
084: */
085: public boolean contains(Object value) {
086: initAll();
087: return super .contains(value);
088: }
089:
090: /**
091: * Check if the table contains a particular key.
092: * @param value the key to look for.
093: * @return true if the table contains key.
094: */
095: public boolean containsKey(Object value) {
096: initAll();
097: return super .containsKey(value);
098: }
099:
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 value) {
106: return contains(value);
107: }
108:
109: /**
110: * Get an enumeration over the keys.
111: * @return an enumeration.
112: */
113: public Enumeration 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: }
|