View Javadoc
1   /*
2    *    Copyright 2012-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.caches.memcached;
17  
18  import static java.lang.String.format;
19  
20  import java.net.InetSocketAddress;
21  import java.util.List;
22  import java.util.concurrent.TimeUnit;
23  
24  import net.spy.memcached.ConnectionFactory;
25  
26  /**
27   * The Memcached client configuration.
28   *
29   * @author Simone Tripodi
30   */
31  final class MemcachedConfiguration {
32  
33    /**
34     * The key prefix.
35     */
36    private String keyPrefix;
37  
38    /**
39     * The Connection Factory used to establish the connection to Memcached server(s).
40     */
41    private ConnectionFactory connectionFactory;
42  
43    /**
44     * The Memcached servers.
45     */
46    private List<InetSocketAddress> addresses;
47  
48    /**
49     * The flag to switch from sync to async Memcached get.
50     */
51    private boolean usingAsyncGet;
52  
53    /**
54     * Compression enabled flag.
55     */
56    private boolean compressionEnabled;
57  
58    /**
59     * The Memcached entries expiration time.
60     */
61    private int expiration;
62  
63    /**
64     * The Memcached connection timeout when using async get.
65     */
66    private int timeout;
67  
68    /**
69     * The Memcached timeout unit when using async get.
70     */
71    private TimeUnit timeUnit;
72  
73    /**
74     * The flag to enable SASL Connection
75     */
76    private boolean usingSASL;
77  
78    /**
79     * The Memcached SASL username
80     */
81    private String username;
82  
83    /**
84     * The Memcached SASL password
85     */
86    private String password;
87  
88    /**
89     * @return the keyPrefix
90     */
91    public String getKeyPrefix() {
92      return keyPrefix;
93    }
94  
95    /**
96     * @param keyPrefix
97     *          the keyPrefix to set
98     */
99    public void setKeyPrefix(String keyPrefix) {
100     this.keyPrefix = keyPrefix;
101   }
102 
103   /**
104    * @return the connectionFactory
105    */
106   public ConnectionFactory getConnectionFactory() {
107     return connectionFactory;
108   }
109 
110   /**
111    * @param connectionFactory
112    *          the connectionFactory to set
113    */
114   public void setConnectionFactory(ConnectionFactory connectionFactory) {
115     this.connectionFactory = connectionFactory;
116   }
117 
118   /**
119    * @return the addresses
120    */
121   public List<InetSocketAddress> getAddresses() {
122     return addresses;
123   }
124 
125   /**
126    * @param addresses
127    *          the addresses to set
128    */
129   public void setAddresses(List<InetSocketAddress> addresses) {
130     this.addresses = addresses;
131   }
132 
133   /**
134    * @return the usingAsyncGet
135    */
136   public boolean isUsingAsyncGet() {
137     return usingAsyncGet;
138   }
139 
140   /**
141    * @param usingAsyncGet
142    *          the usingAsyncGet to set
143    */
144   public void setUsingAsyncGet(boolean usingAsyncGet) {
145     this.usingAsyncGet = usingAsyncGet;
146   }
147 
148   /**
149    * @return the compressionEnabled
150    */
151   public boolean isCompressionEnabled() {
152     return compressionEnabled;
153   }
154 
155   /**
156    * @param compressionEnabled
157    *          the compressionEnabled to set
158    */
159   public void setCompressionEnabled(boolean compressionEnabled) {
160     this.compressionEnabled = compressionEnabled;
161   }
162 
163   /**
164    * @return the expiration
165    */
166   public int getExpiration() {
167     return expiration;
168   }
169 
170   /**
171    * @param expiration
172    *          the expiration to set
173    */
174   public void setExpiration(int expiration) {
175     this.expiration = expiration;
176   }
177 
178   /**
179    * @return the timeout
180    */
181   public int getTimeout() {
182     return timeout;
183   }
184 
185   /**
186    * @param timeout
187    *          the timeout to set
188    */
189   public void setTimeout(int timeout) {
190     this.timeout = timeout;
191   }
192 
193   /**
194    * @return the timeUnit
195    */
196   public TimeUnit getTimeUnit() {
197     return timeUnit;
198   }
199 
200   /**
201    * @param timeUnit
202    *          the timeUnit to set
203    */
204   public void setTimeUnit(TimeUnit timeUnit) {
205     this.timeUnit = timeUnit;
206   }
207 
208   /**
209    * @return the usingSASL
210    */
211   public boolean isUsingSASL() {
212     return usingSASL;
213   }
214 
215   /**
216    * @param usingSASL
217    *          the usingSASL to set
218    */
219   public void setUsingSASL(boolean usingSASL) {
220     this.usingSASL = usingSASL;
221   }
222 
223   /**
224    * @return the username
225    */
226   public String getUsername() {
227     return username;
228   }
229 
230   /**
231    * @param username
232    *          the username to set
233    */
234   public void setUsername(String username) {
235     this.username = username;
236   }
237 
238   /**
239    * @return the password
240    */
241   public String getPassword() {
242     return password;
243   }
244 
245   /**
246    * @param password
247    *          the password to set
248    */
249   public void setPassword(String password) {
250     this.password = password;
251   }
252 
253   /**
254    * {@inheritDoc}
255    */
256   @Override
257   public int hashCode() {
258     return hash(1, 31, addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout,
259         usingAsyncGet, usingSASL, username, password);
260   }
261 
262   /**
263    * Computes a hashCode given the input objects.
264    *
265    * @param initialNonZeroOddNumber
266    *          a non-zero, odd number used as the initial value.
267    * @param multiplierNonZeroOddNumber
268    *          a non-zero, odd number used as the multiplier.
269    * @param objs
270    *          the objects to compute hash code.
271    *
272    * @return the computed hashCode.
273    */
274   public static int hash(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object... objs) {
275     int result = initialNonZeroOddNumber;
276     for (Object obj : objs) {
277       result = multiplierNonZeroOddNumber * result + (obj != null ? obj.hashCode() : 0);
278     }
279     return result;
280   }
281 
282   /**
283    * {@inheritDoc}
284    */
285   @Override
286   public boolean equals(Object obj) {
287     if (this == obj) {
288       return true;
289     }
290     if (obj == null || getClass() != obj.getClass()) {
291       return false;
292     }
293 
294     MemcachedConfiguration other = (MemcachedConfiguration) obj;
295     return eq(addresses, other.addresses) && eq(compressionEnabled, other.compressionEnabled)
296         && eq(connectionFactory, other.connectionFactory) && eq(expiration, other.expiration)
297         && eq(keyPrefix, other.keyPrefix) && eq(timeUnit, other.timeUnit) && eq(timeout, other.timeout)
298         && eq(usingAsyncGet, other.usingAsyncGet) && eq(usingSASL, other.usingSASL) && eq(username, other.username)
299         && eq(password, other.password);
300   }
301 
302   /**
303    * Verifies input objects are equal.
304    *
305    * @param o1
306    *          the first argument to compare
307    * @param o2
308    *          the second argument to compare
309    *
310    * @return true, if the input arguments are equal, false otherwise.
311    */
312   private static <O> boolean eq(O o1, O o2) {
313     return o1 != null ? o1.equals(o2) : o2 == null;
314   }
315 
316   /**
317    * {@inheritDoc}
318    */
319   @Override
320   public String toString() {
321     return format(
322         "MemcachedConfiguration [addresses=%s, compressionEnabled=%s, connectionFactory=%s, , expiration=%s, keyPrefix=%s, timeUnit=%s, timeout=%s, usingAsyncGet=%s, usingSASL=%s, username=%s, password=%s]",
323         addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout, usingAsyncGet,
324         usingSASL, username, password);
325   }
326 
327 }