Java tutorial
/** * Copyright 2013 Philip Koshy Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * Copyright 2013 Philip Koshy Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package org.adminmap.core.ubuntu; import java.util.Arrays; import org.adminmap.core.rpc.JSONizer; import org.json.JSONArray; import org.json.JSONObject; public class NetworkConfiguration extends JSONizer { public String ipAddress; public String subnetMask; public String broadcastAddress; public String defaultGateway; public String[] nameservers; public NetworkConfiguration() { this.nameservers = null; } public NetworkConfiguration(String json) { fromJSONString(json); } @Override public void fromJSONString(String json) { JSONObject jsonObject = new JSONObject(json); this.ipAddress = jsonObject.getString("ipAddress"); this.subnetMask = jsonObject.getString("subnetMask"); this.broadcastAddress = jsonObject.getString("broadcastAddress"); this.defaultGateway = jsonObject.getString("defaultGateway"); JSONArray jsonArray = jsonObject.getJSONArray("nameservers"); this.nameservers = new String[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) this.nameservers[i] = (jsonArray.getString(i)); } // end of fromJSONString() @Override public String toJSONString() { JSONObject jsonObject = new JSONObject(); jsonObject.put("ipAddress", ipAddress); jsonObject.put("subnetMask", subnetMask); jsonObject.put("broadcastAddress", broadcastAddress); jsonObject.put("defaultGateway", defaultGateway); jsonObject.put("nameservers", nameservers); return jsonObject.toString(); } // end of toJSONString() @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((broadcastAddress == null) ? 0 : broadcastAddress.hashCode()); result = prime * result + ((defaultGateway == null) ? 0 : defaultGateway.hashCode()); result = prime * result + ((ipAddress == null) ? 0 : ipAddress.hashCode()); result = prime * result + Arrays.hashCode(nameservers); result = prime * result + ((subnetMask == null) ? 0 : subnetMask.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof NetworkConfiguration)) { return false; } NetworkConfiguration other = (NetworkConfiguration) obj; if (broadcastAddress == null) { if (other.broadcastAddress != null) { return false; } } else if (!broadcastAddress.equals(other.broadcastAddress)) { return false; } if (defaultGateway == null) { if (other.defaultGateway != null) { return false; } } else if (!defaultGateway.equals(other.defaultGateway)) { return false; } if (ipAddress == null) { if (other.ipAddress != null) { return false; } } else if (!ipAddress.equals(other.ipAddress)) { return false; } if (!Arrays.equals(nameservers, other.nameservers)) { return false; } if (subnetMask == null) { if (other.subnetMask != null) { return false; } } else if (!subnetMask.equals(other.subnetMask)) { return false; } return true; } } // end of class NetworkConfiguration