001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.data.dataset;
031
032import java.util.ArrayList;
033import java.util.Collection;
034import java.util.Iterator;
035import java.util.List;
036import java.util.ListIterator;
037
038/**
039 * A {@link ListBackedDataset} is a {@link Dataset} backed by an ordered list of
040 * items. For convenience, the dataset is itself presented as as list.
041 * 
042 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
043 * 
044 * @param <T>
045 *            the type of items in the dataset
046 */
047public class ListBackedDataset<T> implements ListDataset<T> {
048        protected List<T> data;
049
050        /**
051         * Construct with the an empty {@link ArrayList} as the backing store.
052         */
053        public ListBackedDataset() {
054                this.data = new ArrayList<T>();
055        }
056
057        /**
058         * Construct with the given list of items.
059         * 
060         * @param backingList
061         *            the backing list
062         */
063        public ListBackedDataset(List<T> backingList) {
064                this.data = backingList;
065        }
066
067        /**
068         * Construct by consuming the contents of the given iterator into the
069         * backing list.
070         * <p>
071         * Obviously this method could cause problems if the number of items in the
072         * iterator is very large, as memory could be exhausted. Care should be
073         * taken.
074         * 
075         * @param iterator
076         *            the data to read
077         */
078        public ListBackedDataset(Iterable<T> iterator) {
079                super();
080
081                for (final T item : iterator) {
082                        data.add(item);
083                }
084        }
085
086        @Override
087        public T getRandomInstance() {
088                return data.get((int) (Math.random() * data.size()));
089        }
090
091        @Override
092        public final int size() {
093                return data.size();
094        }
095
096        @Override
097        public int numInstances() {
098                return data.size();
099        }
100
101        @Override
102        public T getInstance(int i) {
103                return data.get(i);
104        }
105
106        /**
107         * Get the underlying list backing this dataset
108         * 
109         * @return the list backing this dataset
110         */
111        public List<T> getList() {
112                return data;
113        }
114
115        @Override
116        public Iterator<T> iterator() {
117                return data.iterator();
118        }
119
120        @Override
121        public boolean isEmpty() {
122                return data.isEmpty();
123        }
124
125        @Override
126        public boolean contains(Object o) {
127                return data.contains(o);
128        }
129
130        @Override
131        public Object[] toArray() {
132                return data.toArray();
133        }
134
135        @Override
136        public <V> V[] toArray(V[] a) {
137                return data.toArray(a);
138        }
139
140        @Override
141        public boolean add(T e) {
142                return data.add(e);
143        }
144
145        @Override
146        public boolean remove(Object o) {
147                return data.remove(o);
148        }
149
150        @Override
151        public boolean containsAll(Collection<?> c) {
152                return data.containsAll(c);
153        }
154
155        @Override
156        public boolean addAll(Collection<? extends T> c) {
157                return data.addAll(c);
158        }
159
160        @Override
161        public boolean addAll(int index, Collection<? extends T> c) {
162                return data.addAll(index, c);
163        }
164
165        @Override
166        public boolean removeAll(Collection<?> c) {
167                return data.removeAll(c);
168        }
169
170        @Override
171        public boolean retainAll(Collection<?> c) {
172                return data.retainAll(c);
173        }
174
175        @Override
176        public void clear() {
177                data.clear();
178        }
179
180        @Override
181        public T get(int index) {
182                return data.get(index);
183        }
184
185        @Override
186        public T set(int index, T element) {
187                return data.set(index, element);
188        }
189
190        @Override
191        public void add(int index, T element) {
192                data.add(index, element);
193        }
194
195        @Override
196        public T remove(int index) {
197                return data.remove(index);
198        }
199
200        @Override
201        public int indexOf(Object o) {
202                return data.indexOf(o);
203        }
204
205        @Override
206        public int lastIndexOf(Object o) {
207                return data.lastIndexOf(o);
208        }
209
210        @Override
211        public ListIterator<T> listIterator() {
212                return data.listIterator();
213        }
214
215        @Override
216        public ListIterator<T> listIterator(int index) {
217                return data.listIterator(index);
218        }
219
220        @Override
221        public List<T> subList(int fromIndex, int toIndex) {
222                return data.subList(fromIndex, toIndex);
223        }
224
225        @Override
226        public String toString() {
227                return data.toString();
228        }
229}