fork download
  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 LoopFromSetBuilder from(int from) {
  22. return new Builder().from(from);
  23. }
  24.  
  25. public static LoopToSetBuilder 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. public static class Builder implements LoopFromSetBuilder, LoopToSetBuilder, LoopAllSetBuilder {
  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.  
  65. interface LoopFromSetBuilder {
  66. LoopAllSetBuilder to(int to);
  67. }
  68.  
  69. interface LoopToSetBuilder {
  70. LoopAllSetBuilder from(int from);
  71. }
  72.  
  73. interface LoopAllSetBuilder {
  74. void execute(Runnable runnable);
  75. }
  76. }
Success #stdin #stdout 0.06s 33680KB
stdin
Standard input is empty
stdout
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!