OSDN Git Service

Fix dependencies of packages that target earlier releases
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / lastfm / Result.java
1 /*
2  * Copyright (c) 2012, the Last.fm Java Project and Committers All rights
3  * reserved. Redistribution and use of this software in source and binary forms,
4  * with or without modification, are permitted provided that the following
5  * conditions are met: - Redistributions of source code must retain the above
6  * copyright notice, this list of conditions and the following disclaimer. -
7  * Redistributions in binary form must reproduce the above copyright notice,
8  * this list of conditions and the following disclaimer in the documentation
9  * and/or other materials provided with the distribution. THIS SOFTWARE IS
10  * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
11  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
13  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
14  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
16  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
17  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  */
21
22 package com.cyanogenmod.eleven.lastfm;
23
24 import org.w3c.dom.Document;
25
26 /**
27  * The <code>Result</code> class contains the response sent by the server, i.e.
28  * the status (either ok or failed), an error code and message if failed and the
29  * xml response sent by the server.
30  *
31  * @author Janni Kovacs
32  */
33 public class Result {
34
35     public enum Status {
36         OK, FAILED
37     }
38
39     protected Status status;
40
41     protected String errorMessage = null;
42
43     protected int errorCode = -1;
44
45     protected int httpErrorCode = -1;
46
47     protected Document resultDocument;
48
49     /**
50      * @param resultDocument
51      */
52     protected Result(final Document resultDocument) {
53         status = Status.OK;
54         this.resultDocument = resultDocument;
55     }
56
57     /**
58      * @param errorMessage
59      */
60     protected Result(final String errorMessage) {
61         status = Status.FAILED;
62         this.errorMessage = errorMessage;
63     }
64
65     /**
66      * @param resultDocument
67      * @return
68      */
69     static Result createOkResult(final Document resultDocument) {
70         return new Result(resultDocument);
71     }
72
73     /**
74      * @param httpErrorCode
75      * @param errorMessage
76      * @return
77      */
78     static Result createHttpErrorResult(final int httpErrorCode, final String errorMessage) {
79         final Result r = new Result(errorMessage);
80         r.httpErrorCode = httpErrorCode;
81         return r;
82     }
83
84     /**
85      * @param errorCode
86      * @param errorMessage
87      * @return
88      */
89     static Result createRestErrorResult(final int errorCode, final String errorMessage) {
90         final Result r = new Result(errorMessage);
91         r.errorCode = errorCode;
92         return r;
93     }
94
95     /**
96      * Returns if the operation was successful. Same as
97      * <code>getStatus() == Status.OK</code>.
98      *
99      * @return <code>true</code> if the operation was successful
100      */
101     public boolean isSuccessful() {
102         return status == Status.OK;
103     }
104
105     public int getErrorCode() {
106         return errorCode;
107     }
108
109     public int getHttpErrorCode() {
110         return httpErrorCode;
111     }
112
113     public Status getStatus() {
114         return status;
115     }
116
117     public Document getResultDocument() {
118         return resultDocument;
119     }
120
121     public String getErrorMessage() {
122         return errorMessage;
123     }
124
125     public DomElement getContentElement() {
126         if (!isSuccessful()) {
127             return null;
128         }
129         return new DomElement(resultDocument.getDocumentElement()).getChild("*");
130     }
131
132     @Override
133     public String toString() {
134         return "Result[isSuccessful=" + isSuccessful() + ", errorCode=" + errorCode
135                 + ", httpErrorCode=" + httpErrorCode + ", errorMessage=" + errorMessage
136                 + ", status=" + status + "]";
137     }
138 }