1. /*
  2. * Copyright 2001-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.betwixt.strategy;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. /**
  20. * <p>ClassNormalizer that uses a list of substitutions.</p>
  21. * <p>
  22. * This <code>ClassNormalizer</code> checks a list (in order) to find a matching
  23. * Class.
  24. * This match can be performed either strictly (using equality) or taking into account
  25. * inheritance and implementation.
  26. * If a match is found then the first substituted class is returned as the normalization.
  27. * </p>
  28. * @author Robert Burrell Donkin
  29. * @since 0.5
  30. */
  31. public class ListedClassNormalizer extends ClassNormalizer {
  32. /** Entries to be normalized */
  33. private ArrayList normalizations = new ArrayList();
  34. /** Should the equality (rather than isAssignabledFrom) be used to check */
  35. private boolean strickCheck = false;
  36. /**
  37. * Is strict checking of substitutions on?
  38. * @return true is equality is used to compare classes when considering substition,
  39. * otherwise isAssignableFrom will be used so that super classes and super interfaces
  40. * will be matched.
  41. */
  42. public boolean isStrickCheck() {
  43. return strickCheck;
  44. }
  45. /**
  46. * Sets strict checking of substitutions?
  47. * @param strickCheck if true then equality will be used to compare classes
  48. * when considering substition,
  49. * otherwise isAssignableFrom will be used so that super classes and super interfaces
  50. * will be matched.
  51. */
  52. public void setStrickCheck(boolean strickCheck) {
  53. this.strickCheck = strickCheck;
  54. }
  55. /**
  56. * Adds this given substitution to the list.
  57. * No warning is given if the match has already been added to the list.
  58. * @param match if any classes matching this then the normal class will be substituted
  59. * @param substitute the normalized Class if the primary class is matched
  60. */
  61. public void addSubstitution( Class match, Class substitute ) {
  62. normalizations.add( new ListEntry( match, substitute ));
  63. }
  64. /**
  65. * Adds the given substitute to the list.
  66. * This is a convenience method useful when {@link isStrickCheck} is false.
  67. * In this case, any subclasses (if this is a class) or implementating classes
  68. * if this is an interface) will be subsituted with this value.
  69. * @param substitute sustitude this Class
  70. */
  71. public void addSubstitution( Class substitute ) {
  72. addSubstitution( substitute, substitute );
  73. }
  74. /**
  75. * Normalize given class.
  76. * The normalized Class is the Class that Betwixt should
  77. * introspect.
  78. * This strategy class allows the introspected Class to be
  79. * varied.
  80. *
  81. * @param clazz the class to normalize, not null
  82. * @return this implementation check it's list of substitutations in order
  83. * and returns the first that matchs. If {@link #isStrickCheck} then equality
  84. * is used otherwise isAssignableFrom is used (so that super class and interfaces are matched).
  85. */
  86. public Class normalize( Class clazz ) {
  87. Iterator it = normalizations.iterator();
  88. while ( it.hasNext() ) {
  89. ListEntry entry = (ListEntry) it.next();
  90. if ( strickCheck ) {
  91. if ( entry.match.equals( clazz ) ) {
  92. return entry.substitute;
  93. }
  94. } else {
  95. if ( entry.match.isAssignableFrom( clazz )) {
  96. return entry.substitute;
  97. }
  98. }
  99. }
  100. return clazz;
  101. }
  102. /** Holds list entries */
  103. private class ListEntry {
  104. /** Class to be check */
  105. Class match;
  106. /** Substituted to be returned */
  107. Class substitute;
  108. /**
  109. * Base constructor
  110. * @param match match this Class
  111. * @param subsistute substitute matches with this Class
  112. */
  113. ListEntry( Class match, Class substitute ) {
  114. this.match = match;
  115. this.substitute = substitute;
  116. }
  117. }
  118. }