1. /*
  2. * Copyright 2003-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. package org.apache.commons.attributes;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.Constructor;
  19. import java.lang.reflect.Method;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.WeakHashMap;
  29. /**
  30. * Commonly used convenience functions.
  31. */
  32. public class AttributeUtil {
  33. /**
  34. * Filters a Collection of <code>Class</code> objects. The returned collection
  35. * only contains those classes that have an attribute of the specified type.
  36. * That is, for each Class clazz in the
  37. * returned collection,
  38. * <code>clazz != null && Attributes.hasAttributeType (clazz, attributeClass)</code>
  39. * is true.
  40. *
  41. * @param classes Collection of Classes to filter. May contain <code>null</code> references,
  42. * but may not itself be <code>null</code>.
  43. * @param attributeClass the class to filter based on.
  44. */
  45. public static Collection getClassesWithAttributeType (Collection classes, Class attributeClass) {
  46. if (classes == null) {
  47. throw new NullPointerException ("classes");
  48. }
  49. if (attributeClass == null) {
  50. throw new NullPointerException ("attributeClass");
  51. }
  52. ArrayList result = new ArrayList ();
  53. Iterator iter = classes.iterator ();
  54. while (iter.hasNext ()) {
  55. Class clazz = (Class) iter.next ();
  56. if (clazz != null) {
  57. if (Attributes.hasAttributeType (clazz, attributeClass)) {
  58. result.add (clazz);
  59. }
  60. }
  61. }
  62. return result;
  63. }
  64. /**
  65. * Filters a collection of objects. The returned collection
  66. * only contains those objects whose class have an attribute
  67. * of the specified type. That is, for each Object o in the
  68. * returned collection,
  69. * <code>o != null && Attributes.hasAttributeType (o.getClass (), attributeClass)</code>
  70. * is true.
  71. *
  72. * @param objects Collection of objects to filter. May contain <code>null</code> references,
  73. * but may not itself be <code>null</code>.
  74. * @param attributeClass the class to filter based on.
  75. */
  76. public static Collection getObjectsWithAttributeType (Collection objects, Class attributeClass) {
  77. if (objects == null) {
  78. throw new NullPointerException ("objects");
  79. }
  80. if (attributeClass == null) {
  81. throw new NullPointerException ("attributeClass");
  82. }
  83. ArrayList result = new ArrayList ();
  84. Iterator iter = objects.iterator ();
  85. while (iter.hasNext ()) {
  86. Object object = iter.next ();
  87. if (object != null) {
  88. Class clazz = object.getClass ();
  89. if (Attributes.hasAttributeType (clazz, attributeClass)) {
  90. result.add (object);
  91. }
  92. }
  93. }
  94. return result;
  95. }
  96. }