|
@@ -10,36 +10,69 @@ import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
public class ConstantProcessor implements Processor {
|
|
|
|
|
|
public static String NAME = "c";
|
|
|
|
|
|
- private final Object constant;
|
|
|
- private final boolean namedWriteable;
|
|
|
+ private Object constant;
|
|
|
+ private final Type type;
|
|
|
+
|
|
|
+ enum Type {
|
|
|
+ NAMED_WRITABLE,
|
|
|
+ ZONEDDATETIME,
|
|
|
+ GENERIC
|
|
|
+ }
|
|
|
|
|
|
public ConstantProcessor(Object value) {
|
|
|
this.constant = value;
|
|
|
- this.namedWriteable = value instanceof NamedWriteable;
|
|
|
+ if (value instanceof NamedWriteable) {
|
|
|
+ type = Type.NAMED_WRITABLE;
|
|
|
+ } else if (value instanceof ZonedDateTime) {
|
|
|
+ type = Type.ZONEDDATETIME;
|
|
|
+ } else {
|
|
|
+ type = Type.GENERIC;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public ConstantProcessor(StreamInput in) throws IOException {
|
|
|
- namedWriteable = in.readBoolean();
|
|
|
- if (namedWriteable) {
|
|
|
- constant = in.readNamedWriteable(ConstantNamedWriteable.class);
|
|
|
- } else {
|
|
|
- constant = in.readGenericValue();
|
|
|
+ type = in.readEnum(Type.class);
|
|
|
+ switch (type) {
|
|
|
+ case NAMED_WRITABLE:
|
|
|
+ constant = in.readNamedWriteable(ConstantNamedWriteable.class);
|
|
|
+ break;
|
|
|
+ case ZONEDDATETIME:
|
|
|
+ ZonedDateTime zdt;
|
|
|
+ ZoneId zoneId = in.readZoneId();
|
|
|
+ zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(in.readLong()), zoneId);
|
|
|
+ constant = zdt.withNano(in.readInt());
|
|
|
+ break;
|
|
|
+ case GENERIC:
|
|
|
+ constant = in.readGenericValue();
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
|
|
- out.writeBoolean(namedWriteable);
|
|
|
- if (namedWriteable) {
|
|
|
- out.writeNamedWriteable((NamedWriteable) constant);
|
|
|
- } else {
|
|
|
- out.writeGenericValue(constant);
|
|
|
+ out.writeEnum(type);
|
|
|
+ switch (type) {
|
|
|
+ case NAMED_WRITABLE:
|
|
|
+ out.writeNamedWriteable((NamedWriteable) constant);
|
|
|
+ break;
|
|
|
+ case ZONEDDATETIME:
|
|
|
+ ZonedDateTime zdt = (ZonedDateTime) constant;
|
|
|
+ out.writeZoneId(zdt.getZone());
|
|
|
+ out.writeLong(zdt.toInstant().toEpochMilli());
|
|
|
+ out.writeInt(zdt.getNano());
|
|
|
+ break;
|
|
|
+ case GENERIC:
|
|
|
+ out.writeGenericValue(constant);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -76,4 +109,4 @@ public class ConstantProcessor implements Processor {
|
|
|
public String toString() {
|
|
|
return "^" + constant;
|
|
|
}
|
|
|
-}
|
|
|
+}
|