/
DirectMessageJSONImpl.java
213 lines (189 loc) · 5.87 KB
/
DirectMessageJSONImpl.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
/*
* 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.internal.json;
import twitter4j.DirectMessage;
import twitter4j.ResponseList;
import twitter4j.TwitterException;
import twitter4j.User;
import twitter4j.conf.Configuration;
import twitter4j.internal.http.HttpResponse;
import twitter4j.internal.org.json.JSONArray;
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;
import java.util.Date;
import static twitter4j.internal.json.z_T4JInternalParseUtil.*;
/**
* A data class representing sent/received direct message.
*
* @author Yusuke Yamamoto - yusuke at mac.com
*/
/*package*/ final class DirectMessageJSONImpl extends TwitterResponseImpl implements DirectMessage, java.io.Serializable {
private static final long serialVersionUID = -7104233663827757577L;
private long id;
private String text;
private long senderId;
private long recipientId;
private Date createdAt;
private String senderScreenName;
private String recipientScreenName;
/*package*/DirectMessageJSONImpl(HttpResponse res, Configuration conf) throws TwitterException {
super(res);
JSONObject json = res.asJSONObject();
init(json);
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.clearThreadLocalMap();
DataObjectFactoryUtil.registerJSONObject(this, json);
}
}
/*package*/DirectMessageJSONImpl(JSONObject json) throws TwitterException {
init(json);
}
private void init(JSONObject json) throws TwitterException {
id = getLong("id", json);
text = getUnescapedString("text", json);
senderId = getLong("sender_id", json);
recipientId = getLong("recipient_id", json);
createdAt = getDate("created_at", json);
senderScreenName = getUnescapedString("sender_screen_name", json);
recipientScreenName = getUnescapedString("recipient_screen_name", json);
try {
sender = new UserJSONImpl(json.getJSONObject("sender"));
recipient = new UserJSONImpl(json.getJSONObject("recipient"));
} catch (JSONException jsone) {
throw new TwitterException(jsone);
}
}
/**
* {@inheritDoc}
*/
@Override
public long getId() {
return id;
}
/**
* {@inheritDoc}
*/
@Override
public String getText() {
return text;
}
/**
* {@inheritDoc}
*/
@Override
public long getSenderId() {
return senderId;
}
/**
* {@inheritDoc}
*/
@Override
public long getRecipientId() {
return recipientId;
}
/**
* {@inheritDoc}
*/
@Override
public Date getCreatedAt() {
return createdAt;
}
/**
* {@inheritDoc}
*/
@Override
public String getSenderScreenName() {
return senderScreenName;
}
/**
* {@inheritDoc}
*/
@Override
public String getRecipientScreenName() {
return recipientScreenName;
}
private User sender;
/**
* {@inheritDoc}
*/
@Override
public User getSender() {
return sender;
}
private User recipient;
/**
* {@inheritDoc}
*/
@Override
public User getRecipient() {
return recipient;
}
/*package*/
static ResponseList<DirectMessage> createDirectMessageList(HttpResponse res, Configuration conf) throws TwitterException {
try {
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.clearThreadLocalMap();
}
JSONArray list = res.asJSONArray();
int size = list.length();
ResponseList<DirectMessage> directMessages = new ResponseListImpl<DirectMessage>(size, res);
for (int i = 0; i < size; i++) {
JSONObject json = list.getJSONObject(i);
DirectMessage directMessage = new DirectMessageJSONImpl(json);
directMessages.add(directMessage);
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.registerJSONObject(directMessage, json);
}
}
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.registerJSONObject(directMessages, list);
}
return directMessages;
} catch (JSONException jsone) {
throw new TwitterException(jsone);
} catch (TwitterException te) {
throw te;
}
}
@Override
public int hashCode() {
return (int) id;
}
@Override
public boolean equals(Object obj) {
if (null == obj) {
return false;
}
if (this == obj) {
return true;
}
return obj instanceof DirectMessage && ((DirectMessage) obj).getId() == this.id;
}
@Override
public String toString() {
return "DirectMessageJSONImpl{" +
"id=" + id +
", text='" + text + '\'' +
", sender_id=" + senderId +
", recipient_id=" + recipientId +
", created_at=" + createdAt +
", sender_screen_name='" + senderScreenName + '\'' +
", recipient_screen_name='" + recipientScreenName + '\'' +
", sender=" + sender +
", recipient=" + recipient +
'}';
}
}