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.collections.functors;
  17. import java.io.Serializable;
  18. import org.apache.commons.collections.Closure;
  19. import org.apache.commons.collections.Transformer;
  20. /**
  21. * Closure implementation that calls a Transformer using the input object
  22. * and ignore the result.
  23. *
  24. * @since Commons Collections 3.0
  25. * @version $Revision: 1.5 $ $Date: 2004/05/16 11:47:38 $
  26. *
  27. * @author Stephen Colebourne
  28. */
  29. public class TransformerClosure implements Closure, Serializable {
  30. /** Serial version UID */
  31. static final long serialVersionUID = -5194992589193388969L;
  32. /** The transformer to wrap */
  33. private final Transformer iTransformer;
  34. /**
  35. * Factory method that performs validation.
  36. * <p>
  37. * A null transformer will return the <code>NOPClosure</code>.
  38. *
  39. * @param transformer the transformer to call, null means nop
  40. * @return the <code>transformer</code> closure
  41. */
  42. public static Closure getInstance(Transformer transformer) {
  43. if (transformer == null) {
  44. return NOPClosure.INSTANCE;
  45. }
  46. return new TransformerClosure(transformer);
  47. }
  48. /**
  49. * Constructor that performs no validation.
  50. * Use <code>getInstance</code> if you want that.
  51. *
  52. * @param transformer the transformer to call, not null
  53. */
  54. public TransformerClosure(Transformer transformer) {
  55. super();
  56. iTransformer = transformer;
  57. }
  58. /**
  59. * Executes the closure by calling the decorated transformer.
  60. *
  61. * @param input the input object
  62. */
  63. public void execute(Object input) {
  64. iTransformer.transform(input);
  65. }
  66. /**
  67. * Gets the transformer.
  68. *
  69. * @return the transformer
  70. * @since Commons Collections 3.1
  71. */
  72. public Transformer getTransformer() {
  73. return iTransformer;
  74. }
  75. }