|
23 | 23 | */ |
24 | 24 | package com.esri.core.geometry; |
25 | 25 |
|
26 | | -import java.util.ArrayList; |
27 | | -import org.codehaus.jackson.JsonParser; |
28 | | -import org.codehaus.jackson.JsonToken; |
29 | | -import org.json.JSONArray; |
30 | 26 | import org.json.JSONObject; |
31 | 27 |
|
| 28 | +import java.util.Iterator; |
| 29 | + |
32 | 30 | final class JSONObjectEnumerator { |
33 | 31 |
|
34 | 32 | private JSONObject m_jsonObject; |
35 | | - private boolean m_bStarted; |
36 | | - private int m_currentIndex; |
37 | | - private String[] m_keys; |
| 33 | + private int m_troolean; |
| 34 | + private Iterator<?> m_keys_iter; |
| 35 | + private String m_current_key; |
38 | 36 |
|
39 | 37 | JSONObjectEnumerator(JSONObject jsonObject) { |
40 | | - m_bStarted = false; |
41 | | - m_currentIndex = -1; |
| 38 | + m_troolean = 0; |
42 | 39 | m_jsonObject = jsonObject; |
43 | 40 | } |
44 | 41 |
|
45 | 42 | String getCurrentKey() { |
46 | | - if (!m_bStarted) { |
47 | | - throw new GeometryException("invalid call"); |
48 | | - } |
49 | | - |
50 | | - if (m_currentIndex == m_jsonObject.length()) { |
| 43 | + if (m_troolean != 1) { |
51 | 44 | throw new GeometryException("invalid call"); |
52 | 45 | } |
53 | 46 |
|
54 | | - return m_keys[m_currentIndex]; |
| 47 | + return m_current_key; |
55 | 48 | } |
56 | 49 |
|
57 | 50 | Object getCurrentObject() { |
58 | | - if (!m_bStarted) { |
| 51 | + if (m_troolean != 1) { |
59 | 52 | throw new GeometryException("invalid call"); |
60 | 53 | } |
61 | 54 |
|
62 | | - if (m_currentIndex == m_jsonObject.length()) { |
63 | | - throw new GeometryException("invalid call"); |
64 | | - } |
65 | | - |
66 | | - return m_jsonObject.opt(m_keys[m_currentIndex]); |
| 55 | + return m_jsonObject.opt(m_current_key); |
67 | 56 | } |
68 | 57 |
|
69 | 58 | boolean next() { |
70 | | - if (!m_bStarted) { |
71 | | - m_currentIndex = 0; |
72 | | - m_keys = JSONObject.getNames(m_jsonObject); |
73 | | - m_bStarted = true; |
74 | | - } else if (m_currentIndex != m_jsonObject.length()) { |
75 | | - m_currentIndex++; |
| 59 | + if (m_troolean == 0) { |
| 60 | + if (m_jsonObject.length() > 0) { |
| 61 | + m_keys_iter = m_jsonObject.keys(); |
| 62 | + m_troolean = 1;//started |
| 63 | + } |
| 64 | + else { |
| 65 | + m_troolean = -1;//stopped |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (m_troolean == 1) {//still exploring |
| 70 | + if (m_keys_iter.hasNext()) { |
| 71 | + m_current_key = (String)m_keys_iter.next(); |
| 72 | + } |
| 73 | + else { |
| 74 | + m_troolean = -1; //done |
| 75 | + } |
76 | 76 | } |
77 | 77 |
|
78 | | - return m_currentIndex != m_jsonObject.length(); |
| 78 | + return m_troolean == 1; |
79 | 79 | } |
80 | 80 | } |
0 commit comments