Remove PROTOTYPEs from Ranges

Relates to #17085
This commit is contained in:
Nik Everett 2016-04-18 14:13:29 -04:00
parent 133cec602c
commit c982216dbe
6 changed files with 42 additions and 54 deletions

View File

@ -58,8 +58,6 @@ public class RangeAggregator extends BucketsAggregator {
public static final ParseField KEYED_FIELD = new ParseField("keyed");
public static class Range implements Writeable<Range>, ToXContent {
public static final Range PROTOTYPE = new Range(null, null, null, null, null);
public static final ParseField KEY_FIELD = new ParseField("key");
public static final ParseField FROM_FIELD = new ParseField("from");
public static final ParseField TO_FIELD = new ParseField("to");
@ -78,6 +76,27 @@ public class RangeAggregator extends BucketsAggregator {
this(key, null, from, null, to);
}
/**
* Read from a stream.
*/
public Range(StreamInput in) throws IOException {
key = in.readOptionalString();
fromAsStr = in.readOptionalString();
toAsStr = in.readOptionalString();
from = in.readDouble();
to = in.readDouble();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(key);
out.writeOptionalString(fromAsStr);
out.writeOptionalString(toAsStr);
out.writeDouble(from);
out.writeDouble(to);
}
protected Range(String key, Double from, String fromAsStr, Double to, String toAsStr) {
this.key = key;
this.from = from == null ? Double.NEGATIVE_INFINITY : from;
@ -108,27 +127,7 @@ public class RangeAggregator extends BucketsAggregator {
return new Range(key, from, fromAsStr, to, toAsStr);
}
@Override
public Range readFrom(StreamInput in) throws IOException {
String key = in.readOptionalString();
String fromAsStr = in.readOptionalString();
String toAsStr = in.readOptionalString();
double from = in.readDouble();
double to = in.readDouble();
return new Range(key, from, fromAsStr, to, toAsStr);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(key);
out.writeOptionalString(fromAsStr);
out.writeOptionalString(toAsStr);
out.writeDouble(from);
out.writeDouble(to);
}
public Range fromXContent(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {
public static Range fromXContent(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {
XContentParser.Token token;
String currentFieldName = null;
double from = Double.NEGATIVE_INFINITY;

View File

@ -42,7 +42,7 @@ public class RangeAggregatorBuilder extends AbstractRangeBuilder<RangeAggregator
* Read from a stream.
*/
public RangeAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalRange.FACTORY, Range.PROTOTYPE::readFrom);
super(in, InternalRange.FACTORY, Range::new);
}
/**

View File

@ -88,6 +88,6 @@ public class RangeParser extends NumericValuesSourceParser {
}
protected Range parseRange(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {
return Range.PROTOTYPE.fromXContent(parser, parseFieldMatcher);
return Range.fromXContent(parser, parseFieldMatcher);
}
}

View File

@ -45,7 +45,7 @@ public class DateRangeAggregatorBuilder extends AbstractRangeBuilder<DateRangeAg
* Read from a stream.
*/
public DateRangeAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalDateRange.FACTORY, Range.PROTOTYPE::readFrom);
super(in, InternalDateRange.FACTORY, Range::new);
}
@Override

View File

@ -51,7 +51,7 @@ public class IPv4RangeAggregatorBuilder extends AbstractRangeBuilder<IPv4RangeAg
* Read from a stream.
*/
public IPv4RangeAggregatorBuilder(StreamInput in) throws IOException {
super(in, InternalIPv4Range.FACTORY, Range.PROTOTYPE::readFrom);
super(in, InternalIPv4Range.FACTORY, Range::new);
}
@Override
@ -140,8 +140,6 @@ public class IPv4RangeAggregatorBuilder extends AbstractRangeBuilder<IPv4RangeAg
}
public static class Range extends RangeAggregator.Range {
static final Range PROTOTYPE = new Range(null, null, null, null, null, null);
static final ParseField MASK_FIELD = new ParseField("mask");
private final String cidr;
@ -163,6 +161,20 @@ public class IPv4RangeAggregatorBuilder extends AbstractRangeBuilder<IPv4RangeAg
this.cidr = cidr;
}
/**
* Read from a stream.
*/
public Range(StreamInput in) throws IOException {
super(in);
cidr = in.readOptionalString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalString(cidr);
}
public String mask() {
return cidr;
}
@ -190,9 +202,7 @@ public class IPv4RangeAggregatorBuilder extends AbstractRangeBuilder<IPv4RangeAg
return new Range(key, from, to);
}
@Override
public Range fromXContent(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {
public static Range fromXContent(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {
XContentParser.Token token;
String currentFieldName = null;
double from = Double.NEGATIVE_INFINITY;
@ -251,27 +261,6 @@ public class IPv4RangeAggregatorBuilder extends AbstractRangeBuilder<IPv4RangeAg
return builder;
}
@Override
public Range readFrom(StreamInput in) throws IOException {
String key = in.readOptionalString();
String fromAsStr = in.readOptionalString();
String toAsStr = in.readOptionalString();
double from = in.readDouble();
double to = in.readDouble();
String mask = in.readOptionalString();
return new Range(key, from, fromAsStr, to, toAsStr, mask);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(key);
out.writeOptionalString(fromAsStr);
out.writeOptionalString(toAsStr);
out.writeDouble(from);
out.writeDouble(to);
out.writeOptionalString(cidr);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), cidr);

View File

@ -42,7 +42,7 @@ public class IpRangeParser extends RangeParser {
@Override
protected Range parseRange(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {
return IPv4RangeAggregatorBuilder.Range.PROTOTYPE.fromXContent(parser, parseFieldMatcher);
return IPv4RangeAggregatorBuilder.Range.fromXContent(parser, parseFieldMatcher);
}
@Override