Skip to content

Commit d557d5c

Browse files
authored
Remove redundant switch on enum (elastic#133821)
It's a bit of a code smell to have a switch on an enum like this, so I've replaced it with a required enum parameter. This makes it clear to anyone adding to the enum that this is a required field. While I was at it, I also removed the other switch, since it was redundant with the name field anyway.
1 parent b1081c0 commit d557d5c

File tree

1 file changed

+35
-67
lines changed
  • test/framework/src/main/java/org/elasticsearch/datageneration

1 file changed

+35
-67
lines changed

test/framework/src/main/java/org/elasticsearch/datageneration/FieldType.java

Lines changed: 35 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,87 +30,55 @@
3030
import org.elasticsearch.datageneration.fields.leaf.UnsignedLongFieldDataGenerator;
3131
import org.elasticsearch.datageneration.fields.leaf.WildcardFieldDataGenerator;
3232

33+
import java.util.function.BiFunction;
34+
3335
/**
3436
* Lists all leaf field types that are supported for data generation by default.
3537
*/
3638
public enum FieldType {
37-
KEYWORD("keyword"),
38-
LONG("long"),
39-
UNSIGNED_LONG("unsigned_long"),
40-
INTEGER("integer"),
41-
SHORT("short"),
42-
BYTE("byte"),
43-
DOUBLE("double"),
44-
FLOAT("float"),
45-
HALF_FLOAT("half_float"),
46-
SCALED_FLOAT("scaled_float"),
47-
COUNTED_KEYWORD("counted_keyword"),
48-
BOOLEAN("boolean"),
49-
DATE("date"),
50-
GEO_POINT("geo_point"),
51-
TEXT("text"),
52-
IP("ip"),
53-
CONSTANT_KEYWORD("constant_keyword"),
54-
PASSTHROUGH("passthrough"), // For now this field type does not have default generators.
55-
WILDCARD("wildcard"),
56-
MATCH_ONLY_TEXT("match_only_text");
39+
KEYWORD("keyword", (fn, ds) -> new KeywordFieldDataGenerator(ds)),
40+
LONG("long", LongFieldDataGenerator::new),
41+
UNSIGNED_LONG("unsigned_long", UnsignedLongFieldDataGenerator::new),
42+
INTEGER("integer", IntegerFieldDataGenerator::new),
43+
SHORT("short", ShortFieldDataGenerator::new),
44+
BYTE("byte", ByteFieldDataGenerator::new),
45+
DOUBLE("double", DoubleFieldDataGenerator::new),
46+
FLOAT("float", FloatFieldDataGenerator::new),
47+
HALF_FLOAT("half_float", HalfFloatFieldDataGenerator::new),
48+
SCALED_FLOAT("scaled_float", ScaledFloatFieldDataGenerator::new),
49+
COUNTED_KEYWORD("counted_keyword", CountedKeywordFieldDataGenerator::new),
50+
BOOLEAN("boolean", (fn, ds) -> new BooleanFieldDataGenerator(ds)),
51+
DATE("date", (fn, ds) -> new DateFieldDataGenerator(ds)),
52+
GEO_POINT("geo_point", (fn, ds) -> new GeoPointFieldDataGenerator(ds)),
53+
TEXT("text", (fn, ds) -> new TextFieldDataGenerator(ds)),
54+
IP("ip", (fn, ds) -> new IpFieldDataGenerator(ds)),
55+
CONSTANT_KEYWORD("constant_keyword", (fn, ds) -> new ConstantKeywordFieldDataGenerator()),
56+
PASSTHROUGH("passthrough", (fn, ds) -> {
57+
// For now this field type does not have default generators.
58+
throw new IllegalArgumentException("Passthrough field type does not have a default generator");
59+
}),
60+
WILDCARD("wildcard", (fn, ds) -> new WildcardFieldDataGenerator(ds)),
61+
MATCH_ONLY_TEXT("match_only_text", (fn, ds) -> new MatchOnlyTextFieldDataGenerator(ds)),;
5762

5863
private final String name;
64+
private final BiFunction<String, DataSource, FieldDataGenerator> fieldDataGenerator;
5965

60-
FieldType(String name) {
66+
FieldType(String name, BiFunction<String, DataSource, FieldDataGenerator> fieldDataGenerator) {
6167
this.name = name;
68+
this.fieldDataGenerator = fieldDataGenerator;
6269
}
6370

6471
public FieldDataGenerator generator(String fieldName, DataSource dataSource) {
65-
return switch (this) {
66-
case KEYWORD -> new KeywordFieldDataGenerator(dataSource);
67-
case LONG -> new LongFieldDataGenerator(fieldName, dataSource);
68-
case UNSIGNED_LONG -> new UnsignedLongFieldDataGenerator(fieldName, dataSource);
69-
case INTEGER -> new IntegerFieldDataGenerator(fieldName, dataSource);
70-
case SHORT -> new ShortFieldDataGenerator(fieldName, dataSource);
71-
case BYTE -> new ByteFieldDataGenerator(fieldName, dataSource);
72-
case DOUBLE -> new DoubleFieldDataGenerator(fieldName, dataSource);
73-
case FLOAT -> new FloatFieldDataGenerator(fieldName, dataSource);
74-
case HALF_FLOAT -> new HalfFloatFieldDataGenerator(fieldName, dataSource);
75-
case SCALED_FLOAT -> new ScaledFloatFieldDataGenerator(fieldName, dataSource);
76-
case COUNTED_KEYWORD -> new CountedKeywordFieldDataGenerator(fieldName, dataSource);
77-
case BOOLEAN -> new BooleanFieldDataGenerator(dataSource);
78-
case DATE -> new DateFieldDataGenerator(dataSource);
79-
case GEO_POINT -> new GeoPointFieldDataGenerator(dataSource);
80-
case TEXT -> new TextFieldDataGenerator(dataSource);
81-
case IP -> new IpFieldDataGenerator(dataSource);
82-
case CONSTANT_KEYWORD -> new ConstantKeywordFieldDataGenerator();
83-
case WILDCARD -> new WildcardFieldDataGenerator(dataSource);
84-
case MATCH_ONLY_TEXT -> new MatchOnlyTextFieldDataGenerator(dataSource);
85-
case PASSTHROUGH -> throw new IllegalArgumentException("Passthrough field type does not have a default generator");
86-
};
72+
return fieldDataGenerator.apply(fieldName, dataSource);
8773
}
8874

8975
public static FieldType tryParse(String name) {
90-
return switch (name) {
91-
case "keyword" -> FieldType.KEYWORD;
92-
case "long" -> FieldType.LONG;
93-
case "unsigned_long" -> FieldType.UNSIGNED_LONG;
94-
case "integer" -> FieldType.INTEGER;
95-
case "short" -> FieldType.SHORT;
96-
case "byte" -> FieldType.BYTE;
97-
case "double" -> FieldType.DOUBLE;
98-
case "float" -> FieldType.FLOAT;
99-
case "half_float" -> FieldType.HALF_FLOAT;
100-
case "scaled_float" -> FieldType.SCALED_FLOAT;
101-
case "counted_keyword" -> FieldType.COUNTED_KEYWORD;
102-
case "boolean" -> FieldType.BOOLEAN;
103-
case "date" -> FieldType.DATE;
104-
case "geo_point" -> FieldType.GEO_POINT;
105-
case "text" -> FieldType.TEXT;
106-
case "ip" -> FieldType.IP;
107-
case "constant_keyword" -> FieldType.CONSTANT_KEYWORD;
108-
case "wildcard" -> FieldType.WILDCARD;
109-
case "passthrough" -> FieldType.PASSTHROUGH;
110-
case "match_only_text" -> FieldType.MATCH_ONLY_TEXT;
111-
// Custom types will fail to parse and will return null
112-
default -> null;
113-
};
76+
for (FieldType fieldType : FieldType.values()) {
77+
if (fieldType.name.equals(name)) {
78+
return fieldType;
79+
}
80+
}
81+
return null;
11482
}
11583

11684
@Override

0 commit comments

Comments
 (0)