Hubiquitus Android  0.3
Android client for hubiquitus protocol
HStatus.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Novedia Group 2012.
00003  *
00004  *     This file is part of Hubiquitus.
00005  *
00006  *     Hubiquitus is free software: you can redistribute it and/or modify
00007  *     it under the terms of the GNU General Public License as published by
00008  *     the Free Software Foundation, either version 3 of the License, or
00009  *     (at your option) any later version.
00010  *
00011  *     Hubiquitus is distributed in the hope that it will be useful,
00012  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *     GNU General Public License for more details.
00015  *
00016  *     You should have received a copy of the GNU General Public License
00017  *     along with Hubiquitus.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 package org.hubiquitus.hapi.hStructures;
00021 
00022 import org.json.JSONException;
00023 import org.json.JSONObject;
00024 
00030 public class HStatus implements HJsonObj{
00031 
00032          private JSONObject hstatus = new JSONObject();
00033                   
00034          public HStatus() {};
00035          
00036          public HStatus(JSONObject jsonObj) {
00037                   fromJSON(jsonObj);
00038          }
00039          
00040          public HStatus(ConnectionStatus status ,ConnectionError errorCode ,String errorMsg) {
00041                   setStatus(status);
00042                   setErrorCode(errorCode);
00043                   setErrorMsg(errorMsg);
00044          }
00045 
00046          /* HJsonObj interface */
00047          
00048          public JSONObject toJSON() {
00049                   return hstatus;
00050          }
00051          
00052          public void fromJSON(JSONObject jsonObj) {
00053                   if(jsonObj != null) {
00054                            this.hstatus = jsonObj; 
00055                   } else {
00056                            this.hstatus = new JSONObject();
00057                   }
00058          }
00059          
00060          public String getHType() {
00061                   return "hstatus";
00062          }
00063          
00064          @Override
00065          public String toString() {
00066                   return hstatus.toString();
00067          }
00068          
00074          public boolean equals(HStatus obj) {
00075                   if(obj.getStatus().value() != this.getStatus().value())
00076                            return false;
00077                   if(obj.getErrorCode().value() != this.getErrorCode().value())
00078                            return false;
00079                   if(obj.getErrorMsg() != this.getErrorMsg())
00080                            return false;
00081                   return true;
00082          }
00083          
00084          @Override
00085          public int hashCode() {
00086                   return hstatus.hashCode();
00087          }
00088          
00089          /* Getters & Setters */
00090          
00095          public ConnectionStatus getStatus() {
00096                   ConnectionStatus status;
00097                   try {
00098                            status = ConnectionStatus.constant(hstatus.getInt("status"));
00099                   } catch (Exception e) {
00100                            status = null;                      
00101                   }
00102                   return status;
00103          }
00104 
00105          public void setStatus(ConnectionStatus status) {
00106                   try {
00107                            if(status == null) {
00108                                     hstatus.remove("status");
00109                            } else {
00110                                     hstatus.put("status", status.value());
00111                            }
00112                   } catch (JSONException e) {
00113                   }
00114          }
00115 
00120          public ConnectionError getErrorCode() {
00121                   ConnectionError errorCode;
00122                   try {
00123                            errorCode = ConnectionError.constant(hstatus.getInt("errorCode"));
00124                   } catch (Exception e) {
00125                            errorCode = null;
00126                   }
00127                   return errorCode;
00128          }
00129 
00130          
00131          public void setErrorCode(ConnectionError errorCode) {
00132                   try {
00133                            if(errorCode == null) {
00134                                     hstatus.remove("errorCode");
00135                            } else {
00136                                     hstatus.put("errorCode", errorCode.value());
00137                            }
00138                   } catch (JSONException e) {
00139                   }
00140          }
00141          
00147          public String getErrorMsg() {
00148                   String errorMsg;
00149                   try {
00150                            errorMsg = hstatus.getString("errorMsg");
00151                   } catch (JSONException e) {
00152                            errorMsg = null;
00153                   }
00154                   return errorMsg;
00155          }
00156 
00157          public void setErrorMsg(String errorMsg) {
00158                   try {
00159                            if(errorMsg == null) {
00160                                     hstatus.remove("errorMsg");
00161                            } else {
00162                                     hstatus.put("errorMsg", errorMsg);
00163                            }
00164                   } catch (JSONException e) {
00165                   }
00166          }
00167 }
 All Classes Namespaces Files Functions Variables