/src/main/java/com/alibaba/fastjson/JSONArray.java

https://github.com/flydream/fastjson · Java · 345 lines · 238 code · 89 blank · 18 comment · 18 complexity · f0a0024e81cfc165386d1e0e1b2a6985 MD5 · raw file

  1. /*
  2. * Copyright 1999-2101 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.fastjson;
  17. import static com.alibaba.fastjson.util.TypeUtils.castToBigDecimal;
  18. import static com.alibaba.fastjson.util.TypeUtils.castToBigInteger;
  19. import static com.alibaba.fastjson.util.TypeUtils.castToBoolean;
  20. import static com.alibaba.fastjson.util.TypeUtils.castToByte;
  21. import static com.alibaba.fastjson.util.TypeUtils.castToDate;
  22. import static com.alibaba.fastjson.util.TypeUtils.castToDouble;
  23. import static com.alibaba.fastjson.util.TypeUtils.castToFloat;
  24. import static com.alibaba.fastjson.util.TypeUtils.castToInt;
  25. import static com.alibaba.fastjson.util.TypeUtils.castToLong;
  26. import static com.alibaba.fastjson.util.TypeUtils.castToShort;
  27. import static com.alibaba.fastjson.util.TypeUtils.castToSqlDate;
  28. import static com.alibaba.fastjson.util.TypeUtils.castToString;
  29. import static com.alibaba.fastjson.util.TypeUtils.castToTimestamp;
  30. import java.io.Serializable;
  31. import java.math.BigDecimal;
  32. import java.math.BigInteger;
  33. import java.util.ArrayList;
  34. import java.util.Collection;
  35. import java.util.Iterator;
  36. import java.util.List;
  37. import java.util.ListIterator;
  38. import java.util.RandomAccess;
  39. import com.alibaba.fastjson.util.TypeUtils;
  40. /**
  41. * @author wenshao<szujobs@hotmail.com>
  42. */
  43. public class JSONArray extends JSON implements List<Object>, JSONAware, Cloneable, RandomAccess, Serializable {
  44. private static final long serialVersionUID = 1L;
  45. private final List<Object> list;
  46. public JSONArray(){
  47. this.list = new ArrayList<Object>(10);
  48. }
  49. public JSONArray(List<Object> list){
  50. this.list = list;
  51. }
  52. public JSONArray(int initialCapacity){
  53. this.list = new ArrayList<Object>(initialCapacity);
  54. }
  55. public int size() {
  56. return list.size();
  57. }
  58. public boolean isEmpty() {
  59. return list.isEmpty();
  60. }
  61. public boolean contains(Object o) {
  62. return list.contains(o);
  63. }
  64. public Iterator<Object> iterator() {
  65. return list.iterator();
  66. }
  67. public Object[] toArray() {
  68. return list.toArray();
  69. }
  70. public <T> T[] toArray(T[] a) {
  71. return list.toArray(a);
  72. }
  73. public boolean add(Object e) {
  74. return list.add(e);
  75. }
  76. public boolean remove(Object o) {
  77. return list.remove(o);
  78. }
  79. public boolean containsAll(Collection<?> c) {
  80. return list.containsAll(c);
  81. }
  82. public boolean addAll(Collection<? extends Object> c) {
  83. return list.addAll(c);
  84. }
  85. public boolean addAll(int index, Collection<? extends Object> c) {
  86. return list.addAll(index, c);
  87. }
  88. public boolean removeAll(Collection<?> c) {
  89. return list.removeAll(c);
  90. }
  91. public boolean retainAll(Collection<?> c) {
  92. return list.retainAll(c);
  93. }
  94. public void clear() {
  95. list.clear();
  96. }
  97. public Object set(int index, Object element) {
  98. return list.set(index, element);
  99. }
  100. public void add(int index, Object element) {
  101. list.add(index, element);
  102. }
  103. public Object remove(int index) {
  104. return list.remove(index);
  105. }
  106. public int indexOf(Object o) {
  107. return list.indexOf(o);
  108. }
  109. public int lastIndexOf(Object o) {
  110. return list.lastIndexOf(o);
  111. }
  112. public ListIterator<Object> listIterator() {
  113. return list.listIterator();
  114. }
  115. public ListIterator<Object> listIterator(int index) {
  116. return list.listIterator(index);
  117. }
  118. public List<Object> subList(int fromIndex, int toIndex) {
  119. return list.subList(fromIndex, toIndex);
  120. }
  121. public Object get(int index) {
  122. return list.get(index);
  123. }
  124. public JSONObject getJSONObject(int index) {
  125. Object value = list.get(index);
  126. if (value instanceof JSONObject) {
  127. return (JSONObject) value;
  128. }
  129. return (JSONObject) toJSON(value);
  130. }
  131. public JSONArray getJSONArray(int index) {
  132. Object value = list.get(index);
  133. if (value instanceof JSONArray) {
  134. return (JSONArray) value;
  135. }
  136. return (JSONArray) toJSON(value);
  137. }
  138. public <T> T getObject(int index, Class<T> clazz) {
  139. Object obj = list.get(index);
  140. return TypeUtils.castToJavaBean(obj, clazz);
  141. }
  142. public Boolean getBoolean(int index) {
  143. Object value = get(index);
  144. if (value == null) {
  145. return null;
  146. }
  147. return castToBoolean(value);
  148. }
  149. public boolean getBooleanValue(int index) {
  150. Object value = get(index);
  151. if (value == null) {
  152. return false;
  153. }
  154. return castToBoolean(value).booleanValue();
  155. }
  156. public Byte getByte(int index) {
  157. Object value = get(index);
  158. return castToByte(value);
  159. }
  160. public byte getByteValue(int index) {
  161. Object value = get(index);
  162. if (value == null) {
  163. return 0;
  164. }
  165. return castToByte(value).byteValue();
  166. }
  167. public Short getShort(int index) {
  168. Object value = get(index);
  169. return castToShort(value);
  170. }
  171. public short getShortValue(int index) {
  172. Object value = get(index);
  173. if (value == null) {
  174. return 0;
  175. }
  176. return castToShort(value).shortValue();
  177. }
  178. public Integer getInteger(int index) {
  179. Object value = get(index);
  180. return castToInt(value);
  181. }
  182. public int getIntValue(int index) {
  183. Object value = get(index);
  184. if (value == null) {
  185. return 0;
  186. }
  187. return castToInt(value).intValue();
  188. }
  189. public Long getLong(int index) {
  190. Object value = get(index);
  191. return castToLong(value);
  192. }
  193. public long getLongValue(int index) {
  194. Object value = get(index);
  195. if (value == null) {
  196. return 0L;
  197. }
  198. return castToLong(value).longValue();
  199. }
  200. public Float getFloat(int index) {
  201. Object value = get(index);
  202. return castToFloat(value);
  203. }
  204. public float getFloatValue(int index) {
  205. Object value = get(index);
  206. if (value == null) {
  207. return 0F;
  208. }
  209. return castToFloat(value).floatValue();
  210. }
  211. public Double getDouble(int index) {
  212. Object value = get(index);
  213. return castToDouble(value);
  214. }
  215. public double getDoubleValue(int index) {
  216. Object value = get(index);
  217. if (value == null) {
  218. return 0D;
  219. }
  220. return castToDouble(value).floatValue();
  221. }
  222. public BigDecimal getBigDecimal(int index) {
  223. Object value = get(index);
  224. return castToBigDecimal(value);
  225. }
  226. public BigInteger getBigInteger(int index) {
  227. Object value = get(index);
  228. return castToBigInteger(value);
  229. }
  230. public String getString(int index) {
  231. Object value = get(index);
  232. return castToString(value);
  233. }
  234. public java.util.Date getDate(int index) {
  235. Object value = get(index);
  236. return castToDate(value);
  237. }
  238. public java.sql.Date getSqlDate(int index) {
  239. Object value = get(index);
  240. return castToSqlDate(value);
  241. }
  242. public java.sql.Timestamp getTimestamp(int index) {
  243. Object value = get(index);
  244. return castToTimestamp(value);
  245. }
  246. @Override
  247. public Object clone() {
  248. return new JSONArray(new ArrayList<Object>(list));
  249. }
  250. public boolean equals(Object obj) {
  251. return this.list.equals(obj);
  252. }
  253. public int hashCode() {
  254. return this.list.hashCode();
  255. }
  256. }