-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Use switch expressions introduced in Java 14 to simplify code #18371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -431,33 +431,20 @@ protected static Object convertTo(Schema toSchema, Schema fromSchema, Object val | |
| } | ||
| throw new DataException("Unable to convert a null value to a schema that requires a value"); | ||
| } | ||
| switch (toSchema.type()) { | ||
| case BYTES: | ||
| return convertMaybeLogicalBytes(toSchema, value); | ||
| case STRING: | ||
| return convertToString(fromSchema, value); | ||
| case BOOLEAN: | ||
| return convertToBoolean(fromSchema, value); | ||
| case INT8: | ||
| return convertToByte(fromSchema, value); | ||
| case INT16: | ||
| return convertToShort(fromSchema, value); | ||
| case INT32: | ||
| return convertMaybeLogicalInteger(toSchema, fromSchema, value); | ||
| case INT64: | ||
| return convertMaybeLogicalLong(toSchema, fromSchema, value); | ||
| case FLOAT32: | ||
| return convertToFloat(fromSchema, value); | ||
| case FLOAT64: | ||
| return convertToDouble(fromSchema, value); | ||
| case ARRAY: | ||
| return convertToArray(toSchema, value); | ||
| case MAP: | ||
| return convertToMapInternal(toSchema, value); | ||
| case STRUCT: | ||
| return convertToStructInternal(toSchema, value); | ||
| } | ||
| throw new DataException("Unable to convert " + value + " (" + value.getClass() + ") to " + toSchema); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my curiosity, will the exception here not change with the new syntax? If it changes, do upper layers handle it correctly?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case is never hit since the switch is exhaustive. |
||
| return switch (toSchema.type()) { | ||
| case BYTES -> convertMaybeLogicalBytes(toSchema, value); | ||
| case STRING -> convertToString(fromSchema, value); | ||
| case BOOLEAN -> convertToBoolean(fromSchema, value); | ||
| case INT8 -> convertToByte(fromSchema, value); | ||
| case INT16 -> convertToShort(fromSchema, value); | ||
| case INT32 -> convertMaybeLogicalInteger(toSchema, fromSchema, value); | ||
| case INT64 -> convertMaybeLogicalLong(toSchema, fromSchema, value); | ||
| case FLOAT32 -> convertToFloat(fromSchema, value); | ||
| case FLOAT64 -> convertToDouble(fromSchema, value); | ||
| case ARRAY -> convertToArray(toSchema, value); | ||
| case MAP -> convertToMapInternal(toSchema, value); | ||
| case STRUCT -> convertToStructInternal(toSchema, value); | ||
| }; | ||
| } | ||
|
|
||
| private static Serializable convertMaybeLogicalBytes(Schema toSchema, Object value) { | ||
|
|
@@ -1144,21 +1131,15 @@ private static Schema mergeSchemas(Schema previous, Schema newSchema) { | |
| Type previousType = previous.type(); | ||
| Type newType = newSchema.type(); | ||
| if (previousType != newType) { | ||
| switch (previous.type()) { | ||
| case INT8: | ||
| return commonSchemaForInt8(newSchema, newType); | ||
| case INT16: | ||
| return commonSchemaForInt16(previous, newSchema, newType); | ||
| case INT32: | ||
| return commonSchemaForInt32(previous, newSchema, newType); | ||
| case INT64: | ||
| return commonSchemaForInt64(previous, newSchema, newType); | ||
| case FLOAT32: | ||
| return commonSchemaForFloat32(previous, newSchema, newType); | ||
| case FLOAT64: | ||
| return commonSchemaForFloat64(previous, newType); | ||
| } | ||
| return null; | ||
| return switch (previous.type()) { | ||
| case INT8 -> commonSchemaForInt8(newSchema, newType); | ||
| case INT16 -> commonSchemaForInt16(previous, newSchema, newType); | ||
| case INT32 -> commonSchemaForInt32(previous, newSchema, newType); | ||
| case INT64 -> commonSchemaForInt64(previous, newSchema, newType); | ||
| case FLOAT32 -> commonSchemaForFloat32(previous, newSchema, newType); | ||
| case FLOAT64 -> commonSchemaForFloat64(previous, newType); | ||
| default -> null; | ||
| }; | ||
| } | ||
| if (previous.isOptional() == newSchema.isOptional()) { | ||
| // Use the optional one | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other places you have translated this to
default -> nullin the new syntax. Shouldn't this be the same or am I missing something obvious?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was directly translated by IntelliJ - I started (but haven't completed) the review of every case to make sure it makes sense. Generally speaking, if the switch is exhaustive, then it doesn't need to return
nullif we're ok with throwing an exception if a new enum element is added later and the code is not updated.