/
ImmutableConnectionBuilder.java
127 lines (109 loc) · 4.11 KB
/
ImmutableConnectionBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*
*/
package org.eclipse.ditto.model.amqpbridge;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkArgument;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
/**
* Builder for {@code ImmutableConnection}.
*/
class ImmutableConnectionBuilder implements ConnectionBuilder {
final String id;
final ConnectionType connectionType;
final AuthorizationContext authorizationContext;
final String uri;
@Nullable Set<String> sources;
@Nullable String eventTarget;
@Nullable String replyTarget;
boolean failoverEnabled = true;
boolean validateCertificate = true;
int throttle = -1;
int consumerCount = 1;
int processorPoolSize = 5;
private ImmutableConnectionBuilder(final String id, final ConnectionType connectionType,
final String uri, final AuthorizationContext authorizationContext) {
this.id = checkNotNull(id, "ID");
this.connectionType = checkNotNull(connectionType, "Connection Type");
this.uri = checkNotNull(uri, "URI");
this.authorizationContext = checkNotNull(authorizationContext, "Authorization Context");
}
/**
* Instantiates a new {@code ImmutableAmqpConnectionBuilder}.
*
* @param id the connection id
* @param connectionType the connection type
* @param uri the uri
* @param authorizationContext the authorization context
* @return new instance of {@code ImmutableAmqpConnectionBuilder}
*/
static ConnectionBuilder of(final String id, final ConnectionType connectionType,
final String uri, final AuthorizationContext authorizationContext) {
return new ImmutableConnectionBuilder(id, connectionType, uri, authorizationContext);
}
@Override
public ConnectionBuilder failoverEnabled(final boolean failoverEnabled) {
this.failoverEnabled = failoverEnabled;
return this;
}
@Override
public ConnectionBuilder validateCertificate(final boolean validateCertificate) {
this.validateCertificate = validateCertificate;
return this;
}
@Override
public ConnectionBuilder throttle(final int throttle) {
this.throttle = throttle;
return this;
}
@Override
public ConnectionBuilder consumerCount(final int consumerCount) {
this.consumerCount = checkArgument(consumerCount, c -> c > 0, () -> "consumerCount must be positive");
return this;
}
@Override
public ConnectionBuilder processorPoolSize(final int processorPoolSize) {
this.processorPoolSize = checkArgument(processorPoolSize, ps -> ps > 0, () -> "consumerCount must be positive");
return this;
}
@Override
public ConnectionBuilder sources(final String... sources) {
checkNotNull(sources, "Sources");
this.sources = new HashSet<>(Arrays.asList(sources));
return this;
}
@Override
public ConnectionBuilder sources(final Set<String> sources) {
checkNotNull(sources, "Sources");
this.sources = new HashSet<>(sources);
return this;
}
@Override
public ConnectionBuilder eventTarget(final String eventTarget) {
checkNotNull(eventTarget, "eventTarget");
this.eventTarget = eventTarget;
return this;
}
@Override
public ConnectionBuilder replyTarget(final String replyTarget) {
checkNotNull(replyTarget, "replyTarget");
this.replyTarget = replyTarget;
return this;
}
@Override
public Connection build() {
return new ImmutableConnection(this);
}
}