forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterSettings.java
More file actions
229 lines (200 loc) · 6.78 KB
/
Copy pathClusterSettings.java
File metadata and controls
229 lines (200 loc) · 6.78 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import org.bson.util.annotations.Immutable;
import java.util.Collections;
import java.util.List;
import static org.bson.util.Assertions.isTrueArgument;
import static org.bson.util.Assertions.notNull;
/**
* Settings for the cluster.
*/
@Immutable
final class ClusterSettings {
private final List<ServerAddress> hosts;
private final ClusterConnectionMode mode;
private final ClusterType requiredClusterType;
private final String requiredReplicaSetName;
public static Builder builder() {
return new Builder();
}
/**
* A builder for the cluster settings.
*/
static final class Builder {
private List<ServerAddress> hosts;
private ClusterConnectionMode mode = ClusterConnectionMode.Multiple;
private ClusterType requiredClusterType = ClusterType.Unknown;
private String requiredReplicaSetName;
private Builder() {
}
// CHECKSTYLE:OFF
/**
* Sets the hosts for the cluster.
*
* @param hosts the seed list of hosts
* @return this
*/
public Builder hosts(final List<ServerAddress> hosts) {
notNull("hosts", hosts);
this.hosts = Collections.unmodifiableList(hosts);
return this;
}
/**
* Sets the mode for this cluster.
*
* @param mode the cluster connection mode
* @return this;
*/
public Builder mode(final ClusterConnectionMode mode) {
this.mode = notNull("mode", mode);
return this;
}
/**
* Sets the required replica set name for the cluster.
*
* @param requiredReplicaSetName the required replica set name.
* @return this
*/
public Builder requiredReplicaSetName(final String requiredReplicaSetName) {
this.requiredReplicaSetName = requiredReplicaSetName;
return this;
}
/**
* Sets the required cluster type for the cluster.
*
* @param requiredClusterType the required cluster type
* @return this
*/
public Builder requiredClusterType(final ClusterType requiredClusterType) {
this.requiredClusterType = notNull("requiredClusterType", requiredClusterType);
return this;
}
/**
* Build the settings from the builder.
*
* @return the cluster settings
*/
public ClusterSettings build() {
return new ClusterSettings(this);
}
// CHECKSTYLE:ON
}
/**
* Gets the seed list of hosts for the cluster.
*
* @return the seed list of hosts
*/
public List<ServerAddress> getHosts() {
return hosts;
}
/**
* Gets the mode.
*
* @return the mode
*/
public ClusterConnectionMode getMode() {
return mode;
}
/**
* Get
*
* @return the cluster type
*/
public ClusterType getRequiredClusterType() {
return requiredClusterType;
}
/**
* Gets the required replica set name.
*
* @return the required replica set name
*/
public String getRequiredReplicaSetName() {
return requiredReplicaSetName;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ClusterSettings that = (ClusterSettings) o;
if (!hosts.equals(that.hosts)) {
return false;
}
if (mode != that.mode) {
return false;
}
if (requiredClusterType != that.requiredClusterType) {
return false;
}
if (requiredReplicaSetName != null
? !requiredReplicaSetName.equals(that.requiredReplicaSetName) : that.requiredReplicaSetName != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = hosts.hashCode();
result = 31 * result + mode.hashCode();
result = 31 * result + requiredClusterType.hashCode();
result = 31 * result + (requiredReplicaSetName != null ? requiredReplicaSetName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "{"
+ "hosts=" + hosts
+ ", mode=" + mode
+ ", requiredClusterType=" + requiredClusterType
+ ", requiredReplicaSetName='" + requiredReplicaSetName + '\''
+ '}';
}
public String getShortDescription() {
return "{"
+ "hosts=" + hosts
+ ", mode=" + mode
+ ", requiredClusterType=" + requiredClusterType
+ (requiredReplicaSetName == null ? "" : ", requiredReplicaSetName='" + requiredReplicaSetName + '\'')
+ '}';
}
private ClusterSettings(final Builder builder) {
notNull("hosts", builder.hosts);
isTrueArgument("hosts size > 0", builder.hosts.size() > 0);
if (builder.hosts.size() > 1 && builder.requiredClusterType == ClusterType.StandAlone) {
throw new IllegalArgumentException("Multiple hosts cannot be specified when using ClusterType.StandAlone.");
}
if (builder.mode == ClusterConnectionMode.Single && builder.hosts.size() > 1) {
throw new IllegalArgumentException("Can not directly connect to more than one server");
}
if (builder.requiredReplicaSetName != null) {
if (builder.requiredClusterType == ClusterType.Unknown) {
builder.requiredClusterType = ClusterType.ReplicaSet;
}
else if (builder.requiredClusterType != ClusterType.ReplicaSet) {
throw new IllegalArgumentException(
"When specifying a replica set name, only ClusterType.Unknown and ClusterType.ReplicaSet are valid.");
}
}
hosts = builder.hosts;
mode = builder.mode;
requiredReplicaSetName = builder.requiredReplicaSetName;
requiredClusterType = builder.requiredClusterType;
}
}