add weibo src
[freespace.git] / src / weibo4android / IDs.java
blob564282a258b67d4f64270c04991a9f965f2ce6cb
1 /*
2 Copyright (c) 2007-2009, Yusuke Yamamoto
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Yusuke Yamamoto nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 package weibo4android;
29 import java.util.Arrays;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.NodeList;
34 import weibo4android.http.Response;
35 import weibo4android.org.json.JSONArray;
36 import weibo4android.org.json.JSONException;
37 import weibo4android.org.json.JSONObject;
39 /**
40 * A data class representing array of numeric IDs.
42 * @author Yusuke Yamamoto - yusuke at mac.com
43 * @editor sinaWeibo
45 public class IDs extends WeiboResponse {
46 private long[] ids;
47 private long previousCursor;
48 private long nextCursor;
49 private static final long serialVersionUID = -6585026560164704953L;
50 private static String[] ROOT_NODE_NAMES = {"id_list", "ids"};
52 /*package*/ IDs(Response res) throws WeiboException {
53 super(res);
54 Element elem = res.asDocument().getDocumentElement();
55 ensureRootNodeNameIs(ROOT_NODE_NAMES, elem);
56 NodeList idlist = elem.getElementsByTagName("id");
57 ids = new long[idlist.getLength()];
58 for (int i = 0; i < idlist.getLength(); i++) {
59 try {
60 ids[i] = Long.parseLong(idlist.item(i).getFirstChild().getNodeValue());
61 } catch (NumberFormatException nfe) {
62 throw new WeiboException("Weibo API returned malformed response: " + elem, nfe);
65 previousCursor = getChildLong("previous_cursor", elem);
66 nextCursor = getChildLong("next_cursor", elem);
69 /*package*/ IDs(Response res,Weibo w) throws WeiboException {
70 super(res);
71 if("[]\n".equals(res.asString())){
72 previousCursor=0;
73 nextCursor=0;
74 ids= new long[0];
75 return;
77 JSONObject json= res.asJSONObject();
78 try {
79 previousCursor = json.getLong("previous_cursor");
80 nextCursor = json.getLong("next_cursor");
82 if(!json.isNull("ids")){
83 JSONArray jsona= json.getJSONArray("ids");
84 int size=jsona.length();
85 ids =new long[ size];
86 for (int i = 0; i < size; i++) {
87 ids[i] =jsona.getLong(i);
91 } catch (JSONException jsone) {
92 throw new WeiboException(jsone);
97 public long[] getIDs() {
98 return ids;
103 * @since Weibo4J 1.2.0
105 public boolean hasPrevious(){
106 return 0 != previousCursor;
111 * @since Weibo4J 1.2.0
113 public long getPreviousCursor() {
114 return previousCursor;
119 * @since Weibo4J 1.2.0
121 public boolean hasNext(){
122 return 0 != nextCursor;
127 * @since Weibo4J 1.2.0
129 public long getNextCursor() {
130 return nextCursor;
133 @Override
134 public boolean equals(Object o) {
135 if (this == o) return true;
136 if (!(o instanceof IDs)) return false;
138 IDs iDs = (IDs) o;
140 if (!Arrays.equals(ids, iDs.ids)) return false;
142 return true;
145 @Override
146 public int hashCode() {
147 return ids != null ? Arrays.hashCode(ids) : 0;
150 @Override
151 public String toString() {
152 return "IDs{" +
153 "ids=" + ids +
154 ", previousCursor=" + previousCursor +
155 ", nextCursor=" + nextCursor +
156 '}';