KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > json > client > JSONArray


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.json.client;
17
18 import com.google.gwt.core.client.JavaScriptObject;
19
20 /**
21  * Represents an array of {@link com.google.gwt.json.client.JSONValue} objects.
22  */

23 public class JSONArray extends JSONValue {
24
25   final JavaScriptObject javascriptArray;
26
27   final JavaScriptObject wrappedArray;
28
29   /**
30    * Creates an empty JSONArray.
31    */

32   public JSONArray() {
33     javascriptArray = createArray();
34     wrappedArray = createArray();
35   }
36
37   /**
38    * Creates a new JSONArray from the supplied JavaScriptObject representing a
39    * JavaScript array.
40    *
41    * @param arr a JavaScript array
42    */

43   public JSONArray(JavaScriptObject arr) {
44     javascriptArray = arr;
45     wrappedArray = createArray();
46   }
47
48   /**
49    * Returns the value at the specified index position.
50    *
51    * @param index the index of the array item to retrieve
52    * @return the value at this index, or <code>null</code> if this index is
53    * empty
54    */

55   public JSONValue get(int index) throws JSONException {
56     if (wrappedTest(index)) {
57       return wrappedGet(index);
58     }
59     JSONValue wrapped = null;
60     if (rawTest(index)) {
61       wrapped = JSONParser.buildValue(rawGet(index));
62       rawSet(index, null);
63     }
64     wrappedSet(index, wrapped);
65     return wrapped;
66   }
67
68   /**
69    * Returns <code>this</code>, as this is a JSONArray.
70    */

71   public JSONArray isArray() {
72     return this;
73   }
74
75   /**
76    * Sets the specified index to the given value.
77    *
78    * @param index the index to set
79    * @param jsonValue the value to set
80    * @return the previous value at this index, or <code>null</code> if this
81    * index was empty
82    */

83   public JSONValue set(int index, JSONValue jsonValue) {
84     JSONValue out = get(index);
85     wrappedSet(index, jsonValue);
86     rawSet(index, null);
87     return out;
88   }
89
90   /**
91    * Returns the number of elements in this array.
92    *
93    * @return size of this array
94    */

95   public native int size() /*-{
96     return this.@com.google.gwt.json.client.JSONArray::javascriptArray.length;
97   }-*/
;
98
99   /**
100    * Create the JSON encoded string representation of this JSONArray instance.
101    * This method may take a long time to execute if the underlying array is
102    * large.
103    */

104   public String JavaDoc toString() throws JSONException {
105     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
106     sb.append("[");
107     for (int i = 0, c = size(); i < c; i++) {
108       JSONValue value = get(i);
109       sb.append(value.toString());
110
111       if (i < c - 1) {
112         sb.append(",");
113       }
114     }
115     sb.append("]");
116     return sb.toString();
117   }
118
119   private native JavaScriptObject createArray() /*-{
120     return [];
121   }-*/
;
122
123   private native JavaScriptObject rawGet(int index) /*-{
124     var x = this.@com.google.gwt.json.client.JSONArray::javascriptArray[index];
125     if (typeof x == 'number' || typeof x == 'string' || typeof x == 'array' || typeof x == 'boolean') {
126       x = (Object(x));
127     }
128     return x;
129   }-*/
;
130
131   private native void rawSet(int index, JavaScriptObject jsObject) /*-{
132     this.@com.google.gwt.json.client.JSONArray::javascriptArray[index] = jsObject;
133   }-*/
;
134
135   private native boolean rawTest(int index) /*-{
136     var x = this.@com.google.gwt.json.client.JSONArray::javascriptArray[index];
137     return x !== undefined;
138   }-*/
;
139
140   private native JSONValue wrappedGet(int index) /*-{
141     return this.@com.google.gwt.json.client.JSONArray::wrappedArray[index];
142   }-*/
;
143
144   private native void wrappedSet(int index, JSONValue jsonValue) /*-{
145     this.@com.google.gwt.json.client.JSONArray::wrappedArray[index] = jsonValue;
146   }-*/
;
147
148   private native boolean wrappedTest(int index) /*-{
149     var x = this.@com.google.gwt.json.client.JSONArray::wrappedArray[index];
150     return x !== undefined;
151   }-*/
;
152 }
153
Popular Tags