]> git.basschouten.com Git - openhab-addons.git/blob
44f1cbfb6fa3af50f250365b0febeb5eaaba752f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.transport.modbus.endpoint;
14
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;
21
22 /**
23  * Class representing pooling related configuration of a single endpoint
24  *
25  * This class implements equals hashcode constract, and thus is suitable for use as keys in HashMaps, for example.
26  *
27  * @author Sami Salonen - Initial contribution
28  */
29 @NonNullByDefault
30 public class EndpointPoolConfiguration {
31
32     /**
33      * How long should be the minimum duration between previous transaction end and the next transaction with the same
34      * endpoint.
35      *
36      * In milliseconds.
37      */
38     private long interTransactionDelayMillis;
39
40     /**
41      * How long should be the minimum duration between connection-establishments from the pool (with same endpoint). In
42      * milliseconds.
43      */
44     private long interConnectDelayMillis;
45
46     /**
47      * How many times we want to try connecting to the endpoint before giving up. One means that connection
48      * establishment is tried once.
49      */
50     private int connectMaxTries = 1;
51
52     /**
53      * Re-connect connection every X milliseconds. Negative means that connection is not disconnected automatically.
54      * One can use 0ms to denote reconnection after every transaction (default).
55      */
56     private int reconnectAfterMillis;
57
58     /**
59      * How long before we give up establishing the connection. In milliseconds. Default of 0 means that system/OS
60      * default is respected.
61      */
62     private int connectTimeoutMillis;
63
64     private static StandardToStringStyle toStringStyle = new StandardToStringStyle();
65
66     static {
67         toStringStyle.setUseShortClassName(true);
68     }
69
70     public long getInterConnectDelayMillis() {
71         return interConnectDelayMillis;
72     }
73
74     public void setInterConnectDelayMillis(long interConnectDelayMillis) {
75         this.interConnectDelayMillis = interConnectDelayMillis;
76     }
77
78     public int getConnectMaxTries() {
79         return connectMaxTries;
80     }
81
82     public void setConnectMaxTries(int connectMaxTries) {
83         this.connectMaxTries = connectMaxTries;
84     }
85
86     public int getReconnectAfterMillis() {
87         return reconnectAfterMillis;
88     }
89
90     public void setReconnectAfterMillis(int reconnectAfterMillis) {
91         this.reconnectAfterMillis = reconnectAfterMillis;
92     }
93
94     public long getInterTransactionDelayMillis() {
95         return interTransactionDelayMillis;
96     }
97
98     public void setInterTransactionDelayMillis(long interTransactionDelayMillis) {
99         this.interTransactionDelayMillis = interTransactionDelayMillis;
100     }
101
102     public int getConnectTimeoutMillis() {
103         return connectTimeoutMillis;
104     }
105
106     public void setConnectTimeoutMillis(int connectTimeoutMillis) {
107         this.connectTimeoutMillis = connectTimeoutMillis;
108     }
109
110     @Override
111     public int hashCode() {
112         return new HashCodeBuilder(2149, 3117).append(interTransactionDelayMillis).append(interConnectDelayMillis)
113                 .append(connectMaxTries).append(reconnectAfterMillis).append(connectTimeoutMillis).toHashCode();
114     }
115
116     @Override
117     public String toString() {
118         return new ToStringBuilder(this, toStringStyle)
119                 .append("interTransactionDelayMillis", interTransactionDelayMillis)
120                 .append("interConnectDelayMillis", interConnectDelayMillis).append("connectMaxTries", connectMaxTries)
121                 .append("reconnectAfterMillis", reconnectAfterMillis)
122                 .append("connectTimeoutMillis", connectTimeoutMillis).toString();
123     }
124
125     @Override
126     public boolean equals(@Nullable Object obj) {
127         if (obj == null) {
128             return false;
129         }
130         if (obj == this) {
131             return true;
132         }
133         if (obj.getClass() != getClass()) {
134             return false;
135         }
136         EndpointPoolConfiguration rhs = (EndpointPoolConfiguration) obj;
137         return new EqualsBuilder().append(interTransactionDelayMillis, rhs.interTransactionDelayMillis)
138                 .append(interConnectDelayMillis, rhs.interConnectDelayMillis)
139                 .append(connectMaxTries, rhs.connectMaxTries).append(reconnectAfterMillis, rhs.reconnectAfterMillis)
140                 .append(connectTimeoutMillis, rhs.connectTimeoutMillis).isEquals();
141     }
142 }