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.BoundedCollection;
  22. import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
  23. import org.apache.commons.collections.iterators.UnmodifiableIterator;
  24. /**
  25. * Decorates another <code>List</code> to fix the size preventing add/remove.
  26. * <p>
  27. * The add, remove, clear and retain operations are unsupported.
  28. * The set method is allowed (as it doesn't change the list size).
  29. * <p>
  30. * This class is Serializable from Commons Collections 3.1.
  31. *
  32. * @since Commons Collections 3.0
  33. * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:13 $
  34. *
  35. * @author Stephen Colebourne
  36. * @author Paul Jack
  37. */
  38. public class FixedSizeList
  39. extends AbstractSerializableListDecorator
  40. implements BoundedCollection {
  41. /** Serialization version */
  42. private static final long serialVersionUID = -2218010673611160319L;
  43. /**
  44. * Factory method to create a fixed size list.
  45. *
  46. * @param list the list to decorate, must not be null
  47. * @throws IllegalArgumentException if list is null
  48. */
  49. public static List decorate(List list) {
  50. return new FixedSizeList(list);
  51. }
  52. //-----------------------------------------------------------------------
  53. /**
  54. * Constructor that wraps (not copies).
  55. *
  56. * @param list the list to decorate, must not be null
  57. * @throws IllegalArgumentException if list is null
  58. */
  59. protected FixedSizeList(List list) {
  60. super(list);
  61. }
  62. //-----------------------------------------------------------------------
  63. public boolean add(Object object) {
  64. throw new UnsupportedOperationException("List is fixed size");
  65. }
  66. public void add(int index, Object object) {
  67. throw new UnsupportedOperationException("List is fixed size");
  68. }
  69. public boolean addAll(Collection coll) {
  70. throw new UnsupportedOperationException("List is fixed size");
  71. }
  72. public boolean addAll(int index, Collection coll) {
  73. throw new UnsupportedOperationException("List is fixed size");
  74. }
  75. public void clear() {
  76. throw new UnsupportedOperationException("List is fixed size");
  77. }
  78. public Object get(int index) {
  79. return getList().get(index);
  80. }
  81. public int indexOf(Object object) {
  82. return getList().indexOf(object);
  83. }
  84. public Iterator iterator() {
  85. return UnmodifiableIterator.decorate(getCollection().iterator());
  86. }
  87. public int lastIndexOf(Object object) {
  88. return getList().lastIndexOf(object);
  89. }
  90. public ListIterator listIterator() {
  91. return new FixedSizeListIterator(getList().listIterator(0));
  92. }
  93. public ListIterator listIterator(int index) {
  94. return new FixedSizeListIterator(getList().listIterator(index));
  95. }
  96. public Object remove(int index) {
  97. throw new UnsupportedOperationException("List is fixed size");
  98. }
  99. public boolean remove(Object object) {
  100. throw new UnsupportedOperationException("List is fixed size");
  101. }
  102. public boolean removeAll(Collection coll) {
  103. throw new UnsupportedOperationException("List is fixed size");
  104. }
  105. public boolean retainAll(Collection coll) {
  106. throw new UnsupportedOperationException("List is fixed size");
  107. }
  108. public Object set(int index, Object object) {
  109. return getList().set(index, object);
  110. }
  111. public List subList(int fromIndex, int toIndex) {
  112. List sub = getList().subList(fromIndex, toIndex);
  113. return new FixedSizeList(sub);
  114. }
  115. /**
  116. * List iterator that only permits changes via set()
  117. */
  118. static class FixedSizeListIterator extends AbstractListIteratorDecorator {
  119. protected FixedSizeListIterator(ListIterator iterator) {
  120. super(iterator);
  121. }
  122. public void remove() {
  123. throw new UnsupportedOperationException("List is fixed size");
  124. }
  125. public void add(Object object) {
  126. throw new UnsupportedOperationException("List is fixed size");
  127. }
  128. }
  129. public boolean isFull() {
  130. return true;
  131. }
  132. public int maxSize() {
  133. return size();
  134. }
  135. }