blob: c630cda3fd768089917b52785ca55eeaf20c06e0 (
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 com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
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) {
try {
JsonObject userObj = obj.getAsJsonObject("user");
String screen_name = userObj.get("screen_name").getAsString();
tweetCount++;
users.add(screen_name);
} catch (JsonParseException 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);
}
}
|