Make internal Rounding fields final (#28532)

The fields in the internal rounding classes can be made final with very minor
adjustments to how they are read from a StreamInput.
This commit is contained in:
Christoph Büscher 2018-02-07 09:33:21 +01:00 committed by GitHub
parent c2fcf15d9d
commit 305b87b4b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 31 deletions

View File

@ -21,7 +21,7 @@ package org.elasticsearch.common.rounding;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.unit.TimeValue;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeZone;
@ -33,7 +33,7 @@ import java.util.Objects;
/**
* A strategy for rounding long values.
*/
public abstract class Rounding implements Streamable {
public abstract class Rounding implements Writeable {
public abstract byte id();
@ -107,13 +107,10 @@ public abstract class Rounding implements Streamable {
static final byte ID = 1;
private DateTimeUnit unit;
private DateTimeField field;
private DateTimeZone timeZone;
private boolean unitRoundsToMidnight;
TimeUnitRounding() { // for serialization
}
private final DateTimeUnit unit;
private final DateTimeField field;
private final DateTimeZone timeZone;
private final boolean unitRoundsToMidnight;
TimeUnitRounding(DateTimeUnit unit, DateTimeZone timeZone) {
this.unit = unit;
@ -122,6 +119,13 @@ public abstract class Rounding implements Streamable {
this.timeZone = timeZone;
}
TimeUnitRounding(StreamInput in) throws IOException {
unit = DateTimeUnit.resolve(in.readByte());
timeZone = DateTimeZone.forID(in.readString());
field = unit.field(timeZone);
unitRoundsToMidnight = field.getDurationField().getUnitMillis() > 60L * 60L * 1000L;
}
@Override
public byte id() {
return ID;
@ -237,14 +241,6 @@ public abstract class Rounding implements Streamable {
return next;
}
@Override
public void readFrom(StreamInput in) throws IOException {
unit = DateTimeUnit.resolve(in.readByte());
timeZone = DateTimeZone.forID(in.readString());
field = unit.field(timeZone);
unitRoundsToMidnight = field.getDurationField().getUnitMillis() > 60L * 60L * 1000L;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeByte(unit.id());
@ -278,11 +274,8 @@ public abstract class Rounding implements Streamable {
static final byte ID = 2;
private long interval;
private DateTimeZone timeZone;
TimeIntervalRounding() { // for serialization
}
private final long interval;
private final DateTimeZone timeZone;
TimeIntervalRounding(long interval, DateTimeZone timeZone) {
if (interval < 1)
@ -291,6 +284,11 @@ public abstract class Rounding implements Streamable {
this.timeZone = timeZone;
}
TimeIntervalRounding(StreamInput in) throws IOException {
interval = in.readVLong();
timeZone = DateTimeZone.forID(in.readString());
}
@Override
public byte id() {
return ID;
@ -374,12 +372,6 @@ public abstract class Rounding implements Streamable {
return timeZone.convertLocalToUTC(next, false);
}
@Override
public void readFrom(StreamInput in) throws IOException {
interval = in.readVLong();
timeZone = DateTimeZone.forID(in.readString());
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(interval);
@ -415,11 +407,10 @@ public abstract class Rounding implements Streamable {
Rounding rounding = null;
byte id = in.readByte();
switch (id) {
case TimeUnitRounding.ID: rounding = new TimeUnitRounding(); break;
case TimeIntervalRounding.ID: rounding = new TimeIntervalRounding(); break;
case TimeUnitRounding.ID: rounding = new TimeUnitRounding(in); break;
case TimeIntervalRounding.ID: rounding = new TimeIntervalRounding(in); break;
default: throw new ElasticsearchException("unknown rounding id [" + id + "]");
}
rounding.readFrom(in);
return rounding;
}