/
ConstantProcessor.java
112 lines (96 loc) · 3.09 KB
/
ConstantProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.ql.expression.gen.processor;
import org.elasticsearch.common.io.stream.NamedWriteable;
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 Object constant;
private final Type type;
enum Type {
NAMED_WRITABLE,
ZONEDDATETIME,
GENERIC
}
public ConstantProcessor(Object value) {
this.constant = value;
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 {
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.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;
}
}
@Override
public String getWriteableName() {
return NAME;
}
@Override
public Object process(Object input) {
return constant;
}
@Override
public int hashCode() {
return Objects.hashCode(constant);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ConstantProcessor other = (ConstantProcessor) obj;
return Objects.equals(constant, other.constant);
}
@Override
public String toString() {
return "^" + constant;
}
}