This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
forked from Twitter4J/Twitter4J
/
GeoQuery.java
195 lines (165 loc) · 6.25 KB
/
GeoQuery.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
/*
* Copyright 2007 Yusuke Yamamoto
*
* 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 twitter4j;
import twitter4j.internal.http.HttpParameter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Yusuke Yamamoto - yusuke at mac.com
* @since Twitter4J 2.1.1
*/
public final class GeoQuery implements java.io.Serializable {
private GeoLocation location;
private String ip;
private String accuracy;
private String granularity;
private int maxResults;
public static final String NEIGHBORHOOD = "neighborhood";
public static final String CITY = "city";
private static final long serialVersionUID = 927081526936169802L;
/**
* Creates a GeoQuery with the specified location
*
* @param location
*/
public GeoQuery(GeoLocation location) {
this.location = location;
this.ip = null;
}
/**
* Creates a GeoQuery with the specified IP address
*
* @param ip IP address
*/
public GeoQuery(String ip) {
this.ip = ip;
this.location = null;
}
public GeoLocation getLocation() {
return location;
}
public String getIp() {
return ip;
}
public String getAccuracy() {
return accuracy;
}
/**
* Sets a hint on the "region" in which to search. If a number, then this is a radius in meters, but it can also take a string that is suffixed with ft to specify feet. If this is not passed in, then it is assumed to be 0m. If coming from a device, in practice, this value is whatever accuracy the device has measuring its location (whether it be coming from a GPS, WiFi triangulation, etc.).
*
* @param accuracy a hint on the "region" in which to search.
*/
public void setAccuracy(String accuracy) {
this.accuracy = accuracy;
}
public GeoQuery accuracy(String accuracy) {
setAccuracy(accuracy);
return this;
}
public String getGranularity() {
return granularity;
}
/**
* Sets the minimal granularity of data to return. If this is not passed in, then neighborhood is assumed. city can also be passed.
*
* @param granularity the minimal granularity of data to return
*/
public void setGranularity(String granularity) {
this.granularity = granularity;
}
public GeoQuery granularity(String granularity) {
setGranularity(granularity);
return this;
}
public int getMaxResults() {
return maxResults;
}
/**
* Sets a hint as to the number of results to return. This does not guarantee that the number of results returned will equal max_results, but instead informs how many "nearby" results to return. Ideally, only pass in the number of places you intend to display to the user here.
*
* @param maxResults A hint as to the number of results to return.
*/
public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
public GeoQuery maxResults(int maxResults) {
setMaxResults(maxResults);
return this;
}
/*package*/ HttpParameter[] asHttpParameterArray() {
ArrayList<HttpParameter> params = new ArrayList<HttpParameter>();
if (location != null) {
appendParameter("lat", location.getLatitude(), params);
appendParameter("long", location.getLongitude(), params);
}
if (ip != null) {
appendParameter("ip", ip, params);
}
appendParameter("accuracy", accuracy, params);
appendParameter("granularity", granularity, params);
appendParameter("max_results", maxResults, params);
HttpParameter[] paramArray = new HttpParameter[params.size()];
return params.toArray(paramArray);
}
private void appendParameter(String name, String value, List<HttpParameter> params) {
if (value != null) {
params.add(new HttpParameter(name, value));
}
}
private void appendParameter(String name, int value, List<HttpParameter> params) {
if (0 < value) {
params.add(new HttpParameter(name, String.valueOf(value)));
}
}
private void appendParameter(String name, double value, List<HttpParameter> params) {
params.add(new HttpParameter(name, String.valueOf(value)));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GeoQuery geoQuery = (GeoQuery) o;
if (maxResults != geoQuery.maxResults) return false;
if (accuracy != null ? !accuracy.equals(geoQuery.accuracy) : geoQuery.accuracy != null)
return false;
if (granularity != null ? !granularity.equals(geoQuery.granularity) : geoQuery.granularity != null)
return false;
if (ip != null ? !ip.equals(geoQuery.ip) : geoQuery.ip != null)
return false;
if (location != null ? !location.equals(geoQuery.location) : geoQuery.location != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = location != null ? location.hashCode() : 0;
result = 31 * result + (ip != null ? ip.hashCode() : 0);
result = 31 * result + (accuracy != null ? accuracy.hashCode() : 0);
result = 31 * result + (granularity != null ? granularity.hashCode() : 0);
result = 31 * result + maxResults;
return result;
}
@Override
public String toString() {
return "GeoQuery{" +
"location=" + location +
", ip='" + ip + '\'' +
", accuracy='" + accuracy + '\'' +
", granularity='" + granularity + '\'' +
", maxResults=" + maxResults +
'}';
}
}