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.List;
  19. import java.util.ListIterator;
  20. import org.apache.commons.collections.Transformer;
  21. import org.apache.commons.collections.collection.TransformedCollection;
  22. import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
  23. /**
  24. * Decorates another <code>List</code> to transform objects that are added.
  25. * <p>
  26. * The add and set methods are affected by this class.
  27. * Thus objects must be removed or searched for using their transformed form.
  28. * For example, if the transformation converts Strings to Integers, you must
  29. * use the Integer form to remove objects.
  30. * <p>
  31. * This class is Serializable from Commons Collections 3.1.
  32. *
  33. * @since Commons Collections 3.0
  34. * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
  35. *
  36. * @author Stephen Colebourne
  37. */
  38. public class TransformedList extends TransformedCollection implements List {
  39. /** Serialization version */
  40. private static final long serialVersionUID = 1077193035000013141L;
  41. /**
  42. * Factory method to create a transforming list.
  43. * <p>
  44. * If there are any elements already in the list being decorated, they
  45. * are NOT transformed.
  46. *
  47. * @param list the list to decorate, must not be null
  48. * @param transformer the transformer to use for conversion, must not be null
  49. * @throws IllegalArgumentException if list or transformer is null
  50. */
  51. public static List decorate(List list, Transformer transformer) {
  52. return new TransformedList(list, transformer);
  53. }
  54. //-----------------------------------------------------------------------
  55. /**
  56. * Constructor that wraps (not copies).
  57. * <p>
  58. * If there are any elements already in the list being decorated, they
  59. * are NOT transformed.
  60. *
  61. * @param list the list to decorate, must not be null
  62. * @param transformer the transformer to use for conversion, must not be null
  63. * @throws IllegalArgumentException if list or transformer is null
  64. */
  65. protected TransformedList(List list, Transformer transformer) {
  66. super(list, transformer);
  67. }
  68. /**
  69. * Gets the decorated list.
  70. *
  71. * @return the decorated list
  72. */
  73. protected List getList() {
  74. return (List) collection;
  75. }
  76. //-----------------------------------------------------------------------
  77. public Object get(int index) {
  78. return getList().get(index);
  79. }
  80. public int indexOf(Object object) {
  81. return getList().indexOf(object);
  82. }
  83. public int lastIndexOf(Object object) {
  84. return getList().lastIndexOf(object);
  85. }
  86. public Object remove(int index) {
  87. return getList().remove(index);
  88. }
  89. //-----------------------------------------------------------------------
  90. public void add(int index, Object object) {
  91. object = transform(object);
  92. getList().add(index, object);
  93. }
  94. public boolean addAll(int index, Collection coll) {
  95. coll = transform(coll);
  96. return getList().addAll(index, coll);
  97. }
  98. public ListIterator listIterator() {
  99. return listIterator(0);
  100. }
  101. public ListIterator listIterator(int i) {
  102. return new TransformedListIterator(getList().listIterator(i));
  103. }
  104. public Object set(int index, Object object) {
  105. object = transform(object);
  106. return getList().set(index, object);
  107. }
  108. public List subList(int fromIndex, int toIndex) {
  109. List sub = getList().subList(fromIndex, toIndex);
  110. return new TransformedList(sub, transformer);
  111. }
  112. /**
  113. * Inner class Iterator for the TransformedList
  114. */
  115. protected class TransformedListIterator extends AbstractListIteratorDecorator {
  116. protected TransformedListIterator(ListIterator iterator) {
  117. super(iterator);
  118. }
  119. public void add(Object object) {
  120. object = transform(object);
  121. iterator.add(object);
  122. }
  123. public void set(Object object) {
  124. object = transform(object);
  125. iterator.set(object);
  126. }
  127. }
  128. }