1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.common.rendering.api; 18 19 /** 20 * Scene result class. This is an immutable class. 21 * <p/> 22 * This cannot be allocated directly, instead use 23 * {@link Status#createResult()}, 24 * {@link Status#createResult(String, Throwable)}, 25 * {@link Status#createResult(String)} 26 * {@link Status#createResult(Object)} 27 */ 28 public class Result { 29 30 private final Status mStatus; 31 private final String mErrorMessage; 32 private final Throwable mThrowable; 33 private Object mData; 34 35 /** 36 * Scene Status enum. 37 * <p/>This indicates the status of all scene actions. 38 */ 39 public enum Status { 40 SUCCESS, 41 NOT_IMPLEMENTED, 42 ERROR_TIMEOUT, 43 ERROR_LOCK_INTERRUPTED, 44 ERROR_INFLATION, 45 ERROR_VIEWGROUP_NO_CHILDREN, 46 ERROR_NOT_INFLATED, 47 ERROR_RENDER, 48 ERROR_ANIM_NOT_FOUND, 49 ERROR_NOT_A_DRAWABLE, 50 ERROR_REFLECTION, 51 ERROR_UNKNOWN; 52 53 private Result mResult; 54 55 /** 56 * Returns a {@link Result} object with this status. 57 * @return an instance of SceneResult; 58 */ createResult()59 public Result createResult() { 60 // don't want to get generic error that way. 61 assert this != ERROR_UNKNOWN; 62 63 if (mResult == null) { 64 mResult = new Result(this); 65 } 66 67 return mResult; 68 } 69 70 /** 71 * Returns a {@link Result} object with this status, and the given data. 72 * @return an instance of SceneResult; 73 * 74 * @see Result#getData() 75 */ createResult(Object data)76 public Result createResult(Object data) { 77 Result res = createResult(); 78 79 if (data != null) { 80 res = res.getCopyWithData(data); 81 } 82 83 return res; 84 } 85 86 /** 87 * Returns a {@link #ERROR_UNKNOWN} result with the given message and throwable 88 * @param errorMessage the error message 89 * @param throwable the throwable 90 * @return an instance of SceneResult. 91 */ createResult(String errorMessage, Throwable throwable)92 public Result createResult(String errorMessage, Throwable throwable) { 93 return new Result(this, errorMessage, throwable); 94 } 95 96 /** 97 * Returns a {@link #ERROR_UNKNOWN} result with the given message 98 * @param errorMessage the error message 99 * @return an instance of SceneResult. 100 */ createResult(String errorMessage)101 public Result createResult(String errorMessage) { 102 return new Result(this, errorMessage, null /*throwable*/); 103 } 104 } 105 106 /** 107 * Creates a {@link Result} object with the given SceneStatus. 108 * 109 * @param status the status. Must not be null. 110 */ Result(Status status)111 private Result(Status status) { 112 this(status, null, null); 113 } 114 115 /** 116 * Creates a {@link Result} object with the given SceneStatus, and the given message 117 * and {@link Throwable} 118 * 119 * @param status the status. Must not be null. 120 * @param errorMessage an optional error message. 121 * @param t an optional exception. 122 */ Result(Status status, String errorMessage, Throwable t)123 private Result(Status status, String errorMessage, Throwable t) { 124 assert status != null; 125 mStatus = status; 126 mErrorMessage = errorMessage; 127 mThrowable = t; 128 } 129 Result(Result result)130 private Result(Result result) { 131 mStatus = result.mStatus; 132 mErrorMessage = result.mErrorMessage; 133 mThrowable = result.mThrowable; 134 } 135 136 /** 137 * Returns a copy of the current result with the added (or replaced) given data 138 * @param data the data bundle 139 * 140 * @return returns a new SceneResult instance. 141 */ getCopyWithData(Object data)142 public Result getCopyWithData(Object data) { 143 Result r = new Result(this); 144 r.mData = data; 145 return r; 146 } 147 148 149 /** 150 * Returns whether the status is successful. 151 * <p> 152 * This is the same as calling <code>getStatus() == SceneStatus.SUCCESS</code> 153 * @return <code>true</code> if the status is successful. 154 */ isSuccess()155 public boolean isSuccess() { 156 return mStatus == Status.SUCCESS; 157 } 158 159 /** 160 * Returns the status. This is never null. 161 */ getStatus()162 public Status getStatus() { 163 return mStatus; 164 } 165 166 /** 167 * Returns the error message. This is only non-null when {@link #getStatus()} returns 168 * {@link Status#ERROR_UNKNOWN} 169 */ getErrorMessage()170 public String getErrorMessage() { 171 return mErrorMessage; 172 } 173 174 /** 175 * Returns the exception. This is only non-null when {@link #getStatus()} returns 176 * {@link Status#ERROR_UNKNOWN} 177 */ getException()178 public Throwable getException() { 179 return mThrowable; 180 } 181 182 /** 183 * Returns the optional data bundle stored in the result object. 184 * @return the data bundle or <code>null</code> if none have been set. 185 */ getData()186 public Object getData() { 187 return mData; 188 } 189 } 190