clean builder code when dealing with maps

This commit is contained in:
kimchy 2010-08-14 00:37:33 +03:00
parent 4a9c7d672e
commit 75ee01a6b4
9 changed files with 28 additions and 21 deletions

View File

@ -82,11 +82,6 @@ public abstract class XContentBuilder<T extends XContentBuilder> {
return builder;
}
public T map(Map<String, Object> map) throws IOException {
XContentMapConverter.writeMap(generator, map);
return builder;
}
public T startObject(String name) throws IOException {
field(name);
startObject();
@ -208,6 +203,12 @@ public abstract class XContentBuilder<T extends XContentBuilder> {
return builder;
}
public T field(String name, Map<String, Object> value) throws IOException {
field(name);
value(value);
return builder;
}
public T field(String name, Object value) throws IOException {
if (value == null) {
nullField(name);
@ -232,6 +233,8 @@ public abstract class XContentBuilder<T extends XContentBuilder> {
field(name, (byte[]) value);
} else if (value instanceof ReadableInstant) {
field(name, (ReadableInstant) value);
} else if (value instanceof Map) {
field(name, (Map<String, Object>) value);
} else {
field(name, value.toString());
}
@ -358,6 +361,16 @@ public abstract class XContentBuilder<T extends XContentBuilder> {
return builder;
}
public T map(Map<String, Object> map) throws IOException {
XContentMapConverter.writeMap(generator, map);
return builder;
}
public T value(Map<String, Object> map) throws IOException {
XContentMapConverter.writeMap(generator, map);
return builder;
}
public T value(Object value) throws IOException {
Class type = value.getClass();
if (type == String.class) {
@ -378,6 +391,8 @@ public abstract class XContentBuilder<T extends XContentBuilder> {
value((Date) value);
} else if (value instanceof ReadableInstant) {
value((ReadableInstant) value);
} else if (value instanceof Map) {
value((Map<String, Object>) value);
} else {
throw new IOException("Type not allowed [" + type + "]");
}

View File

@ -95,8 +95,7 @@ public class CustomScoreQueryBuilder extends BaseQueryBuilder {
queryBuilder.toXContent(builder, params);
builder.field("script", script);
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
if (boost != -1) {
builder.field("boost", boost);

View File

@ -60,8 +60,7 @@ public class ScriptFilterBuilder extends BaseFilterBuilder {
builder.startObject(ScriptFilterParser.NAME);
builder.field("script", script);
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
builder.endObject();
}

View File

@ -221,8 +221,7 @@ public class GeoDistanceFacetBuilder extends AbstractFacetBuilder {
if (valueScript != null) {
builder.field("value_script", valueScript);
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
}

View File

@ -96,8 +96,7 @@ public class HistogramScriptFacetBuilder extends AbstractFacetBuilder {
builder.field("interval", interval);
}
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
if (comparatorType != null) {
builder.field("comparator", comparatorType.description());

View File

@ -136,8 +136,7 @@ public class RangeScriptFacetBuilder extends AbstractFacetBuilder {
builder.endArray();
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
builder.endObject();

View File

@ -71,8 +71,7 @@ public class StatisticalScriptFacetBuilder extends AbstractFacetBuilder {
builder.startObject(StatisticalFacetCollectorParser.NAME);
builder.field("script", script);
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
builder.endObject();

View File

@ -128,8 +128,7 @@ public class TermsFacetBuilder extends AbstractFacetBuilder {
if (script != null) {
builder.field("script", script);
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
}

View File

@ -64,8 +64,7 @@ public class ScriptSortBuilder extends SortBuilder {
builder.field("reverse", true);
}
if (this.params != null) {
builder.field("params");
builder.map(this.params);
builder.field("params", this.params);
}
builder.endObject();
}