From 31d93f23a08d03b0df37e9cda62f4755be1dbb3f Mon Sep 17 00:00:00 2001 From: Maurice Laveaux Date: Wed, 30 Apr 2014 15:18:19 +0200 Subject: Renamed OutputStream.java because it was part of the Java classes. --- src/io/DataWriter.java | 137 +++++++++++++++++++++++++++++++++++++++++++++++ src/io/OutputStream.java | 130 -------------------------------------------- 2 files changed, 137 insertions(+), 130 deletions(-) create mode 100644 src/io/DataWriter.java delete mode 100644 src/io/OutputStream.java diff --git a/src/io/DataWriter.java b/src/io/DataWriter.java new file mode 100644 index 0000000..900a467 --- /dev/null +++ b/src/io/DataWriter.java @@ -0,0 +1,137 @@ +package io; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashSet; +import java.util.Scanner; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.json.JSONException; +import org.json.JSONObject; +import provider.ProfileListener; +import provider.TweetListener; + +/** + * This class writes the output data into seperate files. + * + * @author Maurice Laveaux + */ +public class DataListener implements ProfileListener, TweetListener { + + /** + * The writer for the tweet stream. + */ + private final FileWriter m_tweetWriter; + + /** + * the writer for the profile stream. + */ + private final FileWriter m_profileWriter; + + /** + * the buffer of tweet ids that already exist. + */ + private final Set m_tweetIdSet; + + /** + * the buffer of profile ids that already exist. + */ + private final Set m_profileIdSet; + + private static final String profilesName = "profiles.txt"; + private static final String tweetsName = "tweets.txt"; + + /** + * Opens a stream to every single file that data will be streamed to. + */ + public DataListener() { + try { + m_profileIdSet = readIds(profilesName); + m_profileWriter = new FileWriter(profilesName, true); + + m_tweetIdSet = readIds(tweetsName); + m_tweetWriter = new FileWriter(tweetsName, true); + } catch (IOException ex) { + // This should not happen. + throw new RuntimeException(ex.getMessage()); + } + } + + @Override + public void profileGenerated(JSONObject obj) { + writeObject(obj, m_profileWriter, m_profileIdSet); + } + + @Override + public void tweetGenerated(JSONObject obj) { + writeObject(obj, m_tweetWriter, m_tweetIdSet); + } + + /** + * Read the current existing tweetName and profileName filenames and fill + * the existing id set, it will create the file when it doesn't exist. + * + * @param filename The file to parse + * @return The set of ids + */ + private Set readIds(String filename) throws IOException { + try { + Scanner reader = new Scanner(new File(filename)); + + // TODO: Read the file JSON objects and parse the ids. + Set idSet = new HashSet(); + + // parse each line into a JSONObject, read the id and add it to + // the set of ids. + while (reader.hasNext()) { + JSONObject obj = new JSONObject(reader.nextLine()); + long id = obj.getLong("id"); + idSet.add(id); + } + + return idSet; + } catch (FileNotFoundException ex) { + // File does not exist, so create one. + File file = new File(filename); + // Return value should always be true. + file.createNewFile(); + } catch (JSONException ex) { + getLogger().log(Level.SEVERE, null, ex); + } + + // return the empty set. + return new HashSet(); + } + + /** + * Writes the JSONObject to a writer and update the idSet. + * + * @param obj The object to write. + * @param writer The writer object to append the object to. + * @param idSet The id set to add the obj id to. + */ + private void writeObject(JSONObject obj, FileWriter writer, Set idSet) { + try { + long id = obj.getLong("id"); + + if (!idSet.contains(id)) { + // Write a single profile into the profile file. + try { + writer.write(obj.toString() + "\n"); + idSet.add(id); + } catch (IOException ex) { + getLogger().log(Level.SEVERE, null, ex); + } + } + } catch (JSONException ex) { + getLogger().log(Level.SEVERE, null, ex); + } + } + + private Logger getLogger() { + return Logger.getLogger(DataListener.class.getName()); + } +} diff --git a/src/io/OutputStream.java b/src/io/OutputStream.java deleted file mode 100644 index 5a92c59..0000000 --- a/src/io/OutputStream.java +++ /dev/null @@ -1,130 +0,0 @@ -package io; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.util.HashSet; -import java.util.Scanner; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.json.JSONException; -import org.json.JSONObject; -import provider.ProfileListener; -import provider.TweetListener; - -/** - * This class writes the output data into seperate files. - * - * @author Maurice Laveaux - */ -public class OutputStream implements ProfileListener, TweetListener { - - /** - * The writer for the tweet stream. - */ - private final FileWriter m_tweetWriter; - - /** - * the writer for the profile stream. - */ - private final FileWriter m_profileWriter; - - /** - * the buffer of tweet ids that already exist. - */ - private final Set m_tweetIdSet; - - /** - * the buffer of profile ids that already exist. - */ - private final Set m_profileIdSet; - - private static final String profilesName = "profiles.txt"; - private static final String tweetsName = "tweets.txt"; - - /** - * Opens a stream to every single file that data will be streamed to. - */ - public OutputStream() { - try { - m_profileIdSet = readIds(profilesName); - m_profileWriter = new FileWriter(profilesName, true); - - m_tweetIdSet = readIds(tweetsName); - m_tweetWriter = new FileWriter(tweetsName, true); - } catch (IOException ex) { - // This should not happen. - throw new RuntimeException(ex.getMessage()); - } - } - - @Override - public void profileGenerated(JSONObject obj) { - writeObject(obj, m_profileWriter, m_profileIdSet); - } - - @Override - public void tweetGenerated(JSONObject obj) { - writeObject(obj, m_tweetWriter, m_tweetIdSet); - } - - /** - * Read the current existing tweetName and profileName filenames and fill - * the existing id set. - * - * @param filename The file to parse - * @return The set of ids - */ - private Set readIds(String filename) throws IOException { - try { - Scanner reader = new Scanner(new File(filename)); - - // TODO: Read the file JSON objects and parse the ids. - Set idSet = new HashSet(); - - // parse each line into a JSONObject, read the id and add it to - // the set of ids. - while(reader.hasNext()) { - JSONObject obj = new JSONObject(reader.nextLine()); - long id = obj.getLong("id"); - idSet.add(id); - } - - return idSet; - } catch (FileNotFoundException ex) { - // File does not exist, so create one. - File file = new File(filename); - // Return value should always be true. - file.createNewFile(); - } catch (JSONException ex) { - getLogger().log(Level.SEVERE, null, ex); - } - - // return the empty set. - return new HashSet(); - } - - private void writeObject(JSONObject obj, FileWriter writer, Set idSet) { - try { - long id = obj.getLong("id"); - - if (!idSet.contains(id)) { - // Write a single profile into the profile file. - try { - writer.write(obj.toString() + "\n"); - idSet.add(id); - } catch (IOException ex) { - getLogger().log(Level.SEVERE, null, ex); - } - } - } catch (JSONException ex) { - getLogger().log(Level.SEVERE, null, ex); - } - } - - private Logger getLogger() { - return Logger.getLogger(OutputStream.class.getName()); - } -} -- cgit v1.2.1