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.BufferedInputStream;
033import java.io.DataOutput;
034import java.io.IOException;
035import java.io.InputStream;
036import java.io.PrintWriter;
037
038import org.openimaj.twitter.GeneralJSON;
039import org.openimaj.twitter.USMFStatus;
040import org.openimaj.util.list.AbstractStreamBackedList;
041        
042        
043
044/**
045 * Converts an input stream into a list {@link USMFStatus} instances using various methods. 
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk), Sina Samangooei (ss@ecs.soton.ac.uk)
047 *
048 * @param <T>
049 */
050public class StreamTwitterStatusList<T extends USMFStatus> extends AbstractStreamBackedList<T> implements TwitterStatusList<T>{
051        
052        private Class<? extends GeneralJSON> seedClass=USMFStatus.class;
053        
054        protected StreamTwitterStatusList(InputStream stream, int size,boolean isBinary, int headerLength, int recordLength,Class<T> clazz,String charset) throws IOException{
055                super(stream, size, isBinary, headerLength, recordLength,clazz,charset);
056        }
057        
058        protected StreamTwitterStatusList(InputStream stream, int size,boolean isBinary, int headerLength, int recordLength,Class<? extends GeneralJSON> inputClass, Class<T> instanceClass,String charset) throws IOException{
059                super(stream, size, isBinary, headerLength, recordLength,instanceClass,charset);
060                this.seedClass = inputClass;
061        }
062        
063        protected StreamTwitterStatusList(InputStream stream, int size,boolean isBinary, int headerLength, int recordLength,Class<T> clazz) throws IOException{
064                super(stream, size, isBinary, headerLength, recordLength,clazz);
065        }
066        
067        @SuppressWarnings("unchecked")
068        @Override
069        protected T newElementInstance() {
070                return (T) new USMFStatus(seedClass);
071        }
072        
073        /**
074         * Construct a new StreamTwitterStatusList from the given input stream.
075         * 
076         * @param stream the input stream
077         * @param nTweets of tweets to read from this stream
078         * 
079         * @return a new list
080         * @throws IOException if an error occurs reading from the stream
081         */
082        public static StreamTwitterStatusList<USMFStatus> read(InputStream stream, int nTweets) throws IOException {
083                return read(new BufferedInputStream(stream), nTweets,USMFStatus.class);
084        }
085        
086        /**
087         * Construct a new StreamTwitterStatusList from the given input stream.
088         * 
089         * @param stream the input stream
090         * 
091         * @return a new list
092         * @throws IOException if an error occurs reading from the stream
093         */
094        public static StreamTwitterStatusList<USMFStatus> read(InputStream stream) throws IOException {
095                return read(new BufferedInputStream(stream), -1,USMFStatus.class);
096        }
097        
098                
099        /**
100         * Construct a new StreamTwitterStatusList from the given input stream.
101         * 
102         * @param stream the input stream
103         * @param nTweets of tweets to read from this stream
104         * @param charset the charset used to read the stream
105         * 
106         * @return a new list
107         * @throws IOException if an error occurs reading from the stream
108         */
109        public static StreamTwitterStatusList<USMFStatus> read(InputStream stream, int nTweets,String charset) throws IOException {
110                return read(new BufferedInputStream(stream), nTweets,USMFStatus.class,charset);
111        }
112        
113        /**
114         * Construct a new StreamTwitterStatusList from the given input stream.
115         * 
116         * @param stream the input stream
117         * @param charset the charset of the underlying stream
118         * 
119         * @return a new list
120         * @throws IOException if an error occurs reading from the stream
121         */
122        public static StreamTwitterStatusList<USMFStatus> read(InputStream stream, String charset) throws IOException {
123                return read(new BufferedInputStream(stream), -1,USMFStatus.class,charset);
124        }
125        
126        /**
127         * Construct a new StreamTwitterStatusList from the given input stream.
128         * @param <T> the type of the USMFStatus instances returned
129         * 
130         * @param stream the input stream
131         * @param nTweets of tweets to read from this stream
132         * @param clazz the class to instantiate
133         * 
134         * @return a new list
135         * @throws IOException if an error occurs reading from the stream
136         */
137        public static <T extends USMFStatus> StreamTwitterStatusList<T> read(InputStream stream, int nTweets, Class<T> clazz) throws IOException {
138                return read(new BufferedInputStream(stream), nTweets,clazz);
139        }
140        
141        /**
142         * @param stream
143         * @param generalJSON
144         * @return a list of USMFStatus instances
145         * @throws IOException
146         */
147        public static StreamTwitterStatusList<USMFStatus> readUSMF(InputStream stream, Class<? extends GeneralJSON> generalJSON) throws IOException {
148                StreamTwitterStatusList<USMFStatus> a = read(new BufferedInputStream(stream), -1,generalJSON, USMFStatus.class);
149                return a;
150        }
151        
152        /**
153         * @param stream
154         * @param generalJSON
155         * @param charset the charset to read with
156         * @return a list of USMFStatus instances
157         * @throws IOException
158         */
159        public static StreamTwitterStatusList<USMFStatus> readUSMF(InputStream stream, Class<? extends GeneralJSON> generalJSON, String charset) throws IOException {
160                StreamTwitterStatusList<USMFStatus> a = read(new BufferedInputStream(stream), -1,generalJSON, USMFStatus.class,charset);
161                return a;
162        }
163        
164        /**
165         * @param stream
166         * @param nTweets number of tweets to read
167         * @param generalJSON
168         * @param charset the charset to read with
169         * @return a list of USMFStatus instances
170         * @throws IOException
171         */
172        public static StreamTwitterStatusList<USMFStatus> readUSMF(InputStream stream, int nTweets,Class<? extends GeneralJSON> generalJSON, String charset) throws IOException {
173                StreamTwitterStatusList<USMFStatus> a = read(new BufferedInputStream(stream), -1,generalJSON, USMFStatus.class,charset);
174                return a;
175        }
176        
177        /**
178         * @param stream
179         * @param nTweets 
180         * @param generalJSON
181         * @return a list of USMFStatus instances
182         * @throws IOException
183         */
184        public static StreamTwitterStatusList<USMFStatus> readUSMF(InputStream stream, int nTweets, Class<? extends GeneralJSON> generalJSON) throws IOException {
185                StreamTwitterStatusList<USMFStatus> a = read(new BufferedInputStream(stream), nTweets,generalJSON, USMFStatus.class);
186                return a;
187        }
188        
189        
190        /**
191         * @param <T> 
192         * @param stream
193         * @param nTweets 
194         * @param inputClass the class of used by the {@link USMFStatus} instances to read
195         * @param instanceClass the class of the {@link USMFStatus} instances
196         * @return a list of USMFStatus instances
197         * @throws IOException
198         */
199        public static <T extends USMFStatus> StreamTwitterStatusList<T> read(InputStream stream, int nTweets, Class<? extends GeneralJSON> inputClass, Class<T> instanceClass) throws IOException {
200                StreamTwitterStatusList<T> a = read(new BufferedInputStream(stream), nTweets,inputClass, instanceClass, "UTF-8");
201                return a;
202        }
203        
204        /**
205         * Construct a new StreamTwitterStatusList from the given input stream.
206         * @param <T> the instance type
207         * 
208         * @param stream the input stream
209         * @param nTweets the number of tweets expected
210         * @param inputClass the input class
211         * @param instanceClass the instance class
212         * @param charset the charset of the reader
213         * 
214         * @return a new list
215         * @throws IOException if an error occurs reading from the stream
216         */
217        public static <T extends USMFStatus> StreamTwitterStatusList<T> read(BufferedInputStream stream, int nTweets,Class<? extends GeneralJSON> inputClass, Class<T> instanceClass, String charset) throws IOException {
218                boolean isBinary = false;
219                                
220                //read header
221                int size = nTweets;
222                int headerLength = 0;
223                int recordLength = -1;
224                
225                return new StreamTwitterStatusList<T>(stream, size, isBinary, headerLength, recordLength,inputClass,instanceClass,charset);
226        }
227        
228        /**
229         * Construct a new StreamTwitterStatusList from the given input stream.
230         * @param <T> the type of the USMFStats instances returned
231         * 
232         * @param stream the input stream
233         * @param nTweets of tweets to read from this stream
234         * @param clz the class of local feature to read
235         * @return a new list
236         * @throws IOException if an error occurs reading from the stream
237         */
238        public static <T extends USMFStatus> StreamTwitterStatusList<T> read(BufferedInputStream stream, int nTweets,Class<T> clz) throws IOException {
239                boolean isBinary = false;
240                                
241                //read header
242                int size = nTweets;
243                int headerLength = 0;
244                int recordLength = -1;
245                
246                return new StreamTwitterStatusList<T>(stream, size, isBinary, headerLength, recordLength,clz);
247        }
248        
249        /**
250         * Construct a new StreamTwitterStatusList from the given input stream.
251         * @param <T> the type of status instance returned
252         * 
253         * @param stream the input stream
254         * @param nTweets of tweets to read from this stream
255         * @param clazz the class of local feature to read
256         * @param charset the charset to read with
257         * @return a new list
258         * @throws IOException if an error occurs reading from the stream
259         */
260        public static <T extends USMFStatus> StreamTwitterStatusList<T> read(BufferedInputStream stream, int nTweets,Class<T> clazz, String charset) throws IOException {
261                boolean isBinary = false;
262                                
263                //read header
264                int size = nTweets;
265                int headerLength = 0;
266                int recordLength = -1;
267                
268                return new StreamTwitterStatusList<T>(stream, size, isBinary, headerLength, recordLength,clazz,charset);
269        }
270
271        @Override
272        public void writeASCII(PrintWriter out) throws IOException {
273                for (USMFStatus k : this) {
274                        k.writeASCII(out);
275                        out.println();
276                }
277        }
278
279        @Override
280        public String asciiHeader() {
281                return "";
282        }
283
284        @Override
285        public void writeBinary(DataOutput out) throws IOException {
286                throw new UnsupportedOperationException();
287        }
288
289        @Override
290        public byte[] binaryHeader() {
291                throw new UnsupportedOperationException();
292        }
293        
294}