1. /*
  2. * Copyright 2002-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.Dictionary;
  19. import java.util.Enumeration;
  20. import java.util.NoSuchElementException;
  21. import java.util.Vector;
  22. /**
  23. * A set of helper methods related to collection manipulation.
  24. *
  25. *
  26. * @since Ant 1.5
  27. */
  28. public class CollectionUtils {
  29. /**
  30. * Please use Vector.equals() or List.equals()
  31. *
  32. * @since Ant 1.5
  33. * @deprecated
  34. */
  35. public static boolean equals(Vector v1, Vector v2) {
  36. if (v1 == v2) {
  37. return true;
  38. }
  39. if (v1 == null || v2 == null) {
  40. return false;
  41. }
  42. return v1.equals(v2);
  43. }
  44. /**
  45. * Dictionary does not have an equals.
  46. * Please use Map.equals()
  47. *
  48. * <p>Follows the equals contract of Java 2's Map.</p>
  49. *
  50. * @since Ant 1.5
  51. * @deprecated
  52. */
  53. public static boolean equals(Dictionary d1, Dictionary d2) {
  54. if (d1 == d2) {
  55. return true;
  56. }
  57. if (d1 == null || d2 == null) {
  58. return false;
  59. }
  60. if (d1.size() != d2.size()) {
  61. return false;
  62. }
  63. Enumeration e1 = d1.keys();
  64. while (e1.hasMoreElements()) {
  65. Object key = e1.nextElement();
  66. Object value1 = d1.get(key);
  67. Object value2 = d2.get(key);
  68. if (value2 == null || !value1.equals(value2)) {
  69. return false;
  70. }
  71. }
  72. // don't need the opposite check as the Dictionaries have the
  73. // same size, so we've also covered all keys of d2 already.
  74. return true;
  75. }
  76. /**
  77. * Dictionary does not know the putAll method. Please use Map.putAll().
  78. *
  79. * @since Ant 1.6
  80. * @deprecated
  81. */
  82. public static void putAll(Dictionary m1, Dictionary m2) {
  83. for (Enumeration it = m2.keys(); it.hasMoreElements();) {
  84. Object key = it.nextElement();
  85. m1.put(key, m2.get(key));
  86. }
  87. }
  88. /**
  89. * @since Ant 1.6
  90. */
  91. public static final class EmptyEnumeration implements Enumeration {
  92. public EmptyEnumeration() {
  93. }
  94. public boolean hasMoreElements() {
  95. return false;
  96. }
  97. public Object nextElement() throws NoSuchElementException {
  98. throw new NoSuchElementException();
  99. }
  100. }
  101. }