
twitter4j.DirectMessageJSONImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twitter4j-core Show documentation
Show all versions of twitter4j-core Show documentation
A Java library for the Twitter API
The newest version!
/*
Copyright (c) 2007-2009, Yusuke Yamamoto
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Yusuke Yamamoto nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package twitter4j;
import twitter4j.http.HttpResponse;
import twitter4j.org.json.JSONArray;
import twitter4j.org.json.JSONException;
import twitter4j.org.json.JSONObject;
import java.util.Date;
import static twitter4j.ParseUtil.*;
/**
* 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 int id;
private String text;
private int senderId;
private int recipientId;
private Date createdAt;
private String senderScreenName;
private String recipientScreenName;
private static final long serialVersionUID = -3253021825891789737L;
/*package*/DirectMessageJSONImpl(HttpResponse res) throws TwitterException {
super(res);
init(res.asJSONObject());
}
/*package*/DirectMessageJSONImpl(JSONObject json) throws TwitterException {
init(json);
}
private void init(JSONObject json) throws TwitterException{
id = getInt("id", json);
text = getUnescapedString("text", json);
senderId = getInt("sender_id", json);
recipientId = getInt("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}
*/
public int getId() {
return id;
}
/**
* {@inheritDoc}
*/
public String getText() {
return text;
}
/**
* {@inheritDoc}
*/
public int getSenderId() {
return senderId;
}
/**
* {@inheritDoc}
*/
public int getRecipientId() {
return recipientId;
}
/**
* {@inheritDoc}
*/
public Date getCreatedAt() {
return createdAt;
}
/**
* {@inheritDoc}
*/
public String getSenderScreenName() {
return senderScreenName;
}
/**
* {@inheritDoc}
*/
public String getRecipientScreenName() {
return recipientScreenName;
}
private User sender;
/**
* {@inheritDoc}
*/
public User getSender() {
return sender;
}
private User recipient;
/**
* {@inheritDoc}
*/
public User getRecipient() {
return recipient;
}
/*package*/ static ResponseList createDirectMessageList(HttpResponse res) throws TwitterException {
try {
JSONArray list = res.asJSONArray();
int size = list.length();
ResponseList directMessages = new ResponseList(size, res);
for (int i = 0; i < size; i++) {
directMessages.add(new DirectMessageJSONImpl(list.getJSONObject(i)));
}
return directMessages;
} catch (JSONException jsone) {
throw new TwitterException(jsone);
} catch (TwitterException te) {
throw te;
}
}
@Override
public int hashCode() {
return 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 +
'}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy