Keep on same line

Lets you print certain statements on just one line when possible, i.e. if they don’t exceed the maximal line length.

Single if

When enabled, prints single if statements in one line when possible.

Since 1.2

Example 2.9. Standard single if block

if (cond)                                |
    return getMagicNumber();             |
                                         |
...                                      |

Example 2.10. Compact single if block

if (cond) return getMagicNumber();       |
                                         |
...                                      |

if

When enabled, prints the if part of if/else statements in one line when possible.

Since 1.2

Example 2.11. Standard if/else block

if (cond)                                |
    return getMagicNumber();             |
else                                     |
    return -1                            |

Example 2.12. Compact if of if/else block

if (cond) return getMagicNumber();       |
else                                     |
    return -1;                           |

Please note that when the if part has been printed on one line, the following else if or else part always starts on a new line.

else if

When enabled, prints the else if part of if/else statements in one line when possible.

Example 2.13. Standard else of if/else block

if (cond1)                                                 |
    return getMagicNumber();                               |
else if (cond2)                                            |
    throw new IllegalStateException();                     |

Example 2.14. Compact else if block

if (cond1)                                                 |
    return getMagicNumber();                               |
else if (cond2) throw new IllegalStateException();         |

Please note that when the else if part has been printed on one line, the following else part always starts on a new line.

Since 1.2

Example 2.15. Compact if block

if (cond1)                                                 |
    return getMagicNumber();                               |
else if (cond2) throw new IllegalStateException();         |
else                                                       |
    return 0;                                              |

else

When enabled, prints the else part of if/else statements on one line when possible.

Since 1.2

Example 2.16. Standard else block

if (cond1)                                                 |
    return getMagicNumber();                               |
else if (cond2)                                            |
    throw new IllegalStateException();                     |
else                                                       |
    return 0                                               |

Example 2.17. Compact else block

if (cond1)                                                 |
    return getMagicNumber();                               |
else if (cond2)                                            |
    throw new IllegalStateException();                     |
else return 0;                                             |

Enable for

You can narrow the scope for the above mentioned options by selecting whether all statements should be printed on one line when possible or only throw or return statements.

Since 1.2

Example 2.18. All statements on same line

if (true) return result; 
else if (false) System.out.println("unexpected condition"); 
else throw new Error();

Example 2.19. Only throw and return statements on same line

if (true) return result;
else if (false)
    System.out.println("unexpected condition");
else throw new Error();