Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | package de.viathinksoft.distributed.apps.immortal.deepcheck; |
2 | |||
3 | import java.math.BigInteger; |
||
4 | import java.util.Iterator; |
||
5 | |||
6 | // TODO: Iterable aufspalten? |
||
7 | |||
8 | public class DeepImmortalCheckIterator implements Iterator<DeepImmortalCheckState>, |
||
9 | Iterable<DeepImmortalCheckState> { |
||
10 | |||
11 | private DeepImmortalCheckState position; |
||
12 | |||
13 | public DeepImmortalCheckIterator(DeepImmortalCheckState start) { |
||
14 | this.position = start; |
||
15 | } |
||
16 | |||
17 | @Override |
||
18 | public boolean hasNext() { |
||
19 | return true; |
||
20 | } |
||
21 | |||
22 | @Override |
||
23 | public DeepImmortalCheckState next() { |
||
24 | if (!position.isImmortal() && (position.getCount().compareTo(BigInteger.ZERO) > 0)) return position; |
||
25 | |||
26 | BigInteger lastResult = position.getLastResult(); |
||
27 | BigInteger initialNumber = position.getInitialNumber(); |
||
28 | BigInteger counter = position.getCount().add(BigInteger.ONE); |
||
29 | |||
30 | lastResult = lastResult.multiply(initialNumber); |
||
31 | boolean isImmortable = lastResult.toString().endsWith(initialNumber.toString()); |
||
32 | |||
33 | position = new DeepImmortalCheckState(isImmortable, counter, initialNumber, lastResult); |
||
34 | |||
35 | return position; |
||
36 | } |
||
37 | |||
38 | @Override |
||
39 | public void remove() { |
||
40 | throw new UnsupportedOperationException(); |
||
41 | } |
||
42 | |||
43 | @Override |
||
44 | public Iterator<DeepImmortalCheckState> iterator() { |
||
45 | return this; |
||
46 | } |
||
47 | |||
48 | } |