Skip to content

Use switch expressions introduced in Java 14 to simplify code#18371

Closed
ijuma wants to merge 1 commit intoapache:trunkfrom
ijuma:switch-expressions
Closed

Use switch expressions introduced in Java 14 to simplify code#18371
ijuma wants to merge 1 commit intoapache:trunkfrom
ijuma:switch-expressions

Conversation

@ijuma
Copy link
Copy Markdown
Member

@ijuma ijuma commented Jan 1, 2025

Need to fix some formatting issues before it's ready to review.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@github-actions github-actions Bot added streams core Kafka Broker tools connect kraft storage Pull requests that target the storage module tiered-storage Related to the Tiered Storage feature KIP-932 Queues for Kafka transactions Transactions and EOS labels Jan 1, 2025
Copy link
Copy Markdown
Contributor

@clolov clolov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is marked as draft, but I had a short look and left a few minor comments

case STRUCT:
return convertToStructInternal(toSchema, value);
}
throw new DataException("Unable to convert " + value + " (" + value.getClass() + ") to " + toSchema);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is never hit since the switch is exhaustive.

case MAP:
return projectMap(source, record, target);
}
return null;
Copy link
Copy Markdown
Contributor

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 -> null in the new syntax. Shouldn't this be the same or am I missing something obvious?

Copy link
Copy Markdown
Member Author

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 null if we're ok with throwing an exception if a new enum element is added later and the code is not updated.

case DYNAMIC_METADATA_VOTER_DIRECTORY:
return "dynamic metadata voter directory";
}
throw new RuntimeException("invalid enum type " + this);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unreachable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - I'll confirm, but IntelliJ does this if its static analyzer decides the code is unreachable due to an exhaustive switch.

case KRAFT_VERSION_1:
return (short) 1;
}
throw new IllegalStateException("Unsupported KRaft feature level: " + this);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unreachable?

case LEADER_EPOCH:
return FETCH_LEADER_EPOCH_CHECKPOINT;
}
return FETCH_SEGMENT;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unreachable?

case LEADER_EPOCH:
return LEADER_EPOCH_CHECKPOINT;
}
return SEGMENT;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this unreachable?

@mjsax
Copy link
Copy Markdown
Member

mjsax commented Apr 5, 2025

Seems we need to revert all KS related changed, as KS is still on Java 11.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 4, 2025

This PR is being marked as stale since it has not had any activity in 90 days. If you
would like to keep this PR alive, please leave a comment asking for a review. If the PR has
merge conflicts, update it with the latest from the base branch.

If you are having difficulty finding a reviewer, please reach out on the [mailing list](https://kafka.apache.org/contact).

If this PR is no longer valid or desired, please feel free to close it. If no activity occurs in the next 30 days, it will be automatically closed.

@github-actions github-actions Bot added the stale Stale PRs label Jul 4, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Aug 3, 2025

This PR has been closed since it has not had any activity in 120 days. If you feel like this
was a mistake, or you would like to continue working on it, please feel free to re-open the
PR and ask for a review.

@github-actions github-actions Bot added the closed-stale PRs that were closed due to inactivity label Aug 3, 2025
@github-actions github-actions Bot closed this Aug 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

closed-stale PRs that were closed due to inactivity connect core Kafka Broker kraft stale Stale PRs storage Pull requests that target the storage module streams tiered-storage Related to the Tiered Storage feature tools transactions Transactions and EOS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants