Skip to content

Enhance switch statements #2705

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

Closed
junghoon-vans opened this issue Sep 11, 2023 · 0 comments
Closed

Enhance switch statements #2705

junghoon-vans opened this issue Sep 11, 2023 · 0 comments
Assignees
Labels
type: task A general task

Comments

@junghoon-vans
Copy link
Contributor

junghoon-vans commented Sep 11, 2023

Traditional switch statements are not very readable. But with Java 17's support for enhanced switch expressions.
it's possible to make them concise and readable. An example is shown below.

AS-IS

private RedisOperationChain from(Part part, Iterator<Object> iterator, RedisOperationChain sink) {

  switch (part.getType()) {
    case SIMPLE_PROPERTY:
      sink.sismember(part.getProperty().toDotPath(), iterator.next());
      break;
    case TRUE:
      sink.sismember(part.getProperty().toDotPath(), true);
      break;
    case FALSE:
      sink.sismember(part.getProperty().toDotPath(), false);
      break;
    case WITHIN:
    case NEAR:
      sink.near(getNearPath(part, iterator));
      break;
    default:
      throw new IllegalArgumentException(String.format("%s is not supported for Redis query derivation", part.getType()));
  }
  
  return sink;
}

TO-BE

private RedisOperationChain from(Part part, Iterator<Object> iterator, RedisOperationChain sink) {

  switch (part.getType()) {
    case SIMPLE_PROPERTY -> sink.sismember(part.getProperty().toDotPath(), iterator.next());
    case TRUE -> sink.sismember(part.getProperty().toDotPath(), true);
    case FALSE -> sink.sismember(part.getProperty().toDotPath(), false);
    case WITHIN, NEAR -> sink.near(getNearPath(part, iterator));
    default -> throw new IllegalArgumentException(
      String.format("%s is not supported for Redis query derivation", part.getType()));
  }
  
  return sink;
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 11, 2023
@junghoon-vans junghoon-vans changed the title Enhance switch statements Enhance switch expressions Sep 11, 2023
@junghoon-vans junghoon-vans changed the title Enhance switch expressions Enhance switch statements Sep 11, 2023
@mp911de mp911de added type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 11, 2023
@mp911de mp911de self-assigned this Sep 11, 2023
jxblum added a commit to jxblum/spring-data-redis that referenced this issue Sep 11, 2023
jxblum added a commit to jxblum/spring-data-redis that referenced this issue Sep 11, 2023
jxblum added a commit to jxblum/spring-data-redis that referenced this issue Sep 11, 2023
@jxblum jxblum closed this as completed in 5e23156 Sep 11, 2023
jxblum pushed a commit to jxblum/spring-data-redis that referenced this issue Sep 11, 2023
Remove unused default branches.

Closes spring-projects#2705
Original pull request: spring-projects#2706
jxblum added a commit to jxblum/spring-data-redis that referenced this issue Sep 11, 2023
jxblum pushed a commit to jxblum/spring-data-redis that referenced this issue Sep 12, 2023
Remove unused default branches.

Closes spring-projects#2705
Original pull request: spring-projects#2706
jxblum added a commit to jxblum/spring-data-redis that referenced this issue Sep 12, 2023
@jxblum jxblum self-assigned this Sep 12, 2023
@jxblum jxblum added this to the 3.0.10 (2022.0.10) milestone Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants