blob: 6c1397afa7645b7010f0a6f74104df079d4926eb (
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package main;
import java.util.Date;
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;
private final Date start_date;
public TweetCounter() {
this.users = new HashSet<>();
this.start_date = new Date();
}
@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;
}
public Date getStartDate() {
return start_date;
}
public String getActiveTime() {
Date now = new Date();
long timediff = (now.getTime() - start_date.getTime()) / 1000;
return String.format("%d hour(s), %d min(s), %d sec(s)",
timediff / 3600,
(timediff % 3600) / 60,
timediff % 60);
}
}
|