Hubiquitus Android  0.3
Android client for hubiquitus protocol
HOptions.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 java.util.ArrayList;
00023 import java.util.List;
00024 
00025 import org.json.JSONArray;
00026 import org.json.JSONException;
00027 import org.json.JSONObject;
00028 
00029 
00035 public class HOptions implements Cloneable {
00036          
00037          private String serverHost = null;
00038          private int serverPort = 5222;
00039          private String transport = "xmpp";
00040          private List<String> endpoints = null;
00041          private int nbLastMessage = 10;
00042          private String hserver = "hnode";
00043          
00044          public HOptions() {
00045                   setServerHost(null);
00046                   setEndpoints(null);
00047          }
00048          
00049          public HOptions(JSONObject jsonObj) {
00050                   setEndpoints(null);
00051                   try {
00052                            if (jsonObj.has("serverHost")) {
00053                                     setServerHost(jsonObj.getString("serverHost"));
00054                            }
00055                            
00056                            if (jsonObj.has("serverPort") && !jsonObj.getString("serverPort").equals("")) {
00057                                     setServerPort(jsonObj.getInt("serverPort"));
00058                            }
00059                            
00060                            if (jsonObj.has("transport")) {
00061                                     setTransport(jsonObj.getString("transport"));
00062                            }
00063                            
00064                            if (jsonObj.has("endpoints")) {
00065                                     JSONArray jsonEndpoints = jsonObj.getJSONArray("endpoints");
00066                                     ArrayList<String> arrayEndpoints = new ArrayList<String>();
00067                                     for (int i = 0; i < jsonEndpoints.length(); i++) {
00068                                              arrayEndpoints.add(jsonEndpoints.getString(i));
00069                                     }
00070                                     setEndpoints(arrayEndpoints);
00071                            }
00072                            
00073                            if (jsonObj.has("nbLastMessage")) {
00074                                     setNbLastMessage(jsonObj.getInt("nbLastMessage"));
00075                            }
00076                            
00077                   } catch (JSONException e) {
00078                            e.printStackTrace();
00079                   }
00080          }
00081          
00082          public HOptions(HOptions options) {
00083                   this.setServerHost(options.getServerHost());
00084                   this.setServerPort(options.serverPort);
00085                   this.setEndpoints(options.getEndpoints());
00086                   this.setTransport(options.getTransport());
00087                   this.setNbLastMessage(options.getNbLastMessage());
00088          }
00089          
00090          /* Getters & Setters */
00091          
00095          public String getServerHost() {
00096                   return serverHost;
00097          }
00098 
00099          public void setServerHost(String serverHost) {
00100                   if (serverHost == null || serverHost.equals("")) {
00101                            serverHost = null;
00102                   } else {
00103                            this.serverHost = serverHost;
00104                   }
00105          }
00106 
00110          public int getServerPort() {
00111                   return serverPort;
00112          }
00113 
00114          public void setServerPort(int serverPort) {
00115                   if(serverPort == 0)
00116                            this.serverPort = 5222;
00117                   else
00118                            this.serverPort = serverPort;
00119          }
00120          
00121          public void setServerPort(String serverPort) {
00122                   try {
00123                            this.serverPort = Integer.valueOf(serverPort);
00124                   } catch (Exception e) {
00125                            setServerPort(0);
00126                   }
00127          }
00128          
00132          public String getTransport() {
00133                   return transport;
00134          }        
00135 
00136          public void setTransport(String transport) {
00137                   if(transport.equals("xmpp") || transport.equals("socketio")) {
00138                            this.transport = transport;
00139                   } else {
00140                            this.transport = "xmpp";
00141                   }
00142          }
00143 
00148          public List<String> getEndpoints() {
00149                   return new ArrayList<String>(this.endpoints);
00150          }
00151 
00152          public void setEndpoints(List<String> endpoints) {
00153                   if(endpoints != null && endpoints.size() > 0)
00154                            this.endpoints = new ArrayList<String>(endpoints);
00155                   else {
00156                            this.endpoints = new ArrayList<String>();
00157                            this.endpoints.add("http://localhost:8080/");
00158                   }        
00159          }
00160 
00164          public int getNbLastMessage() {
00165                   return nbLastMessage;
00166          }
00167 
00168          public void setNbLastMessage(int nbLastMessage) {
00169                   if(nbLastMessage >=1)
00170                            this.nbLastMessage = nbLastMessage;
00171                   else
00172                            this.nbLastMessage = 10;
00173          }
00174 
00175          public String getHserver() {
00176                   return hserver;
00177          }
00178 
00179          public void setHserver(String hserver) {
00180                   this.hserver = hserver;
00181          }
00182 
00183          /* overrides */
00184          
00185          @Override
00186          public int hashCode() {
00187                   final int prime = 31;
00188                   int result = 1;
00189                   result = prime * result
00190                                     + ((endpoints == null) ? 0 : endpoints.hashCode());
00191                   result = prime * result + ((hserver == null) ? 0 : hserver.hashCode());
00192                   result = prime * result + nbLastMessage;
00193                   result = prime * result
00194                                     + ((serverHost == null) ? 0 : serverHost.hashCode());
00195                   result = prime * result + serverPort;
00196                   result = prime * result
00197                                     + ((transport == null) ? 0 : transport.hashCode());
00198                   return result;
00199          }
00200 
00201          @Override
00202          public String toString() {
00203                   return "HOptions [serverHost=" + serverHost + ", serverPort="
00204                                     + serverPort + ", transport=" + transport + ", endpoints="
00205                                     + endpoints + ", nbLastMessage=" + nbLastMessage + ", hserver="
00206                                     + hserver + "]";
00207          }
00208 
00209          @Override
00210          public boolean equals(Object obj) {
00211                   if (this == obj)
00212                            return true;
00213                   if (obj == null)
00214                            return false;
00215                   if (getClass() != obj.getClass())
00216                            return false;
00217                   HOptions other = (HOptions) obj;
00218                   if (endpoints == null) {
00219                            if (other.endpoints != null)
00220                                     return false;
00221                   } else if (!endpoints.equals(other.endpoints))
00222                            return false;
00223                   if (hserver == null) {
00224                            if (other.hserver != null)
00225                                     return false;
00226                   } else if (!hserver.equals(other.hserver))
00227                            return false;
00228                   if (nbLastMessage != other.nbLastMessage)
00229                            return false;
00230                   if (serverHost == null) {
00231                            if (other.serverHost != null)
00232                                     return false;
00233                   } else if (!serverHost.equals(other.serverHost))
00234                            return false;
00235                   if (serverPort != other.serverPort)
00236                            return false;
00237                   if (transport == null) {
00238                            if (other.transport != null)
00239                                     return false;
00240                   } else if (!transport.equals(other.transport))
00241                            return false;
00242                   return true;
00243          }
00244 }
 All Classes Namespaces Files Functions Variables