Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 94aca5e

Browse files
rozzavbabanin
andauthored
ClientMetaData refactoring (#1807)
Avoid appending duplicate metadata Added new @internal annotation for non public / public apis JAVA-5955 --------- Co-authored-by: Viacheslav Babanin <frest0512@gmail.com>
1 parent 99b9e43 commit 94aca5e

File tree

10 files changed

+536
-228
lines changed

10 files changed

+536
-228
lines changed

‎driver-core/src/main/com/mongodb/MongoDriverInformation.java‎

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package com.mongodb;
1818

19+
import com.mongodb.annotations.Internal;
1920
import com.mongodb.annotations.NotThreadSafe;
21+
import com.mongodb.internal.client.DriverInformation;
22+
import com.mongodb.internal.client.DriverInformationHelper;
2023

2124
import java.util.ArrayList;
2225
import java.util.Collections;
2326
import java.util.List;
2427

25-
import static com.mongodb.assertions.Assertions.isTrue;
2628
import static com.mongodb.assertions.Assertions.notNull;
2729

2830
/**
@@ -46,9 +48,7 @@
4648
* @mongodb.server.release 3.4
4749
*/
4850
public final class MongoDriverInformation {
49-
private final List<String> driverNames;
50-
private final List<String> driverVersions;
51-
private final List<String> driverPlatforms;
51+
private final List<DriverInformation> driverInformationList;
5252

5353
/**
5454
* Convenience method to create a Builder.
@@ -75,7 +75,7 @@ public static Builder builder(final MongoDriverInformation mongoDriverInformatio
7575
* @return the driverNames
7676
*/
7777
public List<String> getDriverNames() {
78-
return driverNames;
78+
return DriverInformationHelper.getNames(driverInformationList);
7979
}
8080

8181
/**
@@ -84,7 +84,7 @@ public List<String> getDriverNames() {
8484
* @return the driverVersions
8585
*/
8686
public List<String> getDriverVersions() {
87-
return driverVersions;
87+
return DriverInformationHelper.getVersions(driverInformationList);
8888
}
8989

9090
/**
@@ -93,15 +93,23 @@ public List<String> getDriverVersions() {
9393
* @return the driverPlatforms
9494
*/
9595
public List<String> getDriverPlatforms() {
96-
return driverPlatforms;
96+
return DriverInformationHelper.getPlatforms(driverInformationList);
97+
}
98+
99+
/**
100+
* For internal use only
101+
*/
102+
@Internal
103+
public List<DriverInformation> getDriverInformationList() {
104+
return driverInformationList;
97105
}
98106

99107
/**
100108
*
101109
*/
102110
@NotThreadSafe
103111
public static final class Builder {
104-
private final MongoDriverInformation driverInformation;
112+
private final MongoDriverInformation mongoDriverInformation;
105113
private String driverName;
106114
private String driverVersion;
107115
private String driverPlatform;
@@ -147,38 +155,26 @@ public Builder driverPlatform(final String driverPlatform) {
147155
* @return the driver information
148156
*/
149157
public MongoDriverInformation build() {
150-
isTrue("You must also set the driver name when setting the driver version", !(driverName == null && driverVersion != null));
151-
152-
List<String> names = prependToList(driverInformation.getDriverNames(), driverName);
153-
List<String> versions = prependToList(driverInformation.getDriverVersions(), driverVersion);
154-
List<String> platforms = prependToList(driverInformation.getDriverPlatforms(), driverPlatform);
155-
return new MongoDriverInformation(names, versions, platforms);
156-
}
157-
158-
private List<String> prependToList(final List<String> stringList, final String value) {
159-
if (value == null) {
160-
return stringList;
161-
} else {
162-
ArrayList<String> newList = new ArrayList<>();
163-
newList.add(value);
164-
newList.addAll(stringList);
165-
return Collections.unmodifiableList(newList);
158+
DriverInformation driverInformation = new DriverInformation(driverName, driverVersion, driverPlatform);
159+
if (mongoDriverInformation.driverInformationList.contains(driverInformation)) {
160+
return mongoDriverInformation;
166161
}
162+
163+
List<DriverInformation> driverInformationList = new ArrayList<>(mongoDriverInformation.driverInformationList);
164+
driverInformationList.add(driverInformation);
165+
return new MongoDriverInformation(Collections.unmodifiableList(driverInformationList));
167166
}
168167

169168
private Builder() {
170-
List<String> immutableEmptyList = Collections.emptyList();
171-
driverInformation = new MongoDriverInformation(immutableEmptyList, immutableEmptyList, immutableEmptyList);
169+
mongoDriverInformation = new MongoDriverInformation(Collections.emptyList());
172170
}
173171

174172
private Builder(final MongoDriverInformation driverInformation) {
175-
this.driverInformation = notNull("driverInformation", driverInformation);
173+
this.mongoDriverInformation = notNull("driverInformation", driverInformation);
176174
}
177175
}
178176

179-
private MongoDriverInformation(final List<String> driverNames, final List<String> driverVersions, final List<String> driverPlatforms) {
180-
this.driverNames = driverNames;
181-
this.driverVersions = driverVersions;
182-
this.driverPlatforms = driverPlatforms;
177+
private MongoDriverInformation(final List<DriverInformation> driverInformation) {
178+
this.driverInformationList = notNull("driverInformation", driverInformation);
183179
}
184180
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
* http://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 com.mongodb.annotations;
17+
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Signifies that a public API element is intended for internal use only.
26+
*
27+
* <p>It is inadvisable for <i>applications</i> to use Internal APIs as they are intended for <b>internal library purposes</b> only.</p>
28+
*/
29+
@Retention(RetentionPolicy.CLASS)
30+
@Target({
31+
ElementType.ANNOTATION_TYPE,
32+
ElementType.CONSTRUCTOR,
33+
ElementType.FIELD,
34+
ElementType.METHOD,
35+
ElementType.PACKAGE,
36+
ElementType.TYPE })
37+
@Documented
38+
@Alpha(Reason.CLIENT)
39+
public @interface Internal {
40+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
* http://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 com.mongodb.internal.client;
17+
18+
import com.mongodb.lang.Nullable;
19+
20+
import java.util.Objects;
21+
22+
public final class DriverInformation {
23+
@Nullable
24+
private final String driverName;
25+
@Nullable
26+
private final String driverVersion;
27+
@Nullable
28+
private final String driverPlatform;
29+
30+
public DriverInformation(@Nullable final String driverName,
31+
@Nullable final String driverVersion,
32+
@Nullable final String driverPlatform) {
33+
this.driverName = driverName == null || driverName.isEmpty() ? null : driverName;
34+
this.driverVersion = driverVersion == null || driverVersion.isEmpty() ? null : driverVersion;
35+
this.driverPlatform = driverPlatform == null || driverPlatform.isEmpty() ? null : driverPlatform;
36+
}
37+
38+
@Nullable
39+
public String getDriverName() {
40+
return driverName;
41+
}
42+
43+
@Nullable
44+
public String getDriverVersion() {
45+
return driverVersion;
46+
}
47+
48+
@Nullable
49+
public String getDriverPlatform() {
50+
return driverPlatform;
51+
}
52+
53+
@Override
54+
public boolean equals(final Object o) {
55+
if (o == null || getClass() != o.getClass()) {
56+
return false;
57+
}
58+
59+
final DriverInformation that = (DriverInformation) o;
60+
return Objects.equals(driverName, that.driverName)
61+
&& Objects.equals(driverVersion, that.driverVersion)
62+
&& Objects.equals(driverPlatform, that.driverPlatform);
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
int result = Objects.hashCode(driverName);
68+
result = 31 * result + Objects.hashCode(driverVersion);
69+
result = 31 * result + Objects.hashCode(driverPlatform);
70+
return result;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return "DriverInformation{"
76+
+ "driverName='" + driverName + '\''
77+
+ ", driverVersion='" + driverVersion + '\''
78+
+ ", driverPlatform='" + driverPlatform + '\''
79+
+ '}';
80+
}
81+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
* http://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 com.mongodb.internal.client;
17+
18+
import com.mongodb.internal.build.MongoDriverVersion;
19+
20+
import java.util.Collections;
21+
import java.util.List;
22+
import java.util.Objects;
23+
import java.util.function.Function;
24+
import java.util.stream.Collectors;
25+
26+
import static java.lang.String.format;
27+
import static java.lang.System.getProperty;
28+
29+
public final class DriverInformationHelper {
30+
31+
public static final DriverInformation INITIAL_DRIVER_INFORMATION =
32+
new DriverInformation(MongoDriverVersion.NAME, MongoDriverVersion.VERSION,
33+
format("Java/%s/%s", getProperty("java.vendor", "unknown-vendor"),
34+
getProperty("java.runtime.version", "unknown-version")));
35+
36+
public static List<String> getNames(final List<DriverInformation> driverInformation) {
37+
return getDriverField(DriverInformation::getDriverName, driverInformation);
38+
}
39+
40+
public static List<String> getVersions(final List<DriverInformation> driverInformation) {
41+
return getDriverField(DriverInformation::getDriverVersion, driverInformation);
42+
}
43+
44+
public static List<String> getPlatforms(final List<DriverInformation> driverInformation) {
45+
return getDriverField(DriverInformation::getDriverPlatform, driverInformation);
46+
}
47+
48+
private static List<String> getDriverField(final Function<DriverInformation, String> fieldSupplier,
49+
final List<DriverInformation> driverInformation) {
50+
return Collections.unmodifiableList(driverInformation.stream()
51+
.map(fieldSupplier)
52+
.filter(Objects::nonNull)
53+
.collect(Collectors.toList()));
54+
}
55+
56+
private DriverInformationHelper() {
57+
}
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
* http://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+
17+
/**
18+
* This package contains classes for internal client functionality.
19+
*/
20+
21+
@NonNullApi
22+
package com.mongodb.internal.client;
23+
24+
import com.mongodb.lang.NonNullApi;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /