Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-2014 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
   *
   *			http://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  org.springframework.util.Assert;
 
Builder class to build near-queries.

Author(s):
Oliver Gierke
Thomas Darimont
 
 public final class NearQuery {
 
 	private final Point point;
 	private Query query;
 	private Distance maxDistance;
 	private Metric metric;
 	private boolean spherical;
 	private Integer num;
 	private Integer skip;

Creates a new NearQuery.

Parameters:
point
 
 	private NearQuery(Point pointMetric metric) {
 
 		Assert.notNull(point);
 
 		this. = point;
 		this. = false;
 
 		if (metric != null) {
 			in(metric);
 		}
 	}

Creates a new NearQuery starting near the given coordinates.

Parameters:
i
j
Returns:
 
 	public static NearQuery near(double xdouble y) {
 		return near(xynull);
 	}

Creates a new NearQuery starting at the given coordinates using the given Metric to adapt given values to further configuration. E.g. setting a maxDistance(double) will be interpreted as a value of the initially set Metric.

Parameters:
x
y
metric
Returns:
 
 	public static NearQuery near(double xdouble yMetric metric) {
 		return near(new Point(xy), metric);
 	}

Creates a new NearQuery starting at the given Point.

Parameters:
point must not be null.
Returns:
 
 	public static NearQuery near(Point point) {
 		return near(pointnull);
 	}

Creates a NearQuery starting near the given Point using the given Metric to adapt given values to further configuration. E.g. setting a maxDistance(double) will be interpreted as a value of the initially set Metric.

Parameters:
point must not be null.
metric
Returns:
	public static NearQuery near(Point pointMetric metric) {
		Assert.notNull(point);
		return new NearQuery(pointmetric);
	}

Returns the Metric underlying the actual query. If no metric was set explicitly Metrics.NEUTRAL will be returned.

Returns:
will never be null.
	public Metric getMetric() {
		return  == null ? . : ;
	}

Configures the maximum number of results to return.

Parameters:
num
Returns:
	public NearQuery num(int num) {
		this. = num;
		return this;
	}

Configures the number of results to skip.

Parameters:
skip
Returns:
	public NearQuery skip(int skip) {
		this. = skip;
		return this;
	}

Configures the Pageable to use.

Parameters:
pageable
Returns:
	public NearQuery with(Pageable pageable) {
		this. = pageable.getOffset() + pageable.getPageSize();
		this. = pageable.getOffset();
		return this;
	}

Sets the max distance results shall have from the configured origin. If a Metric was set before the given value will be interpreted as being a value in that metric. E.g.
 NearQuery query = near(10.0, 20.0, Metrics.KILOMETERS).maxDistance(150);
 
Will set the maximum distance to 150 kilometers.

Parameters:
maxDistance
Returns:
	public NearQuery maxDistance(double maxDistance) {
		return maxDistance(new Distance(maxDistancegetMetric()));
	}

Sets the maximum distance supplied in a given metric. Will normalize the distance but not reconfigure the query's result Metric if one was configured before.

Parameters:
maxDistance
metric must not be null.
Returns:
	public NearQuery maxDistance(double maxDistanceMetric metric) {
		Assert.notNull(metric);
		return maxDistance(new Distance(maxDistancemetric));
	}

Sets the maximum distance to the given Distance. Will set the returned Metric to be the one of the given Distance if no Metric was set before.

Parameters:
distance must not be null.
Returns:
	public NearQuery maxDistance(Distance distance) {
		Assert.notNull(distance);
		if (distance.getMetric() != .) {
			this.spherical(true);
		}
		if (this. == null) {
			in(distance.getMetric());
		}
		this. = distance;
		return this;
	}

Returns the maximum Distance.

Returns:
		return this.;
	}

Configures a CustomMetric with the given multiplier.

Parameters:
distanceMultiplier
Returns:
	public NearQuery distanceMultiplier(double distanceMultiplier) {
		this. = new CustomMetric(distanceMultiplier);
		return this;
	}

Configures the distance multiplier to the multiplier of the given Metric.

Deprecated:
use in(Metric) instead.
Parameters:
metric must not be null.
Returns:
	public NearQuery distanceMultiplier(Metric metric) {
		Assert.notNull(metric);
		return in(metric);
	}

Configures whether to return spherical values for the actual distance.

Parameters:
spherical
Returns:
	public NearQuery spherical(boolean spherical) {
		this. = spherical;
		return this;
	}

Returns whether spharical values will be returned.

Returns:
	public boolean isSpherical() {
		return this.;
	}

Will cause the results' distances being returned in kilometers. Sets distanceMultiplier(double) and spherical(boolean) accordingly.

Returns:
	public NearQuery inKilometers() {
	}

Will cause the results' distances being returned in miles. Sets distanceMultiplier(double) and spherical(boolean) accordingly.

Returns:
	public NearQuery inMiles() {
	}

Will cause the results' distances being returned in the given metric. Sets distanceMultiplier(double) accordingly as well as spherical(boolean) if the given Metric is not Metrics.NEUTRAL.

Parameters:
metric the metric the results shall be returned in. Uses Metrics.NEUTRAL if null is passed.
Returns:
	public NearQuery in(Metric metric) {
		return adaptMetric(metric == null ? . : metric);
	}

Configures the given Metric to be used as base on for this query and recalculate the maximum distance if no metric was set before.

Parameters:
metric
	private NearQuery adaptMetric(Metric metric) {
		if (metric != .) {
			spherical(true);
		}
		this. = metric;
		return this;
	}

Adds an actual query to the NearQuery to restrict the objects considered for the actual near operation.

Parameters:
query
Returns:
	public NearQuery query(Query query) {
		this. = query;
		this. = query.getSkip();
		this. = query.getLimit();
		return this;
	}

Returns:
the number of elements to skip.
	public Integer getSkip() {
		return ;
	}

Returns the DBObject built by the NearQuery.

Returns:
	public DBObject toDBObject() {
		BasicDBObject dbObject = new BasicDBObject();
		if ( != null) {
			dbObject.put("query".getQueryObject());
		}
		if ( != null) {
			dbObject.put("maxDistance"this..getNormalizedValue());
		}
		if ( != null) {
			dbObject.put("distanceMultiplier".getMultiplier());
		}
		if ( != null) {
			dbObject.put("num");
		}
		dbObject.put("near".asList());
		dbObject.put("spherical");
		return dbObject;
	}
New to GrepCode? Check out our FAQ X