-
Notifications
You must be signed in to change notification settings - Fork 3
Implement switch statements and expressions #9
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -398,6 +398,133 @@ public Builder endControlFlow(String controlFlow, Object... args) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Starts a switch statement. To start a switch expression it should be | ||
| * used with {@link #add(String, Object...) add}. | ||
| * @param expressionFormat the format of the expression that will be calculated | ||
| * by the switch statement. It should not include parentheses, braces or | ||
| * newline characters | ||
| * @param args the values to be placed instead of the format's placeholders | ||
| */ | ||
| public Builder beginSwitchStatement(String expressionFormat, Object... args) { | ||
| add("switch ("+expressionFormat+") {\n", args); | ||
| indent(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Starts a switch statement. To start a switch expression it should be | ||
| * used with {@link #add(String, Object...) add}. | ||
| * @param codeblock the expression that will be calculated | ||
| * by the switch statement. It should not include parentheses, braces or | ||
| * newline characters | ||
| */ | ||
| public Builder beginSwitchStatement(CodeBlock codeBlock) { | ||
| return beginSwitchStatement("$L", codeBlock); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Adds a case to a switch statement or expression. | ||
| * @param isFirstCase indicates whether the case is the first of the switch | ||
| * statement. Necessary to apply proper indentation | ||
| * @param valueFormat the value(-s) that the calculated expression will be compared | ||
| * against | ||
| * @param args the values to be placed instead of the format's placeholders | ||
| */ | ||
| public Builder addSwitchCase(Boolean isFirstCase, String valueFormat, Object... args) { | ||
| // If a previous case already exists, we must unindent | ||
| if (!isFirstCase) { | ||
| unindent(); | ||
| } | ||
| add("case " + valueFormat + ":\n", args); | ||
| indent(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a case to a switch statement or expression. | ||
| * @param isFirstCase indicates whether the case is the first of the switch | ||
| * statement. Necessary to apply proper indentation | ||
| * @param codeBlock the value(-s) that the calculated expression will be compared | ||
| * against | ||
| */ | ||
| public Builder addSwitchCase(Boolean isFirstCase, CodeBlock codeBlock) { | ||
| return addSwitchCase(isFirstCase,"$L", codeBlock); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a case to an extended switch statement or expression. | ||
| * @param body the code that needs to be executed if the case is true. | ||
| * @param valueFormat the value(-s) that the calculated expression will be compared | ||
| * against | ||
| * @param args the values to be placed instead of the format's placeholders | ||
| */ | ||
| public Builder addExtendedSwitchCase(CodeBlock body, | ||
| String valueFormat, Object... args) { | ||
| String bodySide = body.toString(); | ||
| // If the body contains multiple lines or expressions it should be | ||
| // contained in braces | ||
| if (bodySide.lines().count() > 1 || bodySide.split(";").length > 1) { | ||
| bodySide = "{ " + bodySide + " }"; | ||
|
Owner
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 is coming up in the lambda PR too. We need a general way for users to specify how to output lambda-like code bodies. We can't assume how the braces are positioned around the code block.
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. How do you suggest I tackle this issue? Would simply removing these lines of code and assuming that the user includes the braces in the code block when necessary resolve the problem?
Owner
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. As I just wrote in the lambda PR, I'd like to see a new builder class shared by both lambda and this PR that is similar to i.e. Some people will want: Some will want: Some will want: And every variation inbetween |
||
| } | ||
| add("case " + valueFormat + " -> " + bodySide + "\n", args); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a case to an extended switch statement or expression. | ||
| * @param body the code that needs to be executed if the case is true. | ||
| * @param value the value(-s) that the calculated expression will be compared | ||
| * against | ||
| */ | ||
| public Builder addExtendedSwitchCase(CodeBlock body, CodeBlock value) { | ||
| return addExtendedSwitchCase(body, "$L", value); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Adds the default case to a switch statement or expression | ||
| */ | ||
| public Builder addDefaultCase() { | ||
| unindent(); | ||
| add("default:\n"); | ||
| indent(); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the default case to an enhanced switch statement or expression | ||
| * @param body the code that needs to be executed if none of the cases | ||
| * are true. | ||
| */ | ||
| public Builder addExtendedDefaultCase (CodeBlock body) { | ||
| String bodySide = body.toString(); | ||
| // If the body contains multiple lines or expressions it should be | ||
| // contained in braces | ||
| if (bodySide.lines().count() > 1 || bodySide.split(";").length > 1) { | ||
| bodySide = "{" + bodySide + "}"; | ||
| } | ||
| add("default -> " + bodySide + "\n", args); | ||
| return this; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Closes the switch statement or expression | ||
| * @param isExtended whether the switch block to close is an extended switch | ||
| * block or not. Necessary to implement proper indentation | ||
| */ | ||
| public Builder endSwitchStatement(Boolean isExtended) { | ||
| // unindent needs to be called twice if the switch block isn't an extended | ||
| // switch block | ||
| if (!isExtended) { | ||
| unindent(); | ||
| } | ||
| return endControlFlow(); | ||
| } | ||
|
|
||
| public Builder addStatement(String format, Object... args) { | ||
| add("$["); | ||
| add(format, args); | ||
|
|
||
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.
We must not assume where newlines, indenting, tabs, etc. go. The library already has mechanisms for users to specify these.
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.
The already existing methods that implement control flows do the same thing, adding newlines and adjusting the indentation. I tried to be consistent with the codebase and create methods that follow the same logic. If that's a problem would you suggest I simply remove newline characters?