Elffuss BraidHub

kikocis/java-driver public

Introduce builder in Policies.
Changesetdb6140bb093e4a8269cc0681e4c6a4d5f16607846d3be42f6e54e498ecaab107
Authorolim7t <omichallat+github@gmail.com> · 2015-06-19 20:04 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-22 10:33 +02:00
Parents392aefac701d

Browse the files at this changeset

3 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +8 −8
@@ -1156,14 +1156,14 @@
11561156 */
11571157 @Override
11581158 public Configuration getConfiguration() {
1159- Policies policies = new Policies(
1160- loadBalancingPolicy == null ? Policies.defaultLoadBalancingPolicy() : loadBalancingPolicy,
1161- Objects.firstNonNull(reconnectionPolicy, Policies.defaultReconnectionPolicy()),
1162- Objects.firstNonNull(retryPolicy, Policies.defaultRetryPolicy()),
1163- Objects.firstNonNull(addressTranslater, Policies.defaultAddressTranslater()),
1164- Objects.firstNonNull(timestampGenerator, Policies.defaultTimestampGenerator()),
1165- Objects.firstNonNull(speculativeExecutionPolicy, Policies.defaultSpeculativeExecutionPolicy())
1166- );
1159+ Policies policies = Policies.builder()
1160+ .withLoadBalancingPolicy(loadBalancingPolicy)
1161+ .withReconnectionPolicy(reconnectionPolicy)
1162+ .withRetryPolicy(retryPolicy)
1163+ .withAddressTranslater(addressTranslater)
1164+ .withTimestampGenerator(timestampGenerator)
1165+ .withSpeculativeExecutionPolicy(speculativeExecutionPolicy)
1166+ .build();
11671167 return new Configuration(policies,
11681168 new ProtocolOptions(port, protocolVersion, maxSchemaAgreementWaitSeconds, sslOptions, authProvider).setCompression(compression),
11691169 poolingOptions == null ? new PoolingOptions() : poolingOptions,
modified driver-core/src/main/java/com/datastax/driver/core/Configuration.java +1 −1
@@ -46,7 +46,7 @@
4646 * Creates a configuration object.
4747 */
4848 public Configuration() {
49- this(new Policies(),
49+ this(Policies.builder().build(),
5050 new ProtocolOptions(),
5151 new PoolingOptions(),
5252 new SocketOptions(),
modified driver-core/src/main/java/com/datastax/driver/core/policies/Policies.java +172 −50
@@ -15,6 +15,8 @@
1515 */
1616 package com.datastax.driver.core.policies;
1717
18+import com.google.common.base.Objects;
19+
1820 import com.datastax.driver.core.ServerSideTimestampGenerator;
1921 import com.datastax.driver.core.TimestampGenerator;
2022
@@ -23,6 +25,15 @@
2325 */
2426 public class Policies {
2527
28+ /**
29+ * Returns a builder to create a new {@code Policies} object.
30+ *
31+ * @return the builder.
32+ */
33+ public static Builder builder() {
34+ return new Builder();
35+ }
36+
2637 private static final ReconnectionPolicy DEFAULT_RECONNECTION_POLICY = new ExponentialReconnectionPolicy(1000, 10 * 60 * 1000);
2738 private static final RetryPolicy DEFAULT_RETRY_POLICY = DefaultRetryPolicy.INSTANCE;
2839 private static final AddressTranslater DEFAULT_ADDRESS_TRANSLATER = new IdentityTranslater();
@@ -35,57 +46,15 @@
3546 private final TimestampGenerator timestampGenerator;
3647 private final SpeculativeExecutionPolicy speculativeExecutionPolicy;
3748
38- public Policies() {
39- this(defaultLoadBalancingPolicy(), defaultReconnectionPolicy(), defaultRetryPolicy(), defaultAddressTranslater(), defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy());
40- }
41-
42- /**
43- * Creates a new {@code Policies} object using the provided policies.
44- * <p>
45- * This constructor use the default {@link IdentityTranslater} and {@link TimestampGenerator}.
46- *
47- * @param loadBalancingPolicy the load balancing policy to use.
48- * @param reconnectionPolicy the reconnection policy to use.
49- * @param retryPolicy the retry policy to use.
50- */
51- public Policies(LoadBalancingPolicy loadBalancingPolicy,
52- ReconnectionPolicy reconnectionPolicy,
53- RetryPolicy retryPolicy,
54- SpeculativeExecutionPolicy speculativeExecutionPolicy) {
55- this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, DEFAULT_ADDRESS_TRANSLATER, speculativeExecutionPolicy);
56- }
57-
5849 /**
59- * Creates a new {@code Policies} object using the provided policies.
60- *
61- * @param loadBalancingPolicy the load balancing policy to use.
62- * @param reconnectionPolicy the reconnection policy to use.
63- * @param retryPolicy the retry policy to use.
64- * @param addressTranslater the address translater to use.
50+ * @deprecated this constructor is exposed for backward compatibility.
51+ * Use {@link #builder()} instead.
6552 */
66- public Policies(LoadBalancingPolicy loadBalancingPolicy,
67- ReconnectionPolicy reconnectionPolicy,
68- RetryPolicy retryPolicy,
69- AddressTranslater addressTranslater,
70- SpeculativeExecutionPolicy speculativeExecutionPolicy) {
71- // NB: this constructor is provided for backward compatibility with 2.1.0
72- this.loadBalancingPolicy = loadBalancingPolicy;
73- this.reconnectionPolicy = reconnectionPolicy;
74- this.retryPolicy = retryPolicy;
75- this.addressTranslater = addressTranslater;
76- this.speculativeExecutionPolicy = speculativeExecutionPolicy;
77- this.timestampGenerator = defaultTimestampGenerator();
78- }
79-
80- /**
81- * Creates a new {@code Policies} object using the provided policies.
82- *
83- * @param loadBalancingPolicy the load balancing policy to use.
84- * @param reconnectionPolicy the reconnection policy to use.
85- * @param retryPolicy the retry policy to use.
86- * @param addressTranslater the address translater to use.
87- * @param timestampGenerator the timestamp generator to use.
88- * @param speculativeExecutionPolicy the speculative execution policy to use.
53+ @Deprecated
54+ /*
55+ * This constructor should be private and called only by the builder.
56+ * If a new field gets added, add it to this constructor and make it private, then expose the current signature as a deprecated method
57+ * (see end of class).
8958 */
9059 public Policies(LoadBalancingPolicy loadBalancingPolicy,
9160 ReconnectionPolicy reconnectionPolicy,
@@ -108,7 +77,7 @@
10877 * awareness (so {@code new TokenAwarePolicy(new DCAwareRoundRobinPolicy())}).
10978 * <p>
11079 * Note that this policy shuffles the replicas when token awareness is used, see
111- * {@link TokenAwarePolicy#TokenAwarePolicy(LoadBalancingPolicy,boolean)} for an
80+ * {@link TokenAwarePolicy#TokenAwarePolicy(LoadBalancingPolicy, boolean)} for an
11281 * explanation of the tradeoffs.
11382 *
11483 * @return the default load balancing policy.
@@ -235,4 +204,157 @@
235204 public SpeculativeExecutionPolicy getSpeculativeExecutionPolicy() {
236205 return speculativeExecutionPolicy;
237206 }
207+
208+ /**
209+ * A builder to create a new {@code Policies} object.
210+ */
211+ public static class Builder {
212+ private LoadBalancingPolicy loadBalancingPolicy;
213+ private ReconnectionPolicy reconnectionPolicy;
214+ private RetryPolicy retryPolicy;
215+ private AddressTranslater addressTranslater;
216+ private TimestampGenerator timestampGenerator;
217+ private SpeculativeExecutionPolicy speculativeExecutionPolicy;
218+
219+ /**
220+ * Sets the load balancing policy.
221+ *
222+ * @param loadBalancingPolicy see {@link #getLoadBalancingPolicy()}.
223+ * @return this builder.
224+ */
225+ public Builder withLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) {
226+ this.loadBalancingPolicy = loadBalancingPolicy;
227+ return this;
228+ }
229+
230+ /**
231+ * Sets the reconnection policy.
232+ *
233+ * @param reconnectionPolicy see {@link #getReconnectionPolicy()}.
234+ * @return this builder.
235+ */
236+ public Builder withReconnectionPolicy(ReconnectionPolicy reconnectionPolicy) {
237+ this.reconnectionPolicy = reconnectionPolicy;
238+ return this;
239+ }
240+
241+ /**
242+ * Sets the retry policy.
243+ *
244+ * @param retryPolicy see {@link #getRetryPolicy()}.
245+ * @return this builder.
246+ */
247+ public Builder withRetryPolicy(RetryPolicy retryPolicy) {
248+ this.retryPolicy = retryPolicy;
249+ return this;
250+ }
251+
252+ /**
253+ * Sets the address translator.
254+ *
255+ * @param addressTranslater see {@link #getAddressTranslater()}.
256+ * @return this builder.
257+ */
258+ public Builder withAddressTranslater(AddressTranslater addressTranslater) {
259+ this.addressTranslater = addressTranslater;
260+ return this;
261+ }
262+
263+ /**
264+ * Sets the timestamp generator.
265+ *
266+ * @param timestampGenerator see {@link #getTimestampGenerator()}.
267+ * @return this builder.
268+ */
269+ public Builder withTimestampGenerator(TimestampGenerator timestampGenerator) {
270+ this.timestampGenerator = timestampGenerator;
271+ return this;
272+ }
273+
274+ /**
275+ * Sets the speculative execution policy.
276+ *
277+ * @param speculativeExecutionPolicy see {@link #getSpeculativeExecutionPolicy()}.
278+ * @return this builder.
279+ */
280+ public Builder withSpeculativeExecutionPolicy(SpeculativeExecutionPolicy speculativeExecutionPolicy) {
281+ this.speculativeExecutionPolicy = speculativeExecutionPolicy;
282+ return this;
283+ }
284+
285+ /**
286+ * Builds the final object from this builder.
287+ * <p>
288+ * Any field that hasn't been set explicitly will get its default value.
289+ *
290+ * @return the object.
291+ */
292+ public Policies build() {
293+ return new Policies(
294+ loadBalancingPolicy == null ? Policies.defaultLoadBalancingPolicy() : loadBalancingPolicy,
295+ Objects.firstNonNull(reconnectionPolicy, Policies.defaultReconnectionPolicy()),
296+ Objects.firstNonNull(retryPolicy, Policies.defaultRetryPolicy()),
297+ Objects.firstNonNull(addressTranslater, Policies.defaultAddressTranslater()),
298+ Objects.firstNonNull(timestampGenerator, Policies.defaultTimestampGenerator()),
299+ Objects.firstNonNull(speculativeExecutionPolicy, Policies.defaultSpeculativeExecutionPolicy()));
300+ }
301+ }
302+
303+ /*
304+ * The public constructors of this class were mistakenly changed over time (this is a breaking change).
305+ *
306+ * The code below provides backward compatibility with every signature that was ever exposed in a released version.
307+ * This is ugly but the safest way to avoid issues for clients upgrading from a previous version.
308+ *
309+ * In the future, new fields will only be added to the builder, which avoids this problem.
310+ */
311+
312+ /**
313+ * @deprecated this constructor is provided for backward compatibility.
314+ * Use {@link #builder()} instead.
315+ */
316+ @Deprecated
317+ public Policies() {
318+ this(defaultLoadBalancingPolicy(), defaultReconnectionPolicy(), defaultRetryPolicy(), defaultAddressTranslater(), defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy());
319+ }
320+
321+ /**
322+ * @deprecated this constructor is provided for backward compatibility.
323+ * Use {@link #builder()} instead.
324+ */
325+ public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy) {
326+ this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, defaultAddressTranslater(), defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy());
327+ }
328+
329+ /**
330+ * @deprecated this constructor is provided for backward compatibility.
331+ * Use {@link #builder()} instead.
332+ */
333+ public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, AddressTranslater addressTranslater) {
334+ this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, addressTranslater, defaultTimestampGenerator(), defaultSpeculativeExecutionPolicy());
335+ }
336+
337+ /**
338+ * @deprecated this constructor is provided for backward compatibility.
339+ * Use {@link #builder()} instead.
340+ */
341+ public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, SpeculativeExecutionPolicy speculativeExecutionPolicy) {
342+ this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, defaultAddressTranslater(), defaultTimestampGenerator(), speculativeExecutionPolicy);
343+ }
344+
345+ /**
346+ * @deprecated this constructor is provided for backward compatibility.
347+ * Use {@link #builder()} instead.
348+ */
349+ public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, AddressTranslater addressTranslater, TimestampGenerator timestampGenerator) {
350+ this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, addressTranslater, timestampGenerator, defaultSpeculativeExecutionPolicy());
351+ }
352+
353+ /**
354+ * @deprecated this constructor is provided for backward compatibility.
355+ * Use {@link #builder()} instead.
356+ */
357+ public Policies(LoadBalancingPolicy loadBalancingPolicy, ReconnectionPolicy reconnectionPolicy, RetryPolicy retryPolicy, AddressTranslater addressTranslater, SpeculativeExecutionPolicy speculativeExecutionPolicy) {
358+ this(loadBalancingPolicy, reconnectionPolicy, retryPolicy, addressTranslater, defaultTimestampGenerator(), speculativeExecutionPolicy);
359+ }
238360 }