Switch to StreamInput.readEnum and StreamOutput.writeEnum (elastic/x-pack-elasticsearch#1313)

Start using StreamInput.readEnum and StreamOutput.writeEnum for enum serialization.

Relates to elastic/elasticsearch#24475

Original commit: elastic/x-pack-elasticsearch@67ca571458
This commit is contained in:
Igor Motov 2017-05-04 18:03:05 -04:00 committed by GitHub
parent 9e3d5d6cf3
commit 9a800cac60
11 changed files with 22 additions and 66 deletions

View File

@ -142,16 +142,12 @@ public class ChunkingConfig extends ToXContentToBytes implements Writeable {
} }
public static Mode readFromStream(StreamInput in) throws IOException { public static Mode readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(Mode.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown Mode ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -37,11 +37,7 @@ public enum DatafeedState implements Task.Status {
} }
public static DatafeedState fromStream(StreamInput in) throws IOException { public static DatafeedState fromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(DatafeedState.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown public enum DatafeedState ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
@ -51,7 +47,7 @@ public enum DatafeedState implements Task.Status {
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -27,16 +27,12 @@ public enum Connective implements Writeable {
} }
public static Connective readFromStream(StreamInput in) throws IOException { public static Connective readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(Connective.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown Connective ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -63,16 +63,12 @@ public class DataDescription extends ToXContentToBytes implements Writeable {
} }
public static DataFormat readFromStream(StreamInput in) throws IOException { public static DataFormat readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(DataFormat.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown DataFormat ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -54,16 +54,12 @@ public class Detector extends ToXContentToBytes implements Writeable {
} }
public static ExcludeFrequent readFromStream(StreamInput in) throws IOException { public static ExcludeFrequent readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(ExcludeFrequent.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown ExcludeFrequent ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -29,16 +29,12 @@ public enum JobState implements ToXContent, Writeable {
} }
public static JobState fromStream(StreamInput in) throws IOException { public static JobState fromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(JobState.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown public enum JobState {\n ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -84,16 +84,12 @@ public enum Operator implements Writeable {
} }
public static Operator readFromStream(StreamInput in) throws IOException { public static Operator readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(Operator.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown Operator ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -26,16 +26,12 @@ public enum RuleAction implements Writeable {
} }
public static RuleAction readFromStream(StreamInput in) throws IOException { public static RuleAction readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(RuleAction.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown RuleAction ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -31,16 +31,12 @@ public enum RuleConditionType implements Writeable {
} }
public static RuleConditionType readFromStream(StreamInput in) throws IOException { public static RuleConditionType readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(RuleConditionType.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown RuleConditionType ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -96,16 +96,12 @@ public class ModelSizeStats extends ToXContentToBytes implements Writeable {
} }
public static MemoryStatus readFromStream(StreamInput in) throws IOException { public static MemoryStatus readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(MemoryStatus.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown MemoryStatus ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override

View File

@ -27,16 +27,12 @@ public enum Level implements Writeable {
} }
public static Level readFromStream(StreamInput in) throws IOException { public static Level readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt(); return in.readEnum(Level.class);
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown Level ordinal [" + ordinal + "]");
}
return values()[ordinal];
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(ordinal()); out.writeEnum(this);
} }
@Override @Override