Response.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.node.response;
00018 
00019 import com.google.common.collect.Lists;
00020 
00021 import org.ros.exception.RemoteException;
00022 import org.ros.exception.RosRuntimeException;
00023 
00024 import java.util.List;
00025 
00033 public class Response<T> {
00034 
00035   private final StatusCode statusCode;
00036   private final String statusMessage;
00037   private final T result;
00038 
00039   public static <T> Response<T> newError(String message, T value) {
00040     return new Response<T>(StatusCode.ERROR, message, value);
00041   }
00042 
00043   public static <T> Response<T> newFailure(String message, T value) {
00044     return new Response<T>(StatusCode.FAILURE, message, value);
00045   }
00046 
00047   public static <T> Response<T> newSuccess(String message, T value) {
00048     return new Response<T>(StatusCode.SUCCESS, message, value);
00049   }
00050 
00068   public static <T> Response<T> fromListCheckedFailure(List<Object> response,
00069       ResultFactory<T> resultFactory) throws RemoteException {
00070     StatusCode statusCode;
00071     String message;
00072     try {
00073       statusCode = StatusCode.fromInt((Integer) response.get(0));
00074       message = (String) response.get(1);
00075       if (statusCode == StatusCode.FAILURE) {
00076         throw new RemoteException(statusCode, message);
00077       }
00078 
00079     } catch (ClassCastException e) {
00080       throw new RosRuntimeException(
00081           "Remote side did not return correct type (status code/message).", e);
00082     }
00083     try {
00084       return new Response<T>(statusCode, message, resultFactory.newFromValue(response.get(2)));
00085     } catch (ClassCastException e) {
00086       throw new RosRuntimeException("Remote side did not return correct value type.", e);
00087     }
00088   }
00089 
00107   public static <T> Response<T> fromListChecked(List<Object> response,
00108       ResultFactory<T> resultFactory) throws RemoteException {
00109     StatusCode statusCode;
00110     String message;
00111     try {
00112       statusCode = StatusCode.fromInt((Integer) response.get(0));
00113       message = (String) response.get(1);
00114       if (statusCode != StatusCode.SUCCESS) {
00115         throw new RemoteException(statusCode, message);
00116       }
00117     } catch (ClassCastException e) {
00118       throw new RosRuntimeException(
00119           "Remote side did not return correct type (status code/message).", e);
00120     }
00121     try {
00122       return new Response<T>(statusCode, message, resultFactory.newFromValue(response.get(2)));
00123     } catch (ClassCastException e) {
00124       throw new RosRuntimeException("Remote side did not return correct value type.", e);
00125     }
00126   }
00127 
00128   public Response(int statusCode, String statusMessage, T value) {
00129     this(StatusCode.fromInt(statusCode), statusMessage, value);
00130   }
00131 
00132   public Response(StatusCode statusCode, String statusMessage, T value) {
00133     this.statusCode = statusCode;
00134     this.statusMessage = statusMessage;
00135     this.result = value;
00136   }
00137 
00138   public List<Object> toList() {
00139     return Lists.newArrayList(statusCode.toInt(), statusMessage, result == null ? "null" : result);
00140   }
00141 
00142   public StatusCode getStatusCode() {
00143     return statusCode;
00144   }
00145 
00146   public String getStatusMessage() {
00147     return statusMessage;
00148   }
00149 
00150   public T getResult() {
00151     return result;
00152   }
00153 
00154   @Override
00155   public String toString() {
00156     return "Response<" + statusCode + ", " + statusMessage + ", " + result + ">";
00157   }
00158 
00159   public boolean isSuccess() {
00160     return statusCode == StatusCode.SUCCESS;
00161   }
00162 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49