• Source
    1. import java.util.Objects;
    2.  
    3. class Ideone {
    4.  
    5. public static void main (String[] args) {
    6. Loop.from(0).to(10).execute(() -> {
    7. System.out.println("Hello World!");
    8. });
    9. }
    10. }
    11.  
    12. class Loop {
    13. private final int from;
    14. private final int to;
    15.  
    16. private Loop(Builder builder) {
    17. this.from = builder.from();
    18. this.to = builder.to();
    19. }
    20.  
    21. public static Builder from(int from) {
    22. return new Builder().from(from);
    23. }
    24.  
    25. public static Builder to(int to) {
    26. return new Builder().to(to);
    27. }
    28.  
    29. public void execute(Runnable executable) {
    30. for (int i = from; i < to; i++) {
    31. executable.run();
    32. }
    33. }
    34.  
    35. pupblic static class Builder {
    36. private int from = 0;
    37. private Integer to = null;
    38.  
    39. private Builder() {}
    40.  
    41. public Builder from(int from) {
    42. this.from = from;
    43. return this;
    44. }
    45.  
    46. private int from() {
    47. return from;
    48. }
    49.  
    50. public Builder to(int to) {
    51. this.to = to;
    52. return this;
    53. }
    54.  
    55. private int to() {
    56. return to;
    57. }
    58.  
    59. public void execute(Runnable runnable) {
    60. Objects.requireNonNull(runnable);
    61. new Loop(this).execute(runnable);
    62. }
    63. }
    64. }