PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/intermine/webservice/client/main/src/org/intermine/webservice/client/results/ResultRowList.java

https://gitlab.com/jsr38/intermine
Java | 237 lines | 177 code | 34 blank | 26 comment | 30 complexity | 31de891f41a45a5480aaaef0119e8c63 MD5 | raw file
  1. package org.intermine.webservice.client.results;
  2. /*
  3. * Copyright (C) 2002-2016 FlyMine
  4. *
  5. * This code may be freely distributed and modified under the
  6. * terms of the GNU Lesser General Public Licence. This should
  7. * be distributed with the code. See the LICENSE file for more
  8. * information or http://www.gnu.org/copyleft/lesser.html.
  9. *
  10. */
  11. import java.util.ArrayList;
  12. import java.util.Collection;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import java.util.ListIterator;
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. /**
  19. * A representation of a result row that allows parsed objects to be retrieved
  20. * by index, where the order of elements is the same as that of the view list
  21. * of the originating PathQuery.
  22. *
  23. * @author Alex Kalderimis
  24. *
  25. */
  26. public class ResultRowList implements List<Object>, Iterable<Object>
  27. {
  28. private final JSONArray data;
  29. /**
  30. * Construct a new result row object backed by the given JSONArray.
  31. * @param ja The source array.
  32. */
  33. public ResultRowList(JSONArray ja) {
  34. this.data = ja;
  35. }
  36. /**
  37. * Construct a new result row object backed by the JSONArray which can
  38. * be parsed from the given String.
  39. * @param input The source string.
  40. */
  41. public ResultRowList(String input) {
  42. try {
  43. this.data = new JSONArray(input);
  44. } catch (JSONException e) {
  45. throw new RuntimeException(e);
  46. }
  47. }
  48. @Override
  49. public boolean add(Object arg0) {
  50. throw new UnsupportedOperationException();
  51. }
  52. @Override
  53. public void add(int arg0, Object arg1) {
  54. throw new UnsupportedOperationException();
  55. }
  56. @Override
  57. public boolean addAll(Collection<? extends Object> arg0) {
  58. throw new UnsupportedOperationException();
  59. }
  60. @Override
  61. public boolean addAll(int arg0, Collection<? extends Object> arg1) {
  62. throw new UnsupportedOperationException();
  63. }
  64. @Override
  65. public void clear() {
  66. throw new UnsupportedOperationException();
  67. }
  68. @Override
  69. public boolean contains(Object value) {
  70. for (int i = 0; i < data.length(); i++) {
  71. Object thingy;
  72. try {
  73. thingy = data.getJSONObject(i).get("value");
  74. } catch (JSONException e) {
  75. throw new RuntimeException(e);
  76. }
  77. if (thingy == null && value == null) {
  78. return true;
  79. }
  80. if (thingy.equals(value)) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. @Override
  87. public boolean containsAll(Collection<?> arg0) {
  88. boolean c = true;
  89. for (Object o : arg0) {
  90. c = c && contains(o);
  91. }
  92. return c;
  93. }
  94. @Override
  95. public Object get(int arg0) {
  96. if (arg0 < 0 || arg0 >= data.length()) {
  97. throw new IndexOutOfBoundsException();
  98. }
  99. try {
  100. return data.getJSONObject(arg0).get("value");
  101. } catch (JSONException e) {
  102. throw new RuntimeException(e);
  103. }
  104. }
  105. @Override
  106. public int indexOf(Object arg0) {
  107. for (int i = 0; i < data.length(); i++) {
  108. Object thingy;
  109. try {
  110. thingy = data.getJSONObject(i).get("value");
  111. } catch (JSONException e) {
  112. throw new RuntimeException(e);
  113. }
  114. if (thingy == null && arg0 == null) {
  115. return i;
  116. } else if (thingy != null && thingy.equals(arg0)) {
  117. return i;
  118. }
  119. }
  120. return -1;
  121. }
  122. @Override
  123. public boolean isEmpty() {
  124. return data.length() == 0;
  125. }
  126. @Override
  127. public Iterator<Object> iterator() {
  128. List<Object> values = getValuesInternal();
  129. return values.iterator();
  130. }
  131. @Override
  132. public int lastIndexOf(Object arg0) {
  133. for (int i = data.length() - 1; i >= 0; i--) {
  134. Object thingy;
  135. try {
  136. thingy = data.getJSONObject(i).get("value");
  137. } catch (JSONException e) {
  138. throw new RuntimeException(e);
  139. }
  140. if (thingy == null && arg0 == null) {
  141. return i;
  142. } else if (thingy != null && thingy.equals(arg0)) {
  143. return i;
  144. }
  145. }
  146. return -1;
  147. }
  148. private List<Object> getValuesInternal() {
  149. List<Object> values = new ArrayList<Object>();
  150. for (int i = 0; i < data.length(); i++) {
  151. try {
  152. values.add(data.getJSONObject(i).get("value"));
  153. } catch (JSONException e) {
  154. throw new RuntimeException(e);
  155. }
  156. }
  157. return values;
  158. }
  159. @Override
  160. public ListIterator<Object> listIterator() {
  161. return getValuesInternal().listIterator();
  162. }
  163. @Override
  164. public ListIterator<Object> listIterator(int arg0) {
  165. return getValuesInternal().listIterator(arg0);
  166. }
  167. @Override
  168. public boolean remove(Object arg0) {
  169. throw new UnsupportedOperationException();
  170. }
  171. @Override
  172. public Object remove(int arg0) {
  173. throw new UnsupportedOperationException();
  174. }
  175. @Override
  176. public boolean removeAll(Collection<?> arg0) {
  177. throw new UnsupportedOperationException();
  178. }
  179. @Override
  180. public boolean retainAll(Collection<?> arg0) {
  181. throw new UnsupportedOperationException();
  182. }
  183. @Override
  184. public Object set(int arg0, Object arg1) {
  185. throw new UnsupportedOperationException();
  186. }
  187. @Override
  188. public int size() {
  189. return data.length();
  190. }
  191. @Override
  192. public List<Object> subList(int arg0, int arg1) {
  193. return getValuesInternal().subList(arg0, arg1);
  194. }
  195. @Override
  196. public Object[] toArray() {
  197. return getValuesInternal().toArray();
  198. }
  199. @Override
  200. public <T> T[] toArray(T[] arg0) {
  201. return getValuesInternal().toArray(arg0);
  202. }
  203. }