|
50 | 50 |
|
51 | 51 | /**
|
52 | 52 | * Central class for creating queries. It follows a fluent API style so that you can easily chain together multiple
|
53 |
| - * criteria. Static import of the 'Criteria.where' method will improve readability. |
| 53 | + * criteria. Static import of the {@link Criteria#where Criteria.where} method improves readability. |
54 | 54 | *
|
55 | 55 | * @author Thomas Risberg
|
56 | 56 | * @author Oliver Gierke
|
@@ -388,7 +388,22 @@ public Criteria type(Type... types) {
|
388 | 388 | Assert.notNull(types, "Types must not be null!");
|
389 | 389 | Assert.noNullElements(types, "Types must not contain null.");
|
390 | 390 |
|
391 |
| - criteria.put("$type", Arrays.asList(types).stream().map(Type::value).collect(Collectors.toList())); |
| 391 | + return type(Arrays.asList(types)); |
| 392 | + } |
| 393 | + |
| 394 | + /** |
| 395 | + * Creates a criterion using the {@literal $type} operator. |
| 396 | + * |
| 397 | + * @param types must not be {@literal null}. |
| 398 | + * @return this. |
| 399 | + * @since 3.2 |
| 400 | + * @see <a href="https://docs.mongodb.com/manual/reference/operator/query/type/">MongoDB Query operator: $type</a> |
| 401 | + */ |
| 402 | + public Criteria type(Collection<Type> types) { |
| 403 | + |
| 404 | + Assert.notNull(types, "Types must not be null!"); |
| 405 | + |
| 406 | + criteria.put("$type", types.stream().map(Type::value).collect(Collectors.toList())); |
392 | 407 | return this;
|
393 | 408 | }
|
394 | 409 |
|
@@ -669,67 +684,103 @@ public BitwiseCriteriaOperators bits() {
|
669 | 684 | }
|
670 | 685 |
|
671 | 686 | /**
|
672 |
| - * Creates an 'or' criteria using the $or operator for all of the provided criteria |
673 |
| - * <p> |
674 |
| - * Note that mongodb doesn't support an $or operator to be wrapped in a $not operator. |
| 687 | + * Creates a criteria using the {@code $or} operator for all of the provided criteria. |
675 | 688 | * <p>
|
| 689 | + * Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator. |
676 | 690 | *
|
677 |
| - * @throws IllegalArgumentException if {@link #orOperator(Criteria...)} follows a not() call directly. |
| 691 | + * @throws IllegalArgumentException if this method follows a {@link #not()} call directly. |
678 | 692 | * @param criteria must not be {@literal null}.
|
679 | 693 | * @return this.
|
680 | 694 | */
|
681 | 695 | public Criteria orOperator(Criteria... criteria) {
|
| 696 | + |
| 697 | + Assert.notNull(criteria, "Criteria must not be null!"); |
| 698 | + |
682 | 699 | return orOperator(Arrays.asList(criteria));
|
683 | 700 | }
|
684 | 701 |
|
685 | 702 | /**
|
686 |
| - * {@link #orOperator(Criteria...)} |
| 703 | + * Creates a criteria using the {@code $or} operator for all of the provided criteria. |
| 704 | + * <p> |
| 705 | + * Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator. |
| 706 | + * |
| 707 | + * @throws IllegalArgumentException if this method follows a {@link #not()} call directly. |
| 708 | + * @param criteria must not be {@literal null}. |
| 709 | + * @return this. |
| 710 | + * @since 3.2 |
687 | 711 | */
|
688 | 712 | public Criteria orOperator(Collection<Criteria> criteria) {
|
| 713 | + |
| 714 | + Assert.notNull(criteria, "Criteria must not be null!"); |
| 715 | + |
689 | 716 | BasicDBList bsonList = createCriteriaList(criteria);
|
690 | 717 | return registerCriteriaChainElement(new Criteria("$or").is(bsonList));
|
691 | 718 | }
|
692 | 719 |
|
693 | 720 | /**
|
694 |
| - * Creates a 'nor' criteria using the $nor operator for all of the provided criteria. |
695 |
| - * <p> |
696 |
| - * Note that mongodb doesn't support an $nor operator to be wrapped in a $not operator. |
| 721 | + * Creates a criteria using the {@code $nor} operator for all of the provided criteria. |
697 | 722 | * <p>
|
| 723 | + * Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator. |
698 | 724 | *
|
699 |
| - * @throws IllegalArgumentException if {@link #norOperator(Criteria...)} follows a not() call directly. |
| 725 | + * @throws IllegalArgumentException if this method follows a {@link #not()} call directly. |
700 | 726 | * @param criteria must not be {@literal null}.
|
701 | 727 | * @return this.
|
702 | 728 | */
|
703 | 729 | public Criteria norOperator(Criteria... criteria) {
|
| 730 | + |
| 731 | + Assert.notNull(criteria, "Criteria must not be null!"); |
| 732 | + |
704 | 733 | return norOperator(Arrays.asList(criteria));
|
705 | 734 | }
|
706 | 735 |
|
707 | 736 | /**
|
708 |
| - * {@link #norOperator(Criteria...)} |
| 737 | + * Creates a criteria using the {@code $nor} operator for all of the provided criteria. |
| 738 | + * <p> |
| 739 | + * Note that MongoDB doesn't support an {@code $nor} operator to be wrapped in a {@code $not} operator. |
| 740 | + * |
| 741 | + * @throws IllegalArgumentException if this method follows a {@link #not()} call directly. |
| 742 | + * @param criteria must not be {@literal null}. |
| 743 | + * @return this. |
| 744 | + * @since 3.2 |
709 | 745 | */
|
710 | 746 | public Criteria norOperator(Collection<Criteria> criteria) {
|
| 747 | + |
| 748 | + Assert.notNull(criteria, "Criteria must not be null!"); |
| 749 | + |
711 | 750 | BasicDBList bsonList = createCriteriaList(criteria);
|
712 | 751 | return registerCriteriaChainElement(new Criteria("$nor").is(bsonList));
|
713 | 752 | }
|
714 | 753 |
|
715 | 754 | /**
|
716 |
| - * Creates an 'and' criteria using the $and operator for all of the provided criteria. |
717 |
| - * <p> |
718 |
| - * Note that mongodb doesn't support an $and operator to be wrapped in a $not operator. |
| 755 | + * Creates a criteria using the {@code $and} operator for all of the provided criteria. |
719 | 756 | * <p>
|
| 757 | + * Note that MongoDB doesn't support an {@code $and} operator to be wrapped in a {@code $not} operator. |
720 | 758 | *
|
721 |
| - * @throws IllegalArgumentException if {@link #andOperator(Criteria...)} follows a not() call directly. |
| 759 | + * @throws IllegalArgumentException if this method follows a {@link #not()} call directly. |
722 | 760 | * @param criteria must not be {@literal null}.
|
723 | 761 | * @return this.
|
724 | 762 | */
|
725 | 763 | public Criteria andOperator(Criteria... criteria) {
|
| 764 | + |
| 765 | + Assert.notNull(criteria, "Criteria must not be null!"); |
| 766 | + |
726 | 767 | return andOperator(Arrays.asList(criteria));
|
727 | 768 | }
|
728 | 769 |
|
729 | 770 | /**
|
730 |
| - * {@link #andOperator(Criteria...)} |
| 771 | + * Creates a criteria using the {@code $and} operator for all of the provided criteria. |
| 772 | + * <p> |
| 773 | + * Note that MongoDB doesn't support an {@code $and} operator to be wrapped in a {@code $not} operator. |
| 774 | + * |
| 775 | + * @throws IllegalArgumentException if this method follows a {@link #not()} call directly. |
| 776 | + * @param criteria must not be {@literal null}. |
| 777 | + * @return this. |
| 778 | + * @since 3.2 |
731 | 779 | */
|
732 | 780 | public Criteria andOperator(Collection<Criteria> criteria) {
|
| 781 | + |
| 782 | + Assert.notNull(criteria, "Criteria must not be null!"); |
| 783 | + |
733 | 784 | BasicDBList bsonList = createCriteriaList(criteria);
|
734 | 785 | return registerCriteriaChainElement(new Criteria("$and").is(bsonList));
|
735 | 786 | }
|
@@ -832,11 +883,13 @@ private BasicDBList createCriteriaList(Collection<Criteria> criteria) {
|
832 | 883 | }
|
833 | 884 |
|
834 | 885 | private void setValue(Document document, String key, Object value) {
|
| 886 | + |
835 | 887 | Object existing = document.get(key);
|
| 888 | + |
836 | 889 | if (existing == null) {
|
837 | 890 | document.put(key, value);
|
838 | 891 | } else {
|
839 |
| - throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDocument, " |
| 892 | + throw new InvalidMongoDbApiUsageException("Due to limitations of the org.bson.Document, " |
840 | 893 | + "you can't add a second '" + key + "' expression specified as '" + key + " : " + value + "'. "
|
841 | 894 | + "Criteria already contains '" + key + " : " + existing + "'.");
|
842 | 895 | }
|
|
0 commit comments