Guest User

Untitled

a guest
Oct 20th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. package com.n1nja.utils;
  2. import java.util.Iterator;
  3. /**
  4. * Allows for Python-like {@code foreach} loop iteration with
  5. * {@link java.lang.Integer Integer}s. Use as follows.
  6. * <pre>
  7. * for ({@link java.lang.Integer Integer} l : new Range());
  8. * </pre>
  9. * @author Colin Haber
  10. * @version 0.1.0
  11. */
  12. public class Range implements Iterable<Integer> {
  13. /**
  14. * The default {@linkplain #start starting value} for {@link Range}s that
  15. * do not explicitly define one.
  16. * @since 0.1.0
  17. * @see #Range(Integer)
  18. */
  19. private static Integer DEFAULT_START = 0;
  20. /**
  21. * The default {@linkplain #step step size} for {@link Range}s that do not
  22. * explicitly define one.
  23. * @since 0.1.0
  24. * @see #Range(Integer)
  25. * @see #Range(Integer, Integer)
  26. */
  27. private static Integer DEFAULT_STEP = 1;
  28. /**
  29. * The {@link Range}'s starting value, inclusive.
  30. * @since 0.0.0
  31. */
  32. private final Integer start;
  33. /**
  34. * The {@link Range}'s ending value, non-inclusive.
  35. * @since 0.0.0
  36. */
  37. private final Integer end;
  38. /**
  39. * The {@link Range}'s step (or interval) size.
  40. * @since 0.0.0
  41. */
  42. private final Integer step;
  43. /**
  44. * Creates a {@link Range} from {@code 0}, inclusive, to {@code count},
  45. * non-inclusive, with a {@linkplain #step} size of {@code 1}.
  46. * @since 0.0.0
  47. * @param count the {@link #end} value for this {@link Range},
  48. * non-inclusive
  49. */
  50. public Range(Integer count) {
  51. this(Range.DEFAULT_START, count);
  52. }
  53. /**
  54. * Creates a {@link Range} from {@code start}, inclusive, to {@code end},
  55. * non-inclusive, with a {@linkplain #step} size of {@code 1}.
  56. * @since 0.0.0
  57. * @param start the {@link #start} value for this {@link Range}, inclusive
  58. * @param end the {@link #end} value for this {@link Range}, non-inclusive
  59. */
  60. public Range(Integer start, Integer end) {
  61. this(start, end, Range.DEFAULT_STEP);
  62. }
  63. /**
  64. * Creates a {@link Range} from {@code start}, inclusive, to {@code end},
  65. * non-inclusive, with a {@linkplain #step} size of {@code step}.
  66. * @since 0.0.0
  67. * @param start the {@link #start} value for this {@link Range}, inclusive
  68. * @param end the {@link #end} value for this {@link Range}, non-inclusive
  69. * @param step the {@link #step} size for this {@link Range}
  70. */
  71. public Range(Integer start, Integer end, Integer step) {
  72. this.start = start;
  73. this.end = end;
  74. this.step = step;
  75. }
  76. @Override
  77. public Iterator<Integer> iterator() {
  78. final Integer startVal = this.start;
  79. final Integer endVal = this.end;
  80. final Integer stepSize = this.step;
  81. return new Iterator<Integer>() {
  82. private final Integer start = startVal;
  83. private final Integer end = endVal;
  84. private final Integer step = stepSize;
  85. private Integer index;
  86. {
  87. if ((this.end - this.start != 0) && (Math.signum(this.end - this.start) != Math.signum(this.step))) {
  88. throw new IllegalArgumentException("Infinite looping statement detected.");
  89. }
  90. this.index = 0;
  91. }
  92. @Override
  93. public boolean hasNext() {
  94. return ((this.start < this.end) && (this.start + (this.step * this.index) < this.end) || ((this.start > this.end) && (this.start + (this.step * this.index) > this.end)));
  95. }
  96. @Override
  97. public synchronized Integer next() {
  98. Integer next = null;
  99. if (this.hasNext()) {
  100. next = this.start + (this.step * this.index);
  101. this.index++;
  102. }
  103. return next;
  104. }
  105. @Override
  106. public void remove() {
  107. throw new UnsupportedOperationException(this.getClass().getSimpleName() + " does not support remove().");
  108. }
  109. };
  110. }
  111. }
Add Comment
Please, Sign In to add comment