001/**
002 * Copyright (c) 2012, 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.twitter.collection;
031
032import java.io.DataOutput;
033import java.io.File;
034import java.io.IOException;
035import java.io.PrintWriter;
036import java.util.List;
037
038import org.openimaj.io.FileUtils;
039import org.openimaj.twitter.GeneralJSON;
040import org.openimaj.twitter.USMFStatus;
041import org.openimaj.util.list.AbstractFileBackedList;
042
043/**
044 * 
045 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk), Sina Samangooei
046 *         (ss@ecs.soton.ac.uk)
047 * 
048 * @param <T>
049 */
050public class FileTwitterStatusList<T extends USMFStatus> extends AbstractFileBackedList<T>
051                implements
052                        TwitterStatusList<T>
053{
054
055        private Class<? extends GeneralJSON> seedClass = null;
056
057        protected FileTwitterStatusList(int size, File file, String charset, Class<T> clazz) {
058                super(size, false, 0, -1, file, clazz, charset);
059        }
060
061        protected FileTwitterStatusList(int size, File file, String charset, Class<T> clazz,
062                        Class<? extends GeneralJSON> seedClass)
063        {
064                super(size, false, 0, -1, file, clazz, charset);
065                this.seedClass = seedClass;
066        }
067
068        protected FileTwitterStatusList(int size, File file, Class<T> clazz) {
069                super(size, false, 0, -1, file, clazz);
070        }
071
072        /**
073         *
074         */
075        private static final long serialVersionUID = -785707085718120105L;
076
077        @Override
078        public void writeASCII(PrintWriter out) throws IOException {
079                for (int i = 0; i < this.size; i++) {
080                        this.get(i).writeASCII(out);
081                        out.println();
082                }
083        }
084
085        @SuppressWarnings("unchecked")
086        @Override
087        protected T newElementInstance() {
088                if (seedClass == null)
089                        return (T) new USMFStatus();
090                else
091                        return (T) new USMFStatus(seedClass);
092        }
093
094        @Override
095        public String asciiHeader() {
096                return "";
097        }
098
099        /**
100         * @param f
101         * @return a status list of {@link USMFStatus} instances read from a file
102         *         assuming the {@link USMFStatus} format
103         * @throws IOException
104         */
105        public static FileTwitterStatusList<USMFStatus> readUSMF(File f) throws IOException {
106                final int size = FileUtils.countLines(f);
107                return new FileTwitterStatusList<USMFStatus>(size, f, USMFStatus.class);
108        }
109
110        /**
111         * @param f
112         * @param charset
113         *            the charset for the reader
114         * @return a status list of {@link USMFStatus} instances read from a file
115         *         assuming the {@link USMFStatus} format
116         * @throws IOException
117         */
118        public static FileTwitterStatusList<USMFStatus> readUSMF(File f, String charset) throws IOException {
119                final int size = FileUtils.countLines(f);
120                return new FileTwitterStatusList<USMFStatus>(size, f, charset, USMFStatus.class);
121        }
122
123        /**
124         * @param f
125         * @param size
126         *            number of statuses to read
127         * @return a status list of {@link USMFStatus} instances read from a file
128         *         assuming the {@link USMFStatus} format
129         * @throws IOException
130         */
131        public static FileTwitterStatusList<USMFStatus> readUSMF(File f, int size) throws IOException {
132                return new FileTwitterStatusList<USMFStatus>(size, f, USMFStatus.class);
133        }
134
135        /**
136         * @param f
137         * @param charset
138         *            the charset for the reader
139         * @param size
140         *            number of statuses to read
141         * @return a status list of {@link USMFStatus} instances read from a file
142         *         assuming the {@link USMFStatus} format
143         * @throws IOException
144         */
145        public static FileTwitterStatusList<USMFStatus> readUSMF(File f, String charset, int size) throws IOException {
146                return new FileTwitterStatusList<USMFStatus>(size, f, charset, USMFStatus.class);
147        }
148
149        /**
150         * @param f
151         * @param charset
152         *            the charset for the reader
153         * @param generalJSON
154         *            the input type
155         * @return a status list of {@link USMFStatus} instances read from a file
156         *         using generalJSON as the input type
157         */
158        public static FileTwitterStatusList<USMFStatus> readUSMF(File f, String charset,
159                        Class<? extends GeneralJSON> generalJSON)
160        {
161                final int size = FileUtils.countLines(f);
162                return new FileTwitterStatusList<USMFStatus>(size, f, charset, USMFStatus.class, generalJSON);
163        }
164
165        /**
166         * @param f
167         * @param charset
168         *            the charset for the reader
169         * @param generalJSON
170         *            the input type
171         * @param size
172         *            number of statuses to read
173         * @return a status list of {@link USMFStatus} instances read from a file
174         *         using generalJSON as the input type
175         */
176        public static FileTwitterStatusList<USMFStatus> readUSMF(File f, int size, String charset,
177                        Class<? extends GeneralJSON> generalJSON)
178        {
179                return new FileTwitterStatusList<USMFStatus>(size, f, charset, USMFStatus.class, generalJSON);
180        }
181
182        @Override
183        public void writeBinary(DataOutput out) throws IOException {
184                throw new UnsupportedOperationException();
185
186        }
187
188        @Override
189        public byte[] binaryHeader() {
190                throw new UnsupportedOperationException();
191        }
192
193        @Override
194        protected AbstractFileBackedList<T> newInstance(int newSize, boolean isBinary, int newHeaderLength, int recordLength,
195                        File file)
196        {
197                return new FileTwitterStatusList<T>(newSize, file, this.charset, this.clz);
198        }
199
200        public static List<USMFStatus> readUSMF(File f, Class<? extends GeneralJSON> generalJSON) {
201                final int size = FileUtils.countLines(f);
202                final String charset = "UTF-8";
203                return new FileTwitterStatusList<USMFStatus>(size, f, charset, USMFStatus.class, generalJSON);
204        }
205
206}