1. /*
  2. * Copyright 1999-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.iterators;
  17. import org.apache.commons.collections.MapIterator;
  18. import org.apache.commons.collections.Unmodifiable;
  19. /**
  20. * Decorates a map iterator such that it cannot be modified.
  21. *
  22. * @since Commons Collections 3.0
  23. * @version $Revision: 1.7 $ $Date: 2004/02/18 00:59:50 $
  24. *
  25. * @author Stephen Colebourne
  26. */
  27. public final class UnmodifiableMapIterator implements MapIterator, Unmodifiable {
  28. /** The iterator being decorated */
  29. private MapIterator iterator;
  30. //-----------------------------------------------------------------------
  31. /**
  32. * Decorates the specified iterator such that it cannot be modified.
  33. *
  34. * @param iterator the iterator to decorate
  35. * @throws IllegalArgumentException if the iterator is null
  36. */
  37. public static MapIterator decorate(MapIterator iterator) {
  38. if (iterator == null) {
  39. throw new IllegalArgumentException("MapIterator must not be null");
  40. }
  41. if (iterator instanceof Unmodifiable) {
  42. return iterator;
  43. }
  44. return new UnmodifiableMapIterator(iterator);
  45. }
  46. //-----------------------------------------------------------------------
  47. /**
  48. * Constructor.
  49. *
  50. * @param iterator the iterator to decorate
  51. */
  52. private UnmodifiableMapIterator(MapIterator iterator) {
  53. super();
  54. this.iterator = iterator;
  55. }
  56. //-----------------------------------------------------------------------
  57. public boolean hasNext() {
  58. return iterator.hasNext();
  59. }
  60. public Object next() {
  61. return iterator.next();
  62. }
  63. public Object getKey() {
  64. return iterator.getKey();
  65. }
  66. public Object getValue() {
  67. return iterator.getValue();
  68. }
  69. public Object setValue(Object value) {
  70. throw new UnsupportedOperationException("setValue() is not supported");
  71. }
  72. public void remove() {
  73. throw new UnsupportedOperationException("remove() is not supported");
  74. }
  75. }