1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.util;
  18. import java.util.Hashtable;
  19. import java.util.Enumeration;
  20. /** Hashtable implementation that allows delayed construction
  21. * of expensive objects
  22. *
  23. * All operations that need access to the full list of objects
  24. * will call initAll() first. Get and put are cheap.
  25. *
  26. * @since Ant 1.6
  27. */
  28. public class LazyHashtable extends Hashtable {
  29. protected boolean initAllDone = false;
  30. public LazyHashtable() {
  31. super();
  32. }
  33. /** Used to be part of init. It must be done once - but
  34. * we delay it until we do need _all_ tasks. Otherwise we
  35. * just get the tasks that we need, and avoid costly init.
  36. */
  37. protected void initAll() {
  38. if (initAllDone) {
  39. return;
  40. }
  41. initAllDone = true;
  42. }
  43. public Enumeration elements() {
  44. initAll();
  45. return super.elements();
  46. }
  47. public boolean isEmpty() {
  48. initAll();
  49. return super.isEmpty();
  50. }
  51. public int size() {
  52. initAll();
  53. return super.size();
  54. }
  55. public boolean contains(Object value) {
  56. initAll();
  57. return super.contains(value);
  58. }
  59. public boolean containsKey(Object value) {
  60. initAll();
  61. return super.containsKey(value);
  62. }
  63. /**
  64. * Delegates to {@link #contains contains}.
  65. */
  66. public boolean containsValue(Object value) {
  67. return contains(value);
  68. }
  69. public Enumeration keys() {
  70. initAll();
  71. return super.keys();
  72. }
  73. // XXX Unfortunately JDK1.2 adds entrySet(), keySet(), values() -
  74. // implementing this requires a small hack, we can add it later.
  75. }