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.collections.collection;
  17. import java.util.Collection;
  18. import org.apache.commons.collections.functors.InstanceofPredicate;
  19. /**
  20. * Decorates a <code>Collection</code> to validate that elements added are of a specific type.
  21. * <p>
  22. * The validation of additions is performed via an instanceof test against
  23. * a specified <code>Class</code>. If an object cannot be added to the
  24. * collection, an IllegalArgumentException is thrown.
  25. *
  26. * @since Commons Collections 3.0
  27. * @version $Revision: 1.5 $ $Date: 2004/05/15 12:39:13 $
  28. *
  29. * @author Stephen Colebourne
  30. * @author Matthew Hawthorne
  31. */
  32. public class TypedCollection {
  33. /**
  34. * Factory method to create a typed collection.
  35. * <p>
  36. * If there are any elements already in the collection being decorated, they
  37. * are validated.
  38. *
  39. * @param coll the collection to decorate, must not be null
  40. * @param type the type to allow into the collection, must not be null
  41. * @return a new typed collection
  42. * @throws IllegalArgumentException if collection or type is null
  43. * @throws IllegalArgumentException if the collection contains invalid elements
  44. */
  45. public static Collection decorate(Collection coll, Class type) {
  46. return new PredicatedCollection(coll, InstanceofPredicate.getInstance(type));
  47. }
  48. /**
  49. * Restrictive constructor.
  50. */
  51. protected TypedCollection() {
  52. super();
  53. }
  54. }