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.
22 package com.cyanogenmod.eleven.lastfm;
24 import org.w3c.dom.Document;
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.
31 * @author Janni Kovacs
39 protected Status status;
41 protected String errorMessage = null;
43 protected int errorCode = -1;
45 protected int httpErrorCode = -1;
47 protected Document resultDocument;
50 * @param resultDocument
52 protected Result(final Document resultDocument) {
54 this.resultDocument = resultDocument;
60 protected Result(final String errorMessage) {
61 status = Status.FAILED;
62 this.errorMessage = errorMessage;
66 * @param resultDocument
69 static Result createOkResult(final Document resultDocument) {
70 return new Result(resultDocument);
74 * @param httpErrorCode
78 static Result createHttpErrorResult(final int httpErrorCode, final String errorMessage) {
79 final Result r = new Result(errorMessage);
80 r.httpErrorCode = httpErrorCode;
89 static Result createRestErrorResult(final int errorCode, final String errorMessage) {
90 final Result r = new Result(errorMessage);
91 r.errorCode = errorCode;
96 * Returns if the operation was successful. Same as
97 * <code>getStatus() == Status.OK</code>.
99 * @return <code>true</code> if the operation was successful
101 public boolean isSuccessful() {
102 return status == Status.OK;
105 public int getErrorCode() {
109 public int getHttpErrorCode() {
110 return httpErrorCode;
113 public Status getStatus() {
117 public Document getResultDocument() {
118 return resultDocument;
121 public String getErrorMessage() {
125 public DomElement getContentElement() {
126 if (!isSuccessful()) {
129 return new DomElement(resultDocument.getDocumentElement()).getChild("*");
133 public String toString() {
134 return "Result[isSuccessful=" + isSuccessful() + ", errorCode=" + errorCode
135 + ", httpErrorCode=" + httpErrorCode + ", errorMessage=" + errorMessage
136 + ", status=" + status + "]";