View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.util.collections.impl;
18  
19  import java.util.AbstractList;
20  import java.util.List;
21  
22  import ch.elca.el4j.util.codingsupport.CollectionUtils;
23  import ch.elca.el4j.util.collections.FilteredList;
24  import ch.elca.el4j.util.collections.TransformedList;
25  import ch.elca.el4j.util.collections.helpers.Filter;
26  import ch.elca.el4j.util.collections.helpers.Function;
27  
28  /**
29   * Default implementation of {@link TransformedList}.
30   *
31   * @param <I> the backing list's element type
32   * @param <O> this list's element type
33   *
34   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/collections/impl/DefaultTransformedList.java $
35   *
36   * @author Adrian Moos (AMS)
37   */
38  public class DefaultTransformedList<I, O> extends AbstractList<O>
39  	implements TransformedList<I, O> {
40  	
41  	/** The backing List. */
42  	private List<? extends I> m_backing;
43  	
44  	/** The backing function. */
45  	private Function<? super I, O> m_function;
46  	
47  	/** Constructor.
48  	 * @param backing the backing list
49  	 * @param function the transformation function
50  	 */
51  	public DefaultTransformedList(List<? extends I> backing,
52  		Function<? super I, O> function) {
53  		
54  		m_backing = backing;
55  		m_function = function;
56  	}
57  	
58  	/**
59  	 * Returns an element of this list.
60  	 * @param index the index of the element to return
61  	 * @return the element
62  	 */
63  	public O get(int index) {
64  		return m_function.apply(m_backing.get(index));
65  	}
66  	
67  	/**
68  	 * {@inheritDoc}
69  	 */
70  	public int size() { return m_backing.size(); }
71  	
72  	/**
73  	 * {@inheritDoc}
74  	 */
75  	public void swap(int i, int j) {
76  		CollectionUtils.swap(m_backing, i, j);
77  	}
78  	
79  	/**
80  	 * {@inheritDoc}
81  	 */
82  	public void orderLike(List<? extends O> example) {
83  		CollectionUtils.orderLike(this, example);
84  	}
85  
86  	/**
87  	 * {@inheritDoc}
88  	 */
89  	@SuppressWarnings("unchecked")
90  	public O[] toArray(Class<O> c) {
91  		return CollectionUtils.toArray(this, c);
92  	}
93  
94  	/**
95  	 * {@inheritDoc}
96  	 */
97  	public <T> TransformedList<O, T> mapped(
98  		Function<? super O, T> function) {
99  		return new DefaultTransformedList<O, T>(this, function);
100 	}
101 
102 	/**
103 	 * {@inheritDoc}
104 	 */
105 	public List<? extends I> getBacking() {
106 		return m_backing;
107 	}
108 
109 	/** {@inheritDoc} */
110 	public FilteredList<O> filtered(Filter<? super O> filter) {
111 		return new DefaultFilteredList<O>(this, filter);
112 	}
113 }