Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 27 additions & 31 deletions driver-core/src/main/com/mongodb/MongoDriverInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.mongodb;

import com.mongodb.annotations.Internal;
import com.mongodb.annotations.NotThreadSafe;
import com.mongodb.internal.client.DriverInformation;
import com.mongodb.internal.client.DriverInformationHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.mongodb.assertions.Assertions.isTrue;
import static com.mongodb.assertions.Assertions.notNull;

/**
Expand All @@ -46,9 +48,7 @@
* @mongodb.server.release 3.4
*/
public final class MongoDriverInformation {
private final List<String> driverNames;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using lists of Strings, no longer works for the new test cases - as it meant we couldn't check for duplicates effectively.

private final List<String> driverVersions;
private final List<String> driverPlatforms;
private final List<DriverInformation> driverInformationList;

/**
* Convenience method to create a Builder.
Expand All @@ -75,7 +75,7 @@ public static Builder builder(final MongoDriverInformation mongoDriverInformatio
* @return the driverNames
*/
public List<String> getDriverNames() {
return driverNames;
return DriverInformationHelper.getNames(driverInformationList);
}

/**
Expand All @@ -84,7 +84,7 @@ public List<String> getDriverNames() {
* @return the driverVersions
*/
public List<String> getDriverVersions() {
return driverVersions;
return DriverInformationHelper.getVersions(driverInformationList);
}

/**
Expand All @@ -93,15 +93,23 @@ public List<String> getDriverVersions() {
* @return the driverPlatforms
*/
public List<String> getDriverPlatforms() {
return driverPlatforms;
return DriverInformationHelper.getPlatforms(driverInformationList);
}

/**
* For internal use only
*/
@Internal
public List<DriverInformation> getDriverInformationList() {
return driverInformationList;
}

/**
*
*/
@NotThreadSafe
public static final class Builder {
private final MongoDriverInformation driverInformation;
private final MongoDriverInformation mongoDriverInformation;
private String driverName;
private String driverVersion;
private String driverPlatform;
Expand Down Expand Up @@ -147,38 +155,26 @@ public Builder driverPlatform(final String driverPlatform) {
* @return the driver information
*/
public MongoDriverInformation build() {
isTrue("You must also set the driver name when setting the driver version", !(driverName == null && driverVersion != null));

List<String> names = prependToList(driverInformation.getDriverNames(), driverName);
List<String> versions = prependToList(driverInformation.getDriverVersions(), driverVersion);
List<String> platforms = prependToList(driverInformation.getDriverPlatforms(), driverPlatform);
return new MongoDriverInformation(names, versions, platforms);
}

private List<String> prependToList(final List<String> stringList, final String value) {
if (value == null) {
return stringList;
} else {
ArrayList<String> newList = new ArrayList<>();
newList.add(value);
newList.addAll(stringList);
return Collections.unmodifiableList(newList);
DriverInformation driverInformation = new DriverInformation(driverName, driverVersion, driverPlatform);
if (mongoDriverInformation.driverInformationList.contains(driverInformation)) {
return mongoDriverInformation;
}

List<DriverInformation> driverInformationList = new ArrayList<>(mongoDriverInformation.driverInformationList);
driverInformationList.add(driverInformation);
return new MongoDriverInformation(Collections.unmodifiableList(driverInformationList));
}

private Builder() {
List<String> immutableEmptyList = Collections.emptyList();
driverInformation = new MongoDriverInformation(immutableEmptyList, immutableEmptyList, immutableEmptyList);
mongoDriverInformation = new MongoDriverInformation(Collections.emptyList());
}

private Builder(final MongoDriverInformation driverInformation) {
this.driverInformation = notNull("driverInformation", driverInformation);
this.mongoDriverInformation = notNull("driverInformation", driverInformation);
}
}

private MongoDriverInformation(final List<String> driverNames, final List<String> driverVersions, final List<String> driverPlatforms) {
this.driverNames = driverNames;
this.driverVersions = driverVersions;
this.driverPlatforms = driverPlatforms;
private MongoDriverInformation(final List<DriverInformation> driverInformation) {
this.driverInformationList = notNull("driverInformation", driverInformation);
}
}
42 changes: 42 additions & 0 deletions driver-core/src/main/com/mongodb/annotations/Internal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2008-present MongoDB, Inc.
* Copyright 2010 The Guava Authors
* Copyright 2011 The Guava Authors
*
* 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.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Signifies that a public API element is intended for internal use only.
*
* <p>It is inadvisable for <i>applications</i> to use Internal APIs as they are intended for <b>internal library purposes</b> only.</p>
*/
@Retention(RetentionPolicy.CLASS)
@Target({
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.FIELD,
ElementType.METHOD,
ElementType.PACKAGE,
ElementType.TYPE })
@Documented
@Alpha(Reason.CLIENT)
public @interface Internal {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add an internal method MongoDriverInformation#getDriverInformationList to get the list of DriverInformation and added this annotation as a way to highlight things for internal use only.

If this ok, then I'll add a ticket to annotation each of the internal packages in their package-info.java

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have VisibleForTesting should we try to unify all these in a similar fashion to ApiStatus
(already supported in IntelliJ ~)

Image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt want to misuse that as it's not for testing. It's needed for the ClientMetaData API as well. This all stems from MongoDriverInformation being lossy, I need the combined information of name, version and platform.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2008-present 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.internal.client;

import com.mongodb.lang.Nullable;

import java.util.Objects;

public final class DriverInformation {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes required by the spec ensure that driver information as a whole can be checked for equality - so that duplicates aren't appended.

@Nullable
private final String driverName;
@Nullable
private final String driverVersion;
@Nullable
private final String driverPlatform;

public DriverInformation(@Nullable final String driverName,
@Nullable final String driverVersion,
@Nullable final String driverPlatform) {
this.driverName = driverName == null || driverName.isEmpty() ? null : driverName;
this.driverVersion = driverVersion == null || driverVersion.isEmpty() ? null : driverVersion;
this.driverPlatform = driverPlatform == null || driverPlatform.isEmpty() ? null : driverPlatform;
}

@Nullable
public String getDriverName() {
return driverName;
}

@Nullable
public String getDriverVersion() {
return driverVersion;
}

@Nullable
public String getDriverPlatform() {
return driverPlatform;
}

@Override
public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}

final DriverInformation that = (DriverInformation) o;
return Objects.equals(driverName, that.driverName)
&& Objects.equals(driverVersion, that.driverVersion)
&& Objects.equals(driverPlatform, that.driverPlatform);
}

@Override
public int hashCode() {
int result = Objects.hashCode(driverName);
result = 31 * result + Objects.hashCode(driverVersion);
result = 31 * result + Objects.hashCode(driverPlatform);
return result;
}

@Override
public String toString() {
return "DriverInformation{"
+ "driverName='" + driverName + '\''
+ ", driverVersion='" + driverVersion + '\''
+ ", driverPlatform='" + driverPlatform + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2008-present 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.internal.client;

import com.mongodb.internal.build.MongoDriverVersion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.mongodb.assertions.Assertions.fail;
import static java.lang.String.format;
import static java.lang.System.getProperty;

public final class DriverInformationHelper {

public static final DriverInformation INITIAL_DRIVER_INFORMATION =
new DriverInformation(MongoDriverVersion.NAME, MongoDriverVersion.VERSION,
format("Java/%s/%s", getProperty("java.vendor", "unknown-vendor"),
getProperty("java.runtime.version", "unknown-version")));

public static List<String> getNames(final List<DriverInformation> driverInformation) {
return getDriverField(DriverInformation::getDriverName, driverInformation);
}

public static List<String> getVersions(final List<DriverInformation> driverInformation) {
return getDriverInformation(DriverInformation::getDriverVersion, driverInformation);
}

public static List<String> getPlatforms(final List<DriverInformation> driverInformation) {
return getDriverField(DriverInformation::getDriverPlatform, driverInformation);
}

private static List<String> getDriverField(final Function<DriverInformation, String> fieldSupplier,
final List<DriverInformation> driverInformation) {
return Collections.unmodifiableList(driverInformation.stream()
.map(fieldSupplier)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
}

private DriverInformationHelper() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2008-present 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.
*/

/**
* This package contains classes for internal client functionality.
*/

@NonNullApi
package com.mongodb.internal.client;

import com.mongodb.lang.NonNullApi;
Loading