PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 368 lines | 253 code | 93 blank | 22 comment | 18 complexity | 194fcc9ed2c7db1f09d4db0b2df99b38 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.lang.reflect.Type;
  32. import java.math.BigDecimal;
  33. import java.math.BigInteger;
  34. import java.util.ArrayList;
  35. import java.util.Collection;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import java.util.ListIterator;
  39. import java.util.RandomAccess;
  40. import com.alibaba.fastjson.util.TypeUtils;
  41. /**
  42. * @author wenshao<szujobs@hotmail.com>
  43. */
  44. public class JSONArray extends JSON implements List<Object>, JSONAware, Cloneable, RandomAccess, Serializable {
  45. private static final long serialVersionUID = 1L;
  46. private final List<Object> list;
  47. protected transient Object relatedArray;
  48. protected transient Type componentType;
  49. public JSONArray(){
  50. this.list = new ArrayList<Object>(10);
  51. }
  52. public JSONArray(List<Object> list){
  53. this.list = list;
  54. }
  55. public JSONArray(int initialCapacity){
  56. this.list = new ArrayList<Object>(initialCapacity);
  57. }
  58. /**
  59. * @since 1.1.16
  60. * @return
  61. */
  62. public Object getRelatedArray() {
  63. return relatedArray;
  64. }
  65. public void setRelatedArray(Object relatedArray) {
  66. this.relatedArray = relatedArray;
  67. }
  68. public Type getComponentType() {
  69. return componentType;
  70. }
  71. public void setComponentType(Type componentType) {
  72. this.componentType = componentType;
  73. }
  74. public int size() {
  75. return list.size();
  76. }
  77. public boolean isEmpty() {
  78. return list.isEmpty();
  79. }
  80. public boolean contains(Object o) {
  81. return list.contains(o);
  82. }
  83. public Iterator<Object> iterator() {
  84. return list.iterator();
  85. }
  86. public Object[] toArray() {
  87. return list.toArray();
  88. }
  89. public <T> T[] toArray(T[] a) {
  90. return list.toArray(a);
  91. }
  92. public boolean add(Object e) {
  93. return list.add(e);
  94. }
  95. public boolean remove(Object o) {
  96. return list.remove(o);
  97. }
  98. public boolean containsAll(Collection<?> c) {
  99. return list.containsAll(c);
  100. }
  101. public boolean addAll(Collection<? extends Object> c) {
  102. return list.addAll(c);
  103. }
  104. public boolean addAll(int index, Collection<? extends Object> c) {
  105. return list.addAll(index, c);
  106. }
  107. public boolean removeAll(Collection<?> c) {
  108. return list.removeAll(c);
  109. }
  110. public boolean retainAll(Collection<?> c) {
  111. return list.retainAll(c);
  112. }
  113. public void clear() {
  114. list.clear();
  115. }
  116. public Object set(int index, Object element) {
  117. return list.set(index, element);
  118. }
  119. public void add(int index, Object element) {
  120. list.add(index, element);
  121. }
  122. public Object remove(int index) {
  123. return list.remove(index);
  124. }
  125. public int indexOf(Object o) {
  126. return list.indexOf(o);
  127. }
  128. public int lastIndexOf(Object o) {
  129. return list.lastIndexOf(o);
  130. }
  131. public ListIterator<Object> listIterator() {
  132. return list.listIterator();
  133. }
  134. public ListIterator<Object> listIterator(int index) {
  135. return list.listIterator(index);
  136. }
  137. public List<Object> subList(int fromIndex, int toIndex) {
  138. return list.subList(fromIndex, toIndex);
  139. }
  140. public Object get(int index) {
  141. return list.get(index);
  142. }
  143. public JSONObject getJSONObject(int index) {
  144. Object value = list.get(index);
  145. if (value instanceof JSONObject) {
  146. return (JSONObject) value;
  147. }
  148. return (JSONObject) toJSON(value);
  149. }
  150. public JSONArray getJSONArray(int index) {
  151. Object value = list.get(index);
  152. if (value instanceof JSONArray) {
  153. return (JSONArray) value;
  154. }
  155. return (JSONArray) toJSON(value);
  156. }
  157. public <T> T getObject(int index, Class<T> clazz) {
  158. Object obj = list.get(index);
  159. return TypeUtils.castToJavaBean(obj, clazz);
  160. }
  161. public Boolean getBoolean(int index) {
  162. Object value = get(index);
  163. if (value == null) {
  164. return null;
  165. }
  166. return castToBoolean(value);
  167. }
  168. public boolean getBooleanValue(int index) {
  169. Object value = get(index);
  170. if (value == null) {
  171. return false;
  172. }
  173. return castToBoolean(value).booleanValue();
  174. }
  175. public Byte getByte(int index) {
  176. Object value = get(index);
  177. return castToByte(value);
  178. }
  179. public byte getByteValue(int index) {
  180. Object value = get(index);
  181. if (value == null) {
  182. return 0;
  183. }
  184. return castToByte(value).byteValue();
  185. }
  186. public Short getShort(int index) {
  187. Object value = get(index);
  188. return castToShort(value);
  189. }
  190. public short getShortValue(int index) {
  191. Object value = get(index);
  192. if (value == null) {
  193. return 0;
  194. }
  195. return castToShort(value).shortValue();
  196. }
  197. public Integer getInteger(int index) {
  198. Object value = get(index);
  199. return castToInt(value);
  200. }
  201. public int getIntValue(int index) {
  202. Object value = get(index);
  203. if (value == null) {
  204. return 0;
  205. }
  206. return castToInt(value).intValue();
  207. }
  208. public Long getLong(int index) {
  209. Object value = get(index);
  210. return castToLong(value);
  211. }
  212. public long getLongValue(int index) {
  213. Object value = get(index);
  214. if (value == null) {
  215. return 0L;
  216. }
  217. return castToLong(value).longValue();
  218. }
  219. public Float getFloat(int index) {
  220. Object value = get(index);
  221. return castToFloat(value);
  222. }
  223. public float getFloatValue(int index) {
  224. Object value = get(index);
  225. if (value == null) {
  226. return 0F;
  227. }
  228. return castToFloat(value).floatValue();
  229. }
  230. public Double getDouble(int index) {
  231. Object value = get(index);
  232. return castToDouble(value);
  233. }
  234. public double getDoubleValue(int index) {
  235. Object value = get(index);
  236. if (value == null) {
  237. return 0D;
  238. }
  239. return castToDouble(value);
  240. }
  241. public BigDecimal getBigDecimal(int index) {
  242. Object value = get(index);
  243. return castToBigDecimal(value);
  244. }
  245. public BigInteger getBigInteger(int index) {
  246. Object value = get(index);
  247. return castToBigInteger(value);
  248. }
  249. public String getString(int index) {
  250. Object value = get(index);
  251. return castToString(value);
  252. }
  253. public java.util.Date getDate(int index) {
  254. Object value = get(index);
  255. return castToDate(value);
  256. }
  257. public java.sql.Date getSqlDate(int index) {
  258. Object value = get(index);
  259. return castToSqlDate(value);
  260. }
  261. public java.sql.Timestamp getTimestamp(int index) {
  262. Object value = get(index);
  263. return castToTimestamp(value);
  264. }
  265. @Override
  266. public Object clone() {
  267. return new JSONArray(new ArrayList<Object>(list));
  268. }
  269. public boolean equals(Object obj) {
  270. return this.list.equals(obj);
  271. }
  272. public int hashCode() {
  273. return this.list.hashCode();
  274. }
  275. }