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.File; 035import java.io.FileInputStream; 036import java.io.IOException; 037import java.io.InputStream; 038import java.io.PrintWriter; 039import java.util.ArrayList; 040import java.util.Collection; 041import java.util.Collections; 042import java.util.Scanner; 043 044import org.openimaj.data.RandomData; 045import org.openimaj.io.FileUtils; 046import org.openimaj.twitter.GeneralJSON; 047import org.openimaj.twitter.USMFStatus; 048 049 050/** 051 * A List of {@link USMFStatus} instances held in memory (backed by an {@link ArrayList}.) 052 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 053 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk), 054 * 055 * @param <T> 056 */ 057public class MemoryTwitterStatusList<T extends USMFStatus> extends ArrayList<T> implements TwitterStatusList<T> { 058 059 /** 060 * 061 */ 062 private static final long serialVersionUID = -785707085718120105L; 063 064 /** 065 * Consume a collection into this list 066 * @param c 067 */ 068 @SuppressWarnings("unchecked") 069 public MemoryTwitterStatusList(Collection<T> c) { 070 for (T twitterStatus : c) { 071 this.add(twitterStatus.clone((Class<T>)twitterStatus.getClass())); 072 } 073 } 074 075 /** 076 * an empty list 077 */ 078 public MemoryTwitterStatusList() { 079 } 080 081 @Override 082 public MemoryTwitterStatusList<T> randomSubList(int nelem) { 083 MemoryTwitterStatusList<T> kl; 084 085 if (nelem > size()) { 086 kl = new MemoryTwitterStatusList<T>(this); 087 Collections.shuffle(kl); 088 } else { 089 int [] rnds = RandomData.getUniqueRandomInts(nelem, 0, this.size()); 090 kl = new MemoryTwitterStatusList<T>(); 091 092 for (int idx : rnds) 093 kl.add(this.get(idx)); 094 } 095 096 return kl; 097 } 098 099 @Override 100 public void writeASCII(PrintWriter out) throws IOException { 101 for (int i = 0; i < this.size(); i++) { 102 this.get(i).writeASCII(out); 103 out.println(); 104 } 105 } 106 107 @Override 108 public String asciiHeader() { 109 return ""; 110 } 111 112 /** 113 * @param f 114 * @return read a file of {@link USMFStatus} instances into memory (i.e. formated as USMF) 115 * @throws IOException 116 */ 117 public static MemoryTwitterStatusList<USMFStatus> read(File f) throws IOException { 118 return read(new FileInputStream(f),FileUtils.countLines(f)); 119 } 120 121 /** 122 * @param <T> 123 * @param f 124 * @param clazz 125 * @return read a file as {@link USMFStatus} instances held ass clazz instances 126 * @throws IOException 127 */ 128 public static <T extends USMFStatus>MemoryTwitterStatusList<T> read(File f,Class<T> clazz) throws IOException { 129 return read(new FileInputStream(f),FileUtils.countLines(f),clazz,USMFStatus.class); 130 } 131 /** 132 * @param is 133 * @param nStatus 134 * @return read nStatus from the input stream input memory 135 * @throws IOException 136 */ 137 public static MemoryTwitterStatusList<USMFStatus> read(InputStream is, int nStatus) throws IOException { 138 return read(new BufferedInputStream(is),nStatus,USMFStatus.class,USMFStatus.class); 139 } 140 /** 141 * @param <T> 142 * @param is 143 * @param nStatus 144 * @param clazz 145 * @param readClass 146 * @return read nStatus instances into a {@link MemoryTwitterStatusList} held as clazz instances and read as readClass instances 147 * @throws IOException 148 */ 149 public static <T extends USMFStatus> MemoryTwitterStatusList<T> read(InputStream is, int nStatus,Class<T> clazz,Class<? extends GeneralJSON> readClass) throws IOException { 150 return read(new BufferedInputStream(is),nStatus,clazz,USMFStatus.class); 151 } 152 153 /** 154 * @param <T> 155 * @param is 156 * @param nTweets 157 * @param clazz 158 * @param readClass 159 * @return read nStatus instances into a {@link MemoryTwitterStatusList} held as clazz instances and read as readClass instances 160 * @throws IOException 161 */ 162 public static <T extends USMFStatus> MemoryTwitterStatusList<T> read(BufferedInputStream is, int nTweets,Class<T> clazz,Class<? extends GeneralJSON> readClass) throws IOException { 163 MemoryTwitterStatusList<T> list = new MemoryTwitterStatusList<T>(); 164 Scanner scanner = new Scanner(is); 165 for (int i = 0; i < nTweets; i++) { 166 T s = TwitterStatusListUtils.newInstance(clazz); 167 s.setGeneralJSONClass(readClass); 168 s.readASCII(scanner); 169 list.add(s); 170 } 171 172 return list; 173 } 174 175 176 @Override 177 public void writeBinary(DataOutput out) throws IOException { 178 throw new UnsupportedOperationException(); 179 180 } 181 182 @Override 183 public byte[] binaryHeader() { 184 throw new UnsupportedOperationException(); 185 } 186 187}