Use static int for easier to read code

This commit is contained in:
Brusic 2014-03-06 08:30:55 -08:00 committed by Martijn van Groningen
parent 7c10b445d4
commit 94074b4028
1 changed files with 23 additions and 12 deletions

View File

@ -29,17 +29,19 @@ import java.io.IOException;
*/ */
public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanNotQueryBuilder> { public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanNotQueryBuilder> {
public static final int NOT_SET = -1;
private SpanQueryBuilder include; private SpanQueryBuilder include;
private SpanQueryBuilder exclude; private SpanQueryBuilder exclude;
private int dist = -1; private int dist = NOT_SET;
private int pre = -1; private int pre = NOT_SET;
private int post = -1; private int post = NOT_SET;
private float boost = -1; private float boost = NOT_SET;
private String queryName; private String queryName;
@ -59,12 +61,12 @@ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBu
} }
public SpanNotQueryBuilder pre(int pre) { public SpanNotQueryBuilder pre(int pre) {
this.pre = pre; this.pre = (pre >=0) ? pre : 0;
return this; return this;
} }
public SpanNotQueryBuilder post(int post) { public SpanNotQueryBuilder post(int post) {
this.post = post; this.post = (post >= 0) ? post : 0;
return this; return this;
} }
@ -75,6 +77,8 @@ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBu
/** /**
* Sets the query name for the filter that can be used when searching for matched_filters per hit. * Sets the query name for the filter that can be used when searching for matched_filters per hit.
* @param queryName The query name
* @return this
*/ */
public SpanNotQueryBuilder queryName(String queryName) { public SpanNotQueryBuilder queryName(String queryName) {
this.queryName = queryName; this.queryName = queryName;
@ -90,8 +94,15 @@ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBu
throw new ElasticsearchIllegalArgumentException("Must specify exclude when using spanNot query"); throw new ElasticsearchIllegalArgumentException("Must specify exclude when using spanNot query");
} }
if ((dist != -1 && (pre != -1 || post != -1)) || (pre != -1 && post == -1) || (pre == -1 && post != -1)) { if (dist != NOT_SET && (pre != NOT_SET || post != NOT_SET)) {
throw new ElasticSearchIllegalArgumentException("spanNot can either use [dist] or [pre] & [post] (or none)"); throw new ElasticsearchIllegalArgumentException("spanNot can either use [dist] or [pre] & [post] (or none)");
}
// set appropriate defaults
if (pre != NOT_SET && post == NOT_SET) {
post = 0;
} else if (pre == NOT_SET && post != NOT_SET){
pre = 0;
} }
builder.startObject(SpanNotQueryParser.NAME); builder.startObject(SpanNotQueryParser.NAME);
@ -99,16 +110,16 @@ public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBu
include.toXContent(builder, params); include.toXContent(builder, params);
builder.field("exclude"); builder.field("exclude");
exclude.toXContent(builder, params); exclude.toXContent(builder, params);
if (dist != -1) { if (dist != NOT_SET) {
builder.field("dist", dist); builder.field("dist", dist);
} }
if (pre != -1) { if (pre != NOT_SET) {
builder.field("pre", pre); builder.field("pre", pre);
} }
if (post != -1) { if (post != NOT_SET) {
builder.field("post", post); builder.field("post", post);
} }
if (boost != -1) { if (boost != NOT_SET) {
builder.field("boost", boost); builder.field("boost", boost);
} }
if (queryName != null) { if (queryName != null) {