001package org.openimaj.picslurper; 002 003import java.io.DataInput; 004import java.io.DataOutput; 005import java.io.IOException; 006import java.io.PrintWriter; 007import java.net.URL; 008import java.util.ArrayList; 009import java.util.List; 010import java.util.Scanner; 011 012import org.openimaj.io.ReadWriteable; 013 014/** 015 * Statistics about status consumption 016 * 017 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 018 * 019 */ 020public class StatusConsumption implements ReadWriteable { 021 022 /** 023 * The consume image urls 024 */ 025 public List<URL> imageURLs = new ArrayList<URL>(); 026 027 /** 028 * number of urls consumed 029 */ 030 public int nURLs; 031 /** 032 * number of images consumed 033 */ 034 public int nImages; 035 /** 036 * number of tweets consumed 037 */ 038 public int nTweets; 039 040 @Override 041 public void readASCII(Scanner in) throws IOException { 042 in.next(); 043 nURLs = in.nextInt(); 044 in.next(); 045 nImages = in.nextInt(); 046 // Now read how many image urls were saved (on the same line) 047 final int savedURLs = in.nextInt(); 048 in.next(); 049 nTweets = in.nextInt(); 050 in.nextLine(); // complete the line 051 this.imageURLs = new ArrayList<URL>(); 052 for (int i = 0; i < savedURLs; i++) { 053 this.imageURLs.add(new URL(in.nextLine())); 054 } 055 } 056 057 @Override 058 public String asciiHeader() { 059 return ""; 060 } 061 062 @Override 063 public void writeASCII(PrintWriter out) throws IOException { 064 out.printf("nURLS: %d\n", nURLs); 065 out.printf("nImages: %d %d\n", nImages, this.imageURLs.size()); 066 out.printf("nTweets: %d\n", nTweets); 067 for (final URL url : this.imageURLs) { 068 out.println(url); 069 } 070 } 071 072 /** 073 * @param other 074 * add two {@link StatusConsumption} instances 075 */ 076 public void incr(StatusConsumption other) { 077 this.nImages += other.nImages; 078 this.nURLs += other.nURLs; 079 this.nTweets += other.nTweets; 080 this.imageURLs.addAll(other.imageURLs); 081 } 082 083 @Override 084 public void readBinary(DataInput in) throws IOException { 085 this.nURLs = in.readInt(); 086 this.nImages = in.readInt(); 087 final int savedURLs = in.readInt(); 088 this.nTweets = in.readInt(); 089 for (int i = 0; i < savedURLs; i++) { 090 this.imageURLs.add(new URL(in.readUTF())); 091 } 092 } 093 094 @Override 095 public byte[] binaryHeader() { 096 return "BSTAT".getBytes(); 097 } 098 099 @Override 100 public void writeBinary(DataOutput out) throws IOException { 101 out.writeInt(nURLs); 102 out.writeInt(nImages); 103 out.writeInt(this.imageURLs.size()); 104 out.writeInt(nTweets); 105 for (final URL url : this.imageURLs) { 106 out.writeUTF(url.toString()); 107 } 108 } 109}