/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveInsertOperationSupport.java

https://github.com/spring-projects/spring-data-mongodb · Java · 90 lines · 45 code · 23 blank · 22 comment · 0 complexity · b3e98687a012c2d65f86c82a8de7c4e9 MD5 · raw file

  1. /*
  2. * Copyright 2017-2022 the original author or authors.
  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. * https://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 org.springframework.data.mongodb.core;
  17. import reactor.core.publisher.Flux;
  18. import reactor.core.publisher.Mono;
  19. import java.util.Collection;
  20. import org.springframework.util.Assert;
  21. import org.springframework.util.StringUtils;
  22. /**
  23. * Implementation of {@link ReactiveInsertOperation}.
  24. *
  25. * @author Mark Paluch
  26. * @author Christoph Strobl
  27. * @since 2.0
  28. */
  29. class ReactiveInsertOperationSupport implements ReactiveInsertOperation {
  30. private final ReactiveMongoTemplate template;
  31. ReactiveInsertOperationSupport(ReactiveMongoTemplate template) {
  32. this.template = template;
  33. }
  34. @Override
  35. public <T> ReactiveInsert<T> insert(Class<T> domainType) {
  36. Assert.notNull(domainType, "DomainType must not be null");
  37. return new ReactiveInsertSupport<>(template, domainType, null);
  38. }
  39. static class ReactiveInsertSupport<T> implements ReactiveInsert<T> {
  40. private final ReactiveMongoTemplate template;
  41. private final Class<T> domainType;
  42. private final String collection;
  43. ReactiveInsertSupport(ReactiveMongoTemplate template, Class<T> domainType, String collection) {
  44. this.template = template;
  45. this.domainType = domainType;
  46. this.collection = collection;
  47. }
  48. @Override
  49. public Mono<T> one(T object) {
  50. Assert.notNull(object, "Object must not be null");
  51. return template.insert(object, getCollectionName());
  52. }
  53. @Override
  54. public Flux<T> all(Collection<? extends T> objects) {
  55. Assert.notNull(objects, "Objects must not be null");
  56. return template.insert(objects, getCollectionName());
  57. }
  58. @Override
  59. public ReactiveInsert<T> inCollection(String collection) {
  60. Assert.hasText(collection, "Collection must not be null nor empty");
  61. return new ReactiveInsertSupport<>(template, domainType, collection);
  62. }
  63. private String getCollectionName() {
  64. return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
  65. }
  66. }
  67. }