This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
SearchParameters.java
298 lines (267 loc) · 7.82 KB
/
SearchParameters.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.twitter.api;
import java.util.Date;
/**
* Twitter search parameters.
* See specifications: https://dev.twitter.com/docs/api/1.1/get/search/tweets
*
* @author Rosty Kerei
*/
public class SearchParameters {
private String query;
private GeoCode geoCode;
private String lang;
private String locale;
private ResultType resultType;
private Integer count;
private Date untilDate;
private Long sinceId;
private Long maxId;
private boolean includeEntities = true;
/**
* Constructs SearchParameter object
*
* @param query Search keywords
*/
public SearchParameters(String query) {
this.query = query;
}
/**
* Sets optional <code>geocode</code> parameter. Restricts tweets by users located within a given radius
* of the given latitude/longitude.
*
* @param geoCode GeoCode object stuffed with coordinates and radius
* @see GeoCode
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters geoCode(GeoCode geoCode) {
this.geoCode = geoCode;
return this;
}
/**
* Sets optional <code>lang</code> parameter. Restricts tweets to the given language, given by an ISO 639-1 code.
*
* @param lang an ISO 639-1 language code
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters lang(String lang) {
this.lang = lang;
return this;
}
/**
* Sets optional <code>locale</code> parameter. Specify the language of the query you are sending
* (only ja is currently effective).
*
* @param locale locale
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters locale(String locale) {
this.locale = locale;
return this;
}
/**
* Sets optional <code>result_type</code> parameter. Specifies what type of search results. Default is <code>mixed</code>.
*
* @param resultType type of preferred result type
* @return The same SearchParameters for additional configuration.
* @see ResultType
*/
public SearchParameters resultType(ResultType resultType) {
this.resultType = resultType;
return this;
}
/**
* Sets optional <code>count</code> parameter. Restricts the number of tweets to return, up to a maximum of 100.
* Defaults to 15.
*
* @param count number of tweets to return
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters count(int count) {
this.count = count;
return this;
}
/**
* Sets optional <code>until</code> parameter. Restricts search to tweets generated before the given date.
*
* @param untilDate date to search until
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters until(Date untilDate) {
this.untilDate = untilDate;
return this;
}
/**
* Sets optional <code>since_id</code> parameter. Restricts search results with an ID greater than the specified one.
*
* @param sinceId The minimum {@link org.springframework.social.twitter.api.Tweet} ID to return in the results
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters sinceId(long sinceId) {
this.sinceId = sinceId;
return this;
}
/**
* Sets optional <code>max_id</code> parameter. Restricts search results with an ID less or equel than the specified one.
*
* @param maxId The maximum {@link org.springframework.social.twitter.api.Tweet} ID to return in the results
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters maxId(long maxId) {
this.maxId = maxId;
return this;
}
/**
* Sets optional <code>include_entities</code> parameter. The entities node will be excluded when set to false.
*
* @param includeEntities Include entities node
* @return The same SearchParameters for additional configuration.
*/
public SearchParameters includeEntities(boolean includeEntities) {
this.includeEntities = includeEntities;
return this;
}
/**
* Returns query, <code>q</code> parameter
*
* @return query
*/
public String getQuery() {
return this.query;
}
/**
* Returns <code>geo_code</code> search parameter
*
* @return geoCode
*/
public GeoCode getGeoCode() {
return this.geoCode;
}
/**
* Returns <code>lang</code> search parameter
*
* @return lang
*/
public String getLang() {
return this.lang;
}
/**
* Returns <code>locale</code> search parameter
*
* @return locale
*/
public String getLocale() {
return this.locale;
}
/**
* Returns <code>result_type</code> search parameter
*
* @return result_type
*/
public ResultType getResultType() {
return this.resultType;
}
/**
* Returns <code>count</code> search parameter
*
* @return count
*/
public Integer getCount() {
return this.count;
}
/**
* Returns <code>until</code> search parameter
*
* @return until
*/
public Date getUntil() {
return this.untilDate;
}
/**
* Returns <code>since_id</code> search parameter
*
* @return since_id
*/
public Long getSinceId() {
return this.sinceId;
}
/**
* Returns <code>max_id</code> search parameter
*
* @return max_id
*/
public Long getMaxId() {
return this.maxId;
}
/**
* Returns <code>include_entities</code> search parameter
*
* @return include_entities
*/
public boolean isIncludeEntities() {
return this.includeEntities;
}
/**
* ResultType enumeration. Used by setResultType/getResultType
*/
public static enum ResultType {
MIXED("mixed"), RECENT("recent"), POPULAR("popular");
private String resultType;
private ResultType(String resultType) {
this.resultType = resultType;
}
@Override
public String toString() {
return this.resultType;
}
}
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
SearchParameters other = (SearchParameters) o;
return other.query.equals(this.query)
&& other.count == this.count
&& bothNullOrEquals(other.geoCode, this.geoCode)
&& other.includeEntities == this.includeEntities
&& bothNullOrEquals(other.lang, this.lang)
&& bothNullOrEquals(other.locale, this.locale)
&& bothNullOrEquals(other.maxId, this.maxId)
&& bothNullOrEquals(other.resultType, this.resultType)
&& bothNullOrEquals(other.sinceId, this.sinceId)
&& bothNullOrEquals(other.untilDate, this.untilDate);
}
private boolean bothNullOrEquals(Object o1, Object o2) {
return (o1 == null && o2 == null) || (o1 != null && o2 != null && o1.equals(o2));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((query == null) ? 0 : query.hashCode());
result = prime * result + ((count == null) ? 0 : count.hashCode());
result = prime * result + ((sinceId == null) ? 0 : sinceId.hashCode());
result = prime * result + ((maxId == null) ? 0 : maxId.hashCode());
result = prime * result + ((lang == null) ? 0 : lang.hashCode());
result = prime * result + ((geoCode == null) ? 0 : geoCode.hashCode());
result = prime * result + ((locale == null) ? 0 : locale.hashCode());
result = prime * result + ((resultType == null) ? 0 : resultType.hashCode());
result = prime * result + ((untilDate == null) ? 0 : untilDate.hashCode());
result = prime * result + (includeEntities ? 0 : 1);
return result;
}
}