With Lombok
01 import static lombok.Yield.yield;
02
03 public class YieldExample {
04 public void test() {
05 for (Long fib : filterMod(5, fibunacci())) {
06 System.out.println(fib);
07 }
08 }
09
10 public Iterable<Long> fibunacci() {
11 long a = 0;
12 long b = 1;
13 while(b > 0) {
14 yield(a);
15 long c = a + b;
16 a = b;
17 b = c;
18 }
19 }
20
21 public Iterable<Long> filterMod(final long mod, final Iterable<Long> values) {
22 for(Long value : values) if ((value.longValue() % mod) == 0) yield(value);
23 }
24 }
Vanilla Java
001 public class YieldExample {
002 public void test() {
003 for (Long fib : filterMod(5, fibunacci())) {
004 System.out.println(fib);
005 }
006 }
007
008 public Iterable<Long> fibunacci() {
009 class $YielderFibunacci implements Iterator<Long>, Iterable<Long> {
010 private long a;
011 private long b;
012 private long c;
013 private int $state;
014 private boolean $hasNext;
015 private boolean $nextDefined;
016 private Long $next;
017
018 public Iterator<Long> iterator() {
019 return new $YielderFibunacci();
020 }
021
022 public boolean hasNext() {
023 if (!$nextDefined) {
024 $hasNext = getNext();
025 $nextDefined = true;
026 }
027 return $hasNext;
028 }
029
030 public Long next() {
031 if (!hasNext()) throw new NoSuchElementException();
032 $nextDefined = false;
033 return $next;
034 }
035
036 public void remove() {
037 throw new UnsupportedOperationException();
038 }
039
040 private boolean getNext() {
041 while (true) switch ($state) {
042 case 0:
043 a = 0;
044 b = 1;
045 case 1:
046 if (!(b <= 0)) {
047 $state = 3;
048 continue;
049 }
050 $next = a;
051 $state = 2;
052 return true;
053 case 2:
054 c = a + b;
055 a = b;
056 b = c;
057 $state = 1;
058 continue;
059 case 3:
060 default:
061 return false;
062 }
063 }
064 }
065 return new $YielderFibunacci();
066 }
067
068 public Iterable<Long> filterMod(final long mod, final Iterable<Long> values) {
069 class $YielderFilterMod implements Iterator<Long>, Iterable<Long> {
070 private Long value;
071 private Iterator $valueIter;
072 private int $state;
073 private boolean $hasNext;
074 private boolean $nextDefined;
075 private Long $next;
076
077 public Iterator<Long> iterator() {
078 return new $YielderFilterMod();
079 }
080
081 public boolean hasNext() {
082 if (!$nextDefined) {
083 $hasNext = getNext();
084 $nextDefined = true;
085 }
086 return $hasNext;
087 }
088
089 public Long next() {
090 if (!hasNext()) throw new NoSuchElementException();
091 $nextDefined = false;
092 return $next;
093 }
094
095 public void remove() {
096 throw new UnsupportedOperationException();
097 }
098
099 private boolean getNext() {
100 while (true) switch ($state) {
101 case 0:
102 $valueIter = values.iterator();
103 case 1:
104 if (!$valueIter.hasNext()) {
105 $state = 3;
106 continue;
107 }
108 value = (Long)$valueIter.next();
109 if (!((value.longValue() % mod) == 0)) {
110 $state = 2;
111 continue;
112 }
113 $next = value;
114 $state = 2;
115 return true;
116 case 2:
117 $state = 1;
118 continue;
119 case 3:
120 default:
121 return false;
122 }
123 }
124 }
125 return new $YielderFilterMod();
126 }
127 }