2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.transport.modbus.endpoint;
15 import org.apache.commons.lang.builder.EqualsBuilder;
16 import org.apache.commons.lang.builder.HashCodeBuilder;
17 import org.apache.commons.lang.builder.StandardToStringStyle;
18 import org.apache.commons.lang.builder.ToStringBuilder;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
23 * Common base class for ip based endpoints. Endpoint differentiates different modbus slaves only by the ip address
24 * (string) and port name.
26 * @author Sami Salonen - Initial contribution
29 public abstract class ModbusIPSlaveEndpoint implements ModbusSlaveEndpoint {
31 private String address;
34 private static StandardToStringStyle toStringStyle = new StandardToStringStyle();
37 toStringStyle.setUseShortClassName(true);
40 public ModbusIPSlaveEndpoint(String address, int port) {
41 this.address = address;
45 public String getAddress() {
49 public int getPort() {
54 public int hashCode() {
55 // differentiate different protocols using the class name, and after that use address and port
56 int protocolHash = this.getClass().getName().hashCode();
57 if (protocolHash % 2 == 0) {
60 return new HashCodeBuilder(11, protocolHash).append(address).append(port).toHashCode();
64 public String toString() {
65 return new ToStringBuilder(this, toStringStyle).append("address", address).append("port", port).toString();
69 public boolean equals(@Nullable Object obj) {
76 if (obj.getClass() != getClass()) {
77 // different protocol -> not equal!
80 ModbusIPSlaveEndpoint rhs = (ModbusIPSlaveEndpoint) obj;
81 return new EqualsBuilder().append(address, rhs.address).append(port, rhs.port).isEquals();