/driver/src/main/com/mongodb/DistinctIterableImpl.java

https://github.com/foursquare/mongo-java-driver · Java · 121 lines · 87 code · 18 blank · 16 comment · 1 complexity · 6c3a11bc897203e866c3f8801399afaf MD5 · raw file

  1. /*
  2. * Copyright 2015 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;
  17. import com.mongodb.client.DistinctIterable;
  18. import com.mongodb.client.MongoCursor;
  19. import com.mongodb.client.MongoIterable;
  20. import com.mongodb.client.model.Collation;
  21. import com.mongodb.operation.DistinctOperation;
  22. import com.mongodb.operation.OperationExecutor;
  23. import org.bson.codecs.configuration.CodecRegistry;
  24. import org.bson.conversions.Bson;
  25. import java.util.Collection;
  26. import java.util.concurrent.TimeUnit;
  27. import static com.mongodb.assertions.Assertions.notNull;
  28. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  29. class DistinctIterableImpl<TDocument, TResult> implements DistinctIterable<TResult> {
  30. private final MongoNamespace namespace;
  31. private final Class<TDocument> documentClass;
  32. private final Class<TResult> resultClass;
  33. private final ReadPreference readPreference;
  34. private final ReadConcern readConcern;
  35. private final CodecRegistry codecRegistry;
  36. private final OperationExecutor executor;
  37. private final String fieldName;
  38. private Bson filter;
  39. private long maxTimeMS;
  40. private Collation collation;
  41. DistinctIterableImpl(final MongoNamespace namespace, final Class<TDocument> documentClass, final Class<TResult> resultClass,
  42. final CodecRegistry codecRegistry, final ReadPreference readPreference, final ReadConcern readConcern,
  43. final OperationExecutor executor, final String fieldName, final Bson filter) {
  44. this.namespace = notNull("namespace", namespace);
  45. this.documentClass = notNull("documentClass", documentClass);
  46. this.resultClass = notNull("resultClass", resultClass);
  47. this.codecRegistry = notNull("codecRegistry", codecRegistry);
  48. this.readPreference = notNull("readPreference", readPreference);
  49. this.readConcern = notNull("readConcern", readConcern);
  50. this.executor = notNull("executor", executor);
  51. this.fieldName = notNull("mapFunction", fieldName);
  52. this.filter = filter;
  53. }
  54. @Override
  55. public DistinctIterable<TResult> filter(final Bson filter) {
  56. this.filter = filter;
  57. return this;
  58. }
  59. @Override
  60. public DistinctIterable<TResult> maxTime(final long maxTime, final TimeUnit timeUnit) {
  61. notNull("timeUnit", timeUnit);
  62. this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit);
  63. return this;
  64. }
  65. @Override
  66. public DistinctIterable<TResult> batchSize(final int batchSize) {
  67. // Noop - not supported by DistinctIterable
  68. return this;
  69. }
  70. @Override
  71. public DistinctIterable<TResult> collation(final Collation collation) {
  72. this.collation = collation;
  73. return this;
  74. }
  75. @Override
  76. public MongoCursor<TResult> iterator() {
  77. return execute().iterator();
  78. }
  79. @Override
  80. public TResult first() {
  81. return execute().first();
  82. }
  83. @Override
  84. public <U> MongoIterable<U> map(final Function<TResult, U> mapper) {
  85. return new MappingIterable<TResult, U>(this, mapper);
  86. }
  87. @Override
  88. public void forEach(final Block<? super TResult> block) {
  89. execute().forEach(block);
  90. }
  91. @Override
  92. public <A extends Collection<? super TResult>> A into(final A target) {
  93. return execute().into(target);
  94. }
  95. private MongoIterable<TResult> execute() {
  96. DistinctOperation<TResult> operation = new DistinctOperation<TResult>(namespace, fieldName, codecRegistry.get(resultClass))
  97. .filter(filter == null ? null : filter.toBsonDocument(documentClass, codecRegistry))
  98. .maxTime(maxTimeMS, MILLISECONDS)
  99. .readConcern(readConcern)
  100. .collation(collation);
  101. return new OperationIterable<TResult>(operation, readPreference, executor);
  102. }
  103. }