This can be done without map lookups => less code and much smaller methods => better inlining potentially.
This commit is contained in:
parent
df3a7c0c8d
commit
25db5acb0d
|
@ -94,25 +94,6 @@ import static org.elasticsearch.ElasticsearchException.readStackTrace;
|
|||
*/
|
||||
public abstract class StreamInput extends InputStream {
|
||||
|
||||
private static final Map<Byte, TimeUnit> BYTE_TIME_UNIT_MAP;
|
||||
|
||||
static {
|
||||
final Map<Byte, TimeUnit> byteTimeUnitMap = new HashMap<>();
|
||||
byteTimeUnitMap.put((byte)0, TimeUnit.NANOSECONDS);
|
||||
byteTimeUnitMap.put((byte)1, TimeUnit.MICROSECONDS);
|
||||
byteTimeUnitMap.put((byte)2, TimeUnit.MILLISECONDS);
|
||||
byteTimeUnitMap.put((byte)3, TimeUnit.SECONDS);
|
||||
byteTimeUnitMap.put((byte)4, TimeUnit.MINUTES);
|
||||
byteTimeUnitMap.put((byte)5, TimeUnit.HOURS);
|
||||
byteTimeUnitMap.put((byte)6, TimeUnit.DAYS);
|
||||
|
||||
for (TimeUnit value : TimeUnit.values()) {
|
||||
assert byteTimeUnitMap.containsValue(value) : value;
|
||||
}
|
||||
|
||||
BYTE_TIME_UNIT_MAP = Collections.unmodifiableMap(byteTimeUnitMap);
|
||||
}
|
||||
|
||||
private Version version = Version.CURRENT;
|
||||
|
||||
/**
|
||||
|
@ -1313,12 +1294,22 @@ public abstract class StreamInput extends InputStream {
|
|||
*/
|
||||
protected abstract void ensureCanReadBytes(int length) throws EOFException;
|
||||
|
||||
private static final TimeUnit[] TIME_UNITS = TimeUnit.values();
|
||||
|
||||
static {
|
||||
// assert the exact form of the TimeUnit values to ensure we're not silently broken by a JDK change
|
||||
if (Arrays.equals(TIME_UNITS, new TimeUnit[]{TimeUnit.NANOSECONDS, TimeUnit.MICROSECONDS, TimeUnit.MILLISECONDS,
|
||||
TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS}) == false) {
|
||||
throw new AssertionError("Incompatible JDK version used that breaks assumptions on the structure of the TimeUnit enum");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a {@link TimeValue} from the stream
|
||||
*/
|
||||
public TimeValue readTimeValue() throws IOException {
|
||||
long duration = readZLong();
|
||||
TimeUnit timeUnit = BYTE_TIME_UNIT_MAP.get(readByte());
|
||||
TimeUnit timeUnit = TIME_UNITS[readByte()];
|
||||
return new TimeValue(duration, timeUnit);
|
||||
}
|
||||
|
||||
|
|
|
@ -66,7 +66,6 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.EnumMap;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
@ -76,7 +75,6 @@ import java.util.LinkedHashSet;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
/**
|
||||
|
@ -92,26 +90,8 @@ import java.util.function.IntFunction;
|
|||
*/
|
||||
public abstract class StreamOutput extends OutputStream {
|
||||
|
||||
private static final Map<TimeUnit, Byte> TIME_UNIT_BYTE_MAP;
|
||||
private static final int MAX_NESTED_EXCEPTION_LEVEL = 100;
|
||||
|
||||
static {
|
||||
final Map<TimeUnit, Byte> timeUnitByteMap = new EnumMap<>(TimeUnit.class);
|
||||
timeUnitByteMap.put(TimeUnit.NANOSECONDS, (byte)0);
|
||||
timeUnitByteMap.put(TimeUnit.MICROSECONDS, (byte)1);
|
||||
timeUnitByteMap.put(TimeUnit.MILLISECONDS, (byte)2);
|
||||
timeUnitByteMap.put(TimeUnit.SECONDS, (byte)3);
|
||||
timeUnitByteMap.put(TimeUnit.MINUTES, (byte)4);
|
||||
timeUnitByteMap.put(TimeUnit.HOURS, (byte)5);
|
||||
timeUnitByteMap.put(TimeUnit.DAYS, (byte)6);
|
||||
|
||||
for (TimeUnit value : TimeUnit.values()) {
|
||||
assert timeUnitByteMap.containsKey(value) : value;
|
||||
}
|
||||
|
||||
TIME_UNIT_BYTE_MAP = Collections.unmodifiableMap(timeUnitByteMap);
|
||||
}
|
||||
|
||||
private Version version = Version.CURRENT;
|
||||
private Set<String> features = Collections.emptySet();
|
||||
|
||||
|
@ -1263,7 +1243,7 @@ public abstract class StreamOutput extends OutputStream {
|
|||
*/
|
||||
public void writeTimeValue(TimeValue timeValue) throws IOException {
|
||||
writeZLong(timeValue.duration());
|
||||
writeByte(TIME_UNIT_BYTE_MAP.get(timeValue.timeUnit()));
|
||||
writeByte((byte) timeValue.timeUnit().ordinal());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue