summaryrefslogtreecommitdiff
path: root/src/main/TweetCounter.java
blob: e922c5c374e3fc482e5be040b1f9c7649ca9180f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import provider.ResultListener;

/**
 * Calculates statistic about the received tweets.
 *
 * @author Peter Wu
 */
public class TweetCounter implements ResultListener {

    private final static Logger LOGGER
            = Logger.getLogger(TweetCounter.class.getName());

    private int tweetCount = 0;
    private final Set<String> users;

    public TweetCounter() {
        this.users = new HashSet<>();
    }

    @Override
    public void tweetGenerated(JSONObject obj) {
        tweetCount++;
        try {
            JSONObject userObj = obj.getJSONObject("user");
            String screen_name = userObj.getString("screen_name");
            users.add(screen_name);
        } catch (JSONException ex) {
            LOGGER.log(Level.WARNING, "Profile is missing data", ex);
        }
    }

    public int getTweetCount() {
        return tweetCount;
    }

    /**
     * @return The set of users who tweeted. Do not modify its contents!
     */
    public Set<String> getUsers() {
        return users;
    }
}