You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StandardObjectChecker.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. /*
  14. * StandardObjectChecker.java created on May 7, 2002
  15. *
  16. */
  17. package org.aspectj.testing.util;
  18. import java.util.List;
  19. /**
  20. * Superclass for checkers that require non-null input
  21. * of a given type.
  22. * Clients may supply delegator for further checks,
  23. * or a list to collect results.
  24. * Subclasses may instead implement doIsValid().
  25. * @author isberg
  26. */
  27. public class StandardObjectChecker implements ObjectChecker {
  28. public final Class type;
  29. private final ObjectChecker delegate;
  30. private final List collector;
  31. private final boolean collectionResult;
  32. /**
  33. * Create one with no delegate.
  34. * @param type the Class of the type required of the input
  35. */
  36. public StandardObjectChecker(Class type) {
  37. this(type, ANY, (List) null, true);
  38. }
  39. /**
  40. * @param type the Class of the type required of the input
  41. * @param delegate the ObjectChecker to delegate to after
  42. * checking for non-null input of the correct type.
  43. */
  44. public StandardObjectChecker(Class type, ObjectChecker delegate) {
  45. this(type, delegate, null, true);
  46. }
  47. /**
  48. * same as StandardObjectChecker(type, collector, true)
  49. * @param type the Class of the type required of the input
  50. * @param collector the list to collect valid entries
  51. */
  52. public StandardObjectChecker(Class type, List collector) {
  53. this(type, ANY, collector, true);
  54. }
  55. /**
  56. * @param type the Class of the type required of the input
  57. * @param collector the list to collect valid entries
  58. * @param collectionResult the value to return when entry was added
  59. */
  60. public StandardObjectChecker(Class type, List collector, boolean collectionResult) {
  61. this(type, ANY, collector, collectionResult);
  62. }
  63. /**
  64. * @param type the Class of the type required of the input
  65. * @param collector the list to collect valid entries
  66. */
  67. public StandardObjectChecker(Class type, ObjectChecker delegate,
  68. List collector, boolean collectionResult) {
  69. if (null == type) throw new IllegalArgumentException("null type");
  70. this.type = type;
  71. this.delegate = delegate;
  72. this.collector = collector;
  73. this.collectionResult = collectionResult;
  74. }
  75. /**
  76. * Check if object is valid by confirming is is non-null and of the
  77. * right type, then delegating to any delegate or calling doIsValid(),
  78. * then (if true) passing to any collector, and returning
  79. * false if the collector failed or the collection result otherwise.
  80. * @see ObjectChecker#isValid(Object)
  81. * @return true unless input is null or wrong type
  82. * or if one of subclass (doIsValid(..)) or delegates
  83. * (list, collector) returns false.
  84. */
  85. public final boolean isValid(Object input) {
  86. if ((null == input) || (!(type.isAssignableFrom(input.getClass())))) {
  87. return false;
  88. } else if (null != delegate) {
  89. if (!delegate.isValid(input)) {
  90. return false;
  91. }
  92. }
  93. if (!doIsValid(input)) {
  94. return false;
  95. }
  96. if (null == collector) {
  97. return true;
  98. } else if (!collector.add(input)) {
  99. return false;
  100. } else {
  101. return collectionResult;
  102. }
  103. }
  104. /**
  105. * Delegate of isValid guarantees that the input
  106. * is not null as is assignable to the specified type.
  107. * Subclasses implement their funtionality here.
  108. * This implementation returns true;
  109. * @return true
  110. */
  111. public boolean doIsValid(Object input) {
  112. return true;
  113. }
  114. }