The initial invocation of {@link #hasNext()} or {@link #next()} calls 40 | * this method, as does the first invocation of {@code hasNext} or {@code 41 | * next} following each successful call to {@code next}. Once the 42 | * implementation either invokes {@code endOfData} or throws an exception, 43 | * {@code computeNext} is guaranteed to never be called again. 44 | * 45 | *
If this method throws an exception, it will propagate outward to the 46 | * {@code hasNext} or {@code next} invocation that invoked this method. Any 47 | * further attempts to use the iterator will result in an {@link 48 | * IllegalStateException}. 49 | * 50 | *
The implementation of this method may not invoke the {@code hasNext}, 51 | * {@code next}, or {@link #peek()} methods on this instance; if it does, an 52 | * {@code IllegalStateException} will result. 53 | * 54 | * @return the next element if there was one. If {@code endOfData} was called 55 | * during execution, the return value will be ignored. 56 | * @throws RuntimeException if any unrecoverable error happens. This exception 57 | * will propagate outward to the {@code hasNext()}, {@code next()}, or 58 | * {@code peek()} invocation that invoked this method. Any further 59 | * attempts to use the iterator will result in an 60 | * {@link IllegalStateException}. 61 | */ 62 | protected abstract T computeNext(); 63 | 64 | /** 65 | * Implementations of {@link #computeNext} must invoke this method when 66 | * there are no elements left in the iteration. 67 | * 68 | * @return {@code null}; a convenience so your {@code computeNext} 69 | * implementation can use the simple statement {@code return endOfData();} 70 | */ 71 | protected final T endOfData() { 72 | state = State.DONE; 73 | return null; 74 | } 75 | 76 | @Override 77 | public final boolean hasNext() { 78 | if (state == State.FAILED) { 79 | throw new IllegalStateException(); 80 | } 81 | switch (state) { 82 | case DONE: 83 | return false; 84 | case READY: 85 | return true; 86 | default: 87 | } 88 | return tryToComputeNext(); 89 | } 90 | 91 | private boolean tryToComputeNext() { 92 | state = State.FAILED; // temporary pessimism 93 | next = computeNext(); 94 | if (state != State.DONE) { 95 | state = State.READY; 96 | return true; 97 | } 98 | return false; 99 | } 100 | 101 | @Override 102 | public final T next() { 103 | if (!hasNext()) { 104 | throw new NoSuchElementException(); 105 | } 106 | state = State.NOT_READY; 107 | T result = next; 108 | next = null; 109 | return result; 110 | } 111 | 112 | /** 113 | * Returns the next element in the iteration without advancing the iteration, 114 | * according to the contract of {@link PeekingIterator#peek()}. 115 | * 116 | *
Implementations of {@code AbstractIterator} that wish to expose this
117 | * functionality should implement {@code PeekingIterator}.
118 | */
119 | public final T peek() {
120 | if (!hasNext()) {
121 | throw new NoSuchElementException();
122 | }
123 | return next;
124 | }
125 |
126 | public final void remove() {
127 | throw new UnsupportedOperationException();
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/src/main/java/com/jmatio/common/DeterministicKeyMap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Code licensed under new-style BSD (see LICENSE).
3 | * All code up to tags/original: Copyright (c) 2006, Wojciech Gradkowski
4 | * All code after tags/original: Copyright (c) 2015, DiffPlug
5 | */
6 | package com.jmatio.common;
7 |
8 | import java.util.Collection;
9 | import java.util.Iterator;
10 | import java.util.Map;
11 | import java.util.Set;
12 |
13 | /**
14 | * A map implementation which guarantees that all of its iterators
15 | * (keySet(), values(), and entrySet()) will be in the same order
16 | * as the keyOrder set which is passed in the constructor.
17 | *
18 | * The keySet must contain all of the keys in the delegate, but it's
19 | * okay if the keySet contains more.
20 | *
21 | * Useful in MLObject and MLStruct for ensuring that arrays have
22 | * their fields in the same order.
23 | */
24 | public class DeterministicKeyMap