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.list;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.ListIterator;
  21. import org.apache.commons.collections.Unmodifiable;
  22. import org.apache.commons.collections.iterators.UnmodifiableIterator;
  23. import org.apache.commons.collections.iterators.UnmodifiableListIterator;
  24. /**
  25. * Decorates another <code>List</code> to ensure it can't be altered.
  26. * <p>
  27. * This class is Serializable from Commons Collections 3.1.
  28. *
  29. * @since Commons Collections 3.0
  30. * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
  31. *
  32. * @author Stephen Colebourne
  33. */
  34. public final class UnmodifiableList
  35. extends AbstractSerializableListDecorator
  36. implements Unmodifiable {
  37. /** Serialization version */
  38. private static final long serialVersionUID = 6595182819922443652L;
  39. /**
  40. * Factory method to create an unmodifiable list.
  41. *
  42. * @param list the list to decorate, must not be null
  43. * @throws IllegalArgumentException if list is null
  44. */
  45. public static List decorate(List list) {
  46. if (list instanceof Unmodifiable) {
  47. return list;
  48. }
  49. return new UnmodifiableList(list);
  50. }
  51. //-----------------------------------------------------------------------
  52. /**
  53. * Constructor that wraps (not copies).
  54. *
  55. * @param list the list to decorate, must not be null
  56. * @throws IllegalArgumentException if list is null
  57. */
  58. private UnmodifiableList(List list) {
  59. super(list);
  60. }
  61. //-----------------------------------------------------------------------
  62. public Iterator iterator() {
  63. return UnmodifiableIterator.decorate(getCollection().iterator());
  64. }
  65. public boolean add(Object object) {
  66. throw new UnsupportedOperationException();
  67. }
  68. public boolean addAll(Collection coll) {
  69. throw new UnsupportedOperationException();
  70. }
  71. public void clear() {
  72. throw new UnsupportedOperationException();
  73. }
  74. public boolean remove(Object object) {
  75. throw new UnsupportedOperationException();
  76. }
  77. public boolean removeAll(Collection coll) {
  78. throw new UnsupportedOperationException();
  79. }
  80. public boolean retainAll(Collection coll) {
  81. throw new UnsupportedOperationException();
  82. }
  83. //-----------------------------------------------------------------------
  84. public ListIterator listIterator() {
  85. return UnmodifiableListIterator.decorate(getList().listIterator());
  86. }
  87. public ListIterator listIterator(int index) {
  88. return UnmodifiableListIterator.decorate(getList().listIterator(index));
  89. }
  90. public void add(int index, Object object) {
  91. throw new UnsupportedOperationException();
  92. }
  93. public boolean addAll(int index, Collection coll) {
  94. throw new UnsupportedOperationException();
  95. }
  96. public Object remove(int index) {
  97. throw new UnsupportedOperationException();
  98. }
  99. public Object set(int index, Object object) {
  100. throw new UnsupportedOperationException();
  101. }
  102. public List subList(int fromIndex, int toIndex) {
  103. List sub = getList().subList(fromIndex, toIndex);
  104. return new UnmodifiableList(sub);
  105. }
  106. }