LUCENE-4413 Standardize on throwing UnsupportedOperationException for a shape the strategy doesn't support. Document it.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1388872 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Wayne Smiley 2012-09-22 19:12:04 +00:00
parent 255e8f1aa8
commit a7979cf3f7
4 changed files with 15 additions and 8 deletions

View File

@ -96,6 +96,7 @@ public abstract class SpatialStrategy {
* since it doesn't use it. * since it doesn't use it.
* *
* @return Not null nor will it have null elements. * @return Not null nor will it have null elements.
* @throws UnsupportedOperationException if given a shape incompatible with the strategy
*/ */
public abstract Field[] createIndexableFields(Shape shape); public abstract Field[] createIndexableFields(Shape shape);
@ -111,6 +112,10 @@ public abstract class SpatialStrategy {
* and {@link Shape} from the supplied {@code args}. * and {@link Shape} from the supplied {@code args}.
* The default implementation is * The default implementation is
* <pre>return new ConstantScoreQuery(makeFilter(args));</pre> * <pre>return new ConstantScoreQuery(makeFilter(args));</pre>
*
* @throws UnsupportedOperationException If the strategy does not support the shape in {@code args}
* @throws org.apache.lucene.spatial.query.UnsupportedSpatialOperation If the strategy does not support the {@link
* org.apache.lucene.spatial.query.SpatialOperation} in {@code args}.
*/ */
public ConstantScoreQuery makeQuery(SpatialArgs args) { public ConstantScoreQuery makeQuery(SpatialArgs args) {
return new ConstantScoreQuery(makeFilter(args)); return new ConstantScoreQuery(makeFilter(args));
@ -124,6 +129,10 @@ public abstract class SpatialStrategy {
* {@link #makeQuery(org.apache.lucene.spatial.query.SpatialArgs)} * {@link #makeQuery(org.apache.lucene.spatial.query.SpatialArgs)}
* then this method could be simply: * then this method could be simply:
* <pre>return new QueryWrapperFilter(makeQuery(args).getQuery());</pre> * <pre>return new QueryWrapperFilter(makeQuery(args).getQuery());</pre>
*
* @throws UnsupportedOperationException If the strategy does not support the shape in {@code args}
* @throws org.apache.lucene.spatial.query.UnsupportedSpatialOperation If the strategy does not support the {@link
* org.apache.lucene.spatial.query.SpatialOperation} in {@code args}.
*/ */
public abstract Filter makeFilter(SpatialArgs args); public abstract Filter makeFilter(SpatialArgs args);

View File

@ -95,7 +95,7 @@ public class BBoxStrategy extends SpatialStrategy {
public Field[] createIndexableFields(Shape shape) { public Field[] createIndexableFields(Shape shape) {
if (shape instanceof Rectangle) if (shape instanceof Rectangle)
return createIndexableFields((Rectangle)shape); return createIndexableFields((Rectangle)shape);
throw new IllegalArgumentException("Can only index Rectangle, not " + shape); throw new UnsupportedOperationException("Can only index Rectangle, not " + shape);
} }
public Field[] createIndexableFields(Rectangle bbox) { public Field[] createIndexableFields(Rectangle bbox) {
@ -150,7 +150,7 @@ public class BBoxStrategy extends SpatialStrategy {
private Query makeSpatialQuery(SpatialArgs args) { private Query makeSpatialQuery(SpatialArgs args) {
Shape shape = args.getShape(); Shape shape = args.getShape();
if (!(shape instanceof Rectangle)) if (!(shape instanceof Rectangle))
throw new IllegalArgumentException("Can only query by Rectangle, not " + shape); throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);
Rectangle bbox = (Rectangle) shape; Rectangle bbox = (Rectangle) shape;
Query spatial = null; Query spatial = null;

View File

@ -18,7 +18,6 @@ package org.apache.lucene.spatial.vector;
*/ */
import com.spatial4j.core.context.SpatialContext; import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.exception.InvalidShapeException;
import com.spatial4j.core.shape.Circle; import com.spatial4j.core.shape.Circle;
import com.spatial4j.core.shape.Point; import com.spatial4j.core.shape.Point;
import com.spatial4j.core.shape.Rectangle; import com.spatial4j.core.shape.Rectangle;
@ -90,7 +89,7 @@ public class TwoDoublesStrategy extends SpatialStrategy {
public Field[] createIndexableFields(Shape shape) { public Field[] createIndexableFields(Shape shape) {
if (shape instanceof Point) if (shape instanceof Point)
return createIndexableFields((Point) shape); return createIndexableFields((Point) shape);
throw new IllegalArgumentException("Can only index Point, not " + shape); throw new UnsupportedOperationException("Can only index Point, not " + shape);
} }
/** @see #createIndexableFields(com.spatial4j.core.shape.Shape) */ /** @see #createIndexableFields(com.spatial4j.core.shape.Shape) */
@ -139,7 +138,7 @@ public class TwoDoublesStrategy extends SpatialStrategy {
circle.getRadius() ); circle.getRadius() );
return new ConstantScoreQuery(vsf); return new ConstantScoreQuery(vsf);
} else { } else {
throw new InvalidShapeException("Only Rectangles and Circles are currently supported, " + throw new UnsupportedOperationException("Only Rectangles and Circles are currently supported, " +
"found [" + shape.getClass() + "]");//TODO "found [" + shape.getClass() + "]");//TODO
} }
} }
@ -149,7 +148,7 @@ public class TwoDoublesStrategy extends SpatialStrategy {
// For starters, just limit the bbox // For starters, just limit the bbox
Shape shape = args.getShape(); Shape shape = args.getShape();
if (!(shape instanceof Rectangle || shape instanceof Circle)) { if (!(shape instanceof Rectangle || shape instanceof Circle)) {
throw new InvalidShapeException("Only Rectangles and Circles are currently supported, " + throw new UnsupportedOperationException("Only Rectangles and Circles are currently supported, " +
"found [" + shape.getClass() + "]");//TODO "found [" + shape.getClass() + "]");//TODO
} }

View File

@ -18,7 +18,6 @@ package org.apache.lucene.spatial.vector;
*/ */
import com.spatial4j.core.context.SpatialContext; import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.exception.InvalidShapeException;
import com.spatial4j.core.shape.Circle; import com.spatial4j.core.shape.Circle;
import com.spatial4j.core.shape.Point; import com.spatial4j.core.shape.Point;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
@ -50,7 +49,7 @@ public class TestTwoDoublesStrategy extends StrategyTestCase {
assertNotNull(query); assertNotNull(query);
} }
@Test(expected = InvalidShapeException.class) @Test(expected = UnsupportedOperationException.class)
public void testInvalidQueryShape() { public void testInvalidQueryShape() {
Point point = ctx.makePoint(0, 0); Point point = ctx.makePoint(0, 0);
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, point); SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, point);