/driver-core/src/main/com/mongodb/internal/client/model/Util.java

https://github.com/jyemin/mongo-java-driver · Java · 77 lines · 46 code · 7 blank · 24 comment · 7 complexity · db384b6eb7c511a6abd82bab8553a6e5 MD5 · raw file

  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.model;
  17. import com.mongodb.Function;
  18. import com.mongodb.client.model.search.FieldSearchPath;
  19. import com.mongodb.client.model.search.SearchPath;
  20. import org.bson.BsonArray;
  21. import org.bson.BsonString;
  22. import org.bson.BsonValue;
  23. import java.util.Iterator;
  24. import static com.mongodb.assertions.Assertions.fail;
  25. public final class Util {
  26. public static final String SEARCH_PATH_VALUE_KEY = "value";
  27. /**
  28. * If {@code nonEmptyPaths} has exactly one element, then returns the result of {@link SearchPath#toBsonValue()},
  29. * otherwise returns a {@link BsonArray} of such results.
  30. *
  31. * @param nonEmptyPaths One or more {@link SearchPath} to convert.
  32. * @param valueOnly If {@code true}, then {@link FieldSearchPath#toValue()} is used when possible;
  33. * if {@code false}, then {@link SearchPath#toBsonValue()} is used.
  34. * @return A single {@link BsonValue} representing the specified paths.
  35. */
  36. public static BsonValue combineToBsonValue(final Iterator<? extends SearchPath> nonEmptyPaths, final boolean valueOnly) {
  37. Function<SearchPath, BsonValue> toBsonValueFunc = valueOnly
  38. ? path -> {
  39. if (path instanceof FieldSearchPath) {
  40. return new BsonString(((FieldSearchPath) path).toValue());
  41. } else {
  42. return path.toBsonValue();
  43. }
  44. }
  45. : SearchPath::toBsonValue;
  46. BsonValue firstPath = toBsonValueFunc.apply(nonEmptyPaths.next());
  47. if (nonEmptyPaths.hasNext()) {
  48. BsonArray bsonArray = new BsonArray();
  49. bsonArray.add(firstPath);
  50. while (nonEmptyPaths.hasNext()) {
  51. bsonArray.add(toBsonValueFunc.apply(nonEmptyPaths.next()));
  52. }
  53. return bsonArray;
  54. } else {
  55. return firstPath;
  56. }
  57. }
  58. public static boolean sizeAtLeast(final Iterable<?> iterable, final int minInclusive) {
  59. Iterator<?> iter = iterable.iterator();
  60. int size = 0;
  61. while (size < minInclusive && iter.hasNext()) {
  62. iter.next();
  63. size++;
  64. }
  65. return size >= minInclusive;
  66. }
  67. private Util() {
  68. throw fail();
  69. }
  70. }