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.Factory;
  19. import org.apache.commons.collections.Transformer;
  20. /**
  21. * Transformer implementation that calls a Factory and returns the result.
  22. *
  23. * @since Commons Collections 3.0
  24. * @version $Revision: 1.6 $ $Date: 2004/05/16 11:36:31 $
  25. *
  26. * @author Stephen Colebourne
  27. */
  28. public class FactoryTransformer implements Transformer, Serializable {
  29. /** Serial version UID */
  30. static final long serialVersionUID = -6817674502475353160L;
  31. /** The factory to wrap */
  32. private final Factory iFactory;
  33. /**
  34. * Factory method that performs validation.
  35. *
  36. * @param factory the factory to call, not null
  37. * @return the <code>factory</code> transformer
  38. * @throws IllegalArgumentException if the factory is null
  39. */
  40. public static Transformer getInstance(Factory factory) {
  41. if (factory == null) {
  42. throw new IllegalArgumentException("Factory must not be null");
  43. }
  44. return new FactoryTransformer(factory);
  45. }
  46. /**
  47. * Constructor that performs no validation.
  48. * Use <code>getInstance</code> if you want that.
  49. *
  50. * @param factory the factory to call, not null
  51. */
  52. public FactoryTransformer(Factory factory) {
  53. super();
  54. iFactory = factory;
  55. }
  56. /**
  57. * Transforms the input by ignoring the input and returning the result of
  58. * calling the decorated factory.
  59. *
  60. * @param input the input object to transform
  61. * @return the transformed result
  62. */
  63. public Object transform(Object input) {
  64. return iFactory.create();
  65. }
  66. /**
  67. * Gets the factory.
  68. *
  69. * @return the factory
  70. * @since Commons Collections 3.1
  71. */
  72. public Factory getFactory() {
  73. return iFactory;
  74. }
  75. }