|
30 | 30 | final class JSONObjectEnumerator { |
31 | 31 |
|
32 | 32 | private JSONObject m_jsonObject; |
33 | | - private boolean m_bStarted; |
34 | | - private int m_currentIndex; |
| 33 | + private int m_troolean; |
35 | 34 | private Iterator<?> m_keys_iter; |
36 | 35 | private String m_current_key; |
37 | 36 |
|
38 | 37 | JSONObjectEnumerator(JSONObject jsonObject) { |
39 | | - m_bStarted = false; |
40 | | - m_currentIndex = -1; |
| 38 | + m_troolean = 0; |
41 | 39 | m_jsonObject = jsonObject; |
42 | 40 | } |
43 | 41 |
|
44 | 42 | String getCurrentKey() { |
45 | | - if (!m_bStarted) { |
46 | | - throw new GeometryException("invalid call"); |
47 | | - } |
48 | | - |
49 | | - if (m_currentIndex == m_jsonObject.length()) { |
| 43 | + if (m_troolean != 1) { |
50 | 44 | throw new GeometryException("invalid call"); |
51 | 45 | } |
52 | 46 |
|
53 | 47 | return m_current_key; |
54 | 48 | } |
55 | 49 |
|
56 | 50 | Object getCurrentObject() { |
57 | | - if (!m_bStarted) { |
58 | | - throw new GeometryException("invalid call"); |
59 | | - } |
60 | | - |
61 | | - if (m_currentIndex == m_jsonObject.length()) { |
| 51 | + if (m_troolean != 1) { |
62 | 52 | throw new GeometryException("invalid call"); |
63 | 53 | } |
64 | 54 |
|
65 | 55 | return m_jsonObject.opt(m_current_key); |
66 | 56 | } |
67 | 57 |
|
68 | 58 | boolean next() { |
69 | | - if (!m_bStarted) { |
70 | | - m_currentIndex = 0; |
71 | | - m_keys_iter = m_jsonObject.keys(); |
72 | | - m_bStarted = true; |
73 | | - if (m_keys_iter.hasNext()) { |
74 | | - m_current_key = (String)m_keys_iter.next(); |
| 59 | + if (m_troolean == 0) { |
| 60 | + if (m_jsonObject.length() > 0) { |
| 61 | + m_keys_iter = m_jsonObject.keys(); |
| 62 | + m_troolean = 1;//started |
75 | 63 | } |
76 | | - |
77 | | - } else if (m_currentIndex != m_jsonObject.length()) { |
| 64 | + else { |
| 65 | + m_troolean = -1;//stopped |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (m_troolean == 1) {//still exploring |
78 | 70 | if (m_keys_iter.hasNext()) { |
79 | 71 | m_current_key = (String)m_keys_iter.next(); |
80 | 72 | } |
81 | | - |
82 | | - m_currentIndex++; |
| 73 | + else { |
| 74 | + m_troolean = -1; //done |
| 75 | + } |
83 | 76 | } |
84 | 77 |
|
85 | | - return m_currentIndex != m_jsonObject.length(); |
| 78 | + return m_troolean == 1; |
86 | 79 | } |
87 | 80 | } |
0 commit comments