OSDN Git Service

タブを閉じたときにStreamingAPIの更新もストップするようにした
[nt-manager/nt-manager.git] / src / twitter / action / TweetSearchResultGetter.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package twitter.action;
7
8 import java.util.List;
9
10 import javax.xml.bind.Marshaller.Listener;
11
12 import twitter.action.streaming.TweetStreamingListener;
13 import twitter.manage.TweetManager;
14 import twitter4j.Status;
15
16 /**
17  * 指定したワードを含むツイートを取得する
18  * @author nishio
19  */
20 public class TweetSearchResultGetter implements TweetGetter{
21
22     //tweet管理用
23     private TweetManager tweetManager;
24     //検索したいワード
25     private String searchWord;
26     //sinceid
27     private long sinceID;
28
29     /**
30      *
31      * @param tweetManager
32      */
33     public TweetSearchResultGetter(TweetManager tweetManager, String searchWord) {
34         this.tweetManager = tweetManager;
35         this.searchWord = searchWord;
36     }
37
38     /**
39      * 指定したワードのツイートを指定した数だけ取得
40      * @param num
41      * @return
42      */
43     public List<Status> getTweetData(int num) {
44         List<Status> status = tweetManager.getSearchResult(num, searchWord);
45         if( status != null ) {
46             //一番最後のtweetのsinceIDを取得する
47             int lastnum = status.size();
48             if( lastnum > 0 ) {
49                 sinceID = status.get(lastnum - 1).getId();
50             }
51         }
52         return status;
53     }
54
55     /**
56      * 指定したワードのツイートの新しく投稿されたものだけを取得
57      * @param sinceID
58      * @return
59      */
60     public List<Status> getNewTweetData() {
61         List<Status> status = tweetManager.getNewSearchResult(this.sinceID, this.searchWord);
62         if( status != null ) {
63             //一番最後のtweetのsinceIDを取得する
64             int lastnum = status.size();
65             if( lastnum > 0 ) {
66                 sinceID = status.get(lastnum - 1).getId();
67             }
68         }
69         return status;
70     }
71
72         @Override
73         public void setUpdateListener(TweetStreamingListener listener) {
74                 if( listener != null ) {
75                         tweetManager.getStreamManager().setSearchListener(this.searchWord, listener);
76                 }
77         }
78
79         /**
80      * streaming api有効時のアップデートを受け取るlistenerを削除
81      */
82     public void stopUpdateListener() {
83         tweetManager.getStreamManager().stopSearchListener(this.searchWord);
84     }
85
86 }