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