Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2010-2022 the original author or 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
*
* https://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 org.springframework.data.mongodb.core.query;
import static org.springframework.util.ObjectUtils.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.bson.BsonRegularExpression;
import org.bson.BsonType;
import org.bson.Document;
import org.bson.types.Binary;
import org.springframework.data.domain.Example;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.Shape;
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
import org.springframework.data.mongodb.core.geo.GeoJson;
import org.springframework.data.mongodb.core.geo.Sphere;
import org.springframework.data.mongodb.core.schema.JsonSchemaObject.Type;
import org.springframework.data.mongodb.core.schema.JsonSchemaProperty;
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
import org.springframework.data.mongodb.util.RegexFlags;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.mongodb.BasicDBList;
/**
* Central class for creating queries. It follows a fluent API style so that you can easily chain together multiple
* criteria. Static import of the {@link Criteria#where Criteria.where} method improves readability.
*
* @author Thomas Risberg
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Andreas Zink
* @author Ziemowit Stolarczyk
* @author Clément Petit
* @author James McNee
*/
public class Criteria implements CriteriaDefinition {
/**
* Custom "not-null" object as we have to be able to work with {@literal null} values as well.
*/
private static final Object NOT_SET = new Object();
private @Nullable String key;
private List criteriaChain;
private LinkedHashMap criteria = new LinkedHashMap();
private @Nullable Object isValue = NOT_SET;
public Criteria() {
this.criteriaChain = new ArrayList();
}
public Criteria(String key) {
this.criteriaChain = new ArrayList();
this.criteriaChain.add(this);
this.key = key;
}
protected Criteria(List criteriaChain, String key) {
this.criteriaChain = criteriaChain;
this.criteriaChain.add(this);
this.key = key;
}
/**
* Static factory method to create a Criteria using the provided key
*
* @param key the property or field name.
* @return new instance of {@link Criteria}.
*/
public static Criteria where(String key) {
return new Criteria(key);
}
/**
* Static factory method to create a {@link Criteria} matching an example object.
*
* @param example must not be {@literal null}.
* @return new instance of {@link Criteria}.
* @see Criteria#alike(Example)
* @since 1.8
*/
public static Criteria byExample(Object example) {
return byExample(Example.of(example));
}
/**
* Static factory method to create a {@link Criteria} matching an example object.
* By default the {@link Example} uses typed matching restricting it to probe assignable types. For example, when
* sticking with the default type key ({@code _class}), the query has restrictions such as
* _class : { $in : [com.acme.Person] } .
* To avoid the above mentioned type restriction use an {@link UntypedExampleMatcher} with
* {@link Example#of(Object, org.springframework.data.domain.ExampleMatcher)}.
*
* @param example must not be {@literal null}.
* @return new instance of {@link Criteria}.
* @see Criteria#alike(Example)
* @since 1.8
*/
public static Criteria byExample(Example example) {
return new Criteria().alike(example);
}
/**
* Static factory method to create a {@link Criteria} matching documents against a given structure defined by the
* {@link MongoJsonSchema} using ({@code $jsonSchema}) operator.
*
* @param schema must not be {@literal null}.
* @return this
* @since 2.1
* @see MongoDB Query operator:
* $jsonSchema
*/
public static Criteria matchingDocumentStructure(MongoJsonSchema schema) {
return new Criteria().andDocumentStructureMatches(schema);
}
/**
* Static factory method to create a Criteria using the provided key
*
* @return new instance of {@link Criteria}.
*/
public Criteria and(String key) {
return new Criteria(this.criteriaChain, key);
}
/**
* Creates a criterion using equality
*
* @param value can be {@literal null}.
* @return this.
*/
public Criteria is(@Nullable Object value) {
if (!isValue.equals(NOT_SET)) {
throw new InvalidMongoDbApiUsageException(
"Multiple 'is' values declared. You need to use 'and' with multiple criteria");
}
if (lastOperatorWasNot()) {
throw new InvalidMongoDbApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead.");
}
this.isValue = value;
return this;
}
/**
* Creates a criterion using {@literal null} equality comparison which matches documents that either contain the item
* field whose value is {@literal null} or that do not contain the item field.
*
* Use {@link #isNullValue()} to only query for documents that contain the field whose value is equal to
* {@link org.bson.BsonType#NULL}.
* Use {@link #exists(boolean)} to query for documents that do (not) contain the field.
*
* @return this.
* @see Query for Null or
* Missing Fields: Equality Filter
* @since 3.3
*/
public Criteria isNull() {
return is(null);
}
/**
* Creates a criterion using a {@link org.bson.BsonType} comparison which matches only documents that contain the item
* field whose value is equal to {@link org.bson.BsonType#NULL}.
*
* Use {@link #isNull()} to query for documents that contain the field with a {@literal null} value or do not contain the
* field at all.
* Use {@link #exists(boolean)} to query for documents that do (not) contain the field.
*
* @return this.
* @see Query for Null or Missing
* Fields: Type Check
* @since 3.3
*/
public Criteria isNullValue() {
criteria.put("$type", BsonType.NULL.getValue());
return this;
}
private boolean lastOperatorWasNot() {
return !this.criteria.isEmpty() && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1]);
}
/**
* Creates a criterion using the {@literal $ne} operator.
*
* @param value can be {@literal null}.
* @return this.
* @see MongoDB Query operator: $ne
*/
public Criteria ne(@Nullable Object value) {
criteria.put("$ne", value);
return this;
}
/**
* Creates a criterion using the {@literal $lt} operator.
*
* @param value must not be {@literal null}.
* @return this.
* @see MongoDB Query operator: $lt
*/
public Criteria lt(Object value) {
criteria.put("$lt", value);
return this;
}
/**
* Creates a criterion using the {@literal $lte} operator.
*
* @param value must not be {@literal null}.
* @return this.
* @see MongoDB Query operator: $lte
*/
public Criteria lte(Object value) {
criteria.put("$lte", value);
return this;
}
/**
* Creates a criterion using the {@literal $gt} operator.
*
* @param value must not be {@literal null}.
* @return this.
* @see MongoDB Query operator: $gt
*/
public Criteria gt(Object value) {
criteria.put("$gt", value);
return this;
}
/**
* Creates a criterion using the {@literal $gte} operator.
*
* @param value can be {@literal null}.
* @return this.
* @see MongoDB Query operator: $gte
*/
public Criteria gte(Object value) {
criteria.put("$gte", value);
return this;
}
/**
* Creates a criterion using the {@literal $in} operator.
*
* @param values the values to match against
* @return this.
* @see MongoDB Query operator: $in
*/
public Criteria in(Object... values) {
if (values.length > 1 && values[1] instanceof Collection) {
throw new InvalidMongoDbApiUsageException(
"You can only pass in one argument of type " + values[1].getClass().getName());
}
criteria.put("$in", Arrays.asList(values));
return this;
}
/**
* Creates a criterion using the {@literal $in} operator.
*
* @param values the collection containing the values to match against
* @return this.
* @see MongoDB Query operator: $in
*/
public Criteria in(Collection values) {
criteria.put("$in", values);
return this;
}
/**
* Creates a criterion using the {@literal $nin} operator.
*
* @param values
* @return this.
* @see MongoDB Query operator: $nin
*/
public Criteria nin(Object... values) {
return nin(Arrays.asList(values));
}
/**
* Creates a criterion using the {@literal $nin} operator.
*
* @param values must not be {@literal null}.
* @return this.
* @see MongoDB Query operator: $nin
*/
public Criteria nin(Collection values) {
criteria.put("$nin", values);
return this;
}
/**
* Creates a criterion using the {@literal $mod} operator.
*
* @param value must not be {@literal null}.
* @param remainder must not be {@literal null}.
* @return this.
* @see MongoDB Query operator: $mod
*/
public Criteria mod(Number value, Number remainder) {
List