KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > lang > Array


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.lang;
17
18 // CHECKSTYLE_NAMING_OFF: Follows legacy naming pattern. Fix me.
19

20 /**
21  * This is a magic class the compiler uses as a base class for injected array
22  * classes.
23  */

24 public final class Array {
25
26   /**
27    * Creates an array like "new T[a][b][c][][]" by passing in javascript objects
28    * as follows: [a, b, c].
29    */

30   public static Array initDims(String JavaDoc typeName, Object JavaDoc typeIdExprs,
31       Object JavaDoc queryIdExprs, Object JavaDoc dimExprs, Object JavaDoc defaultValue) {
32     // ASSERT: dimExprs.length > 0 or else code gen is broken
33
//
34
return initDims(typeName, typeIdExprs, queryIdExprs, dimExprs, 0,
35         getValueCount(dimExprs), defaultValue);
36   }
37
38   /**
39    * Creates an array like "new T[][]{a,b,c,d}" by passing in javascript objects
40    * as follows: [a,b,c,d].
41    */

42   public static final Array initValues(String JavaDoc typeName, int typeId,
43       int queryId, Object JavaDoc values) {
44     int length = getValueCount(values);
45     Array result = new Array(length, typeId, queryId, typeName);
46     for (int i = 0; i < length; ++i) {
47       _set(result, i, getValue(values, i));
48     }
49     return result;
50   }
51
52   /**
53    * Performs an array assignment, checking for valid index and type.
54    */

55   public static Object JavaDoc setCheck(Array array, int index, Object JavaDoc value) {
56     if (value != null && array.queryId != 0
57         && !Cast.instanceOf(value, array.queryId)) {
58       throw new ArrayStoreException JavaDoc();
59     }
60     return _set(array, index, value);
61   }
62
63   /**
64    * Sets a value in the array.
65    */

66   private static native Object JavaDoc _set(Array array, int index, Object JavaDoc value) /*-{
67     return array[index] = value;
68   }-*/
;
69
70   /**
71    * Gets an the first value from a JSON int array.
72    */

73   private static native int getIntValue(Object JavaDoc values, int index) /*-{
74     return values[index];
75   }-*/
;
76
77   /**
78    * Gets a value from a JSON array.
79    */

80   private static native Object JavaDoc getValue(Object JavaDoc values, int index) /*-{
81     return values[index];
82   }-*/
;
83
84   /**
85    * Gets the length of a JSON array.
86    */

87   private static native int getValueCount(Object JavaDoc values) /*-{
88     return values.length;
89   }-*/
;
90
91   /**
92    * Creates an array like "new T[a][b][c][][]" by passing in javascript objects
93    * as follows: [a,b,c].
94    */

95   private static Array initDims(String JavaDoc typeName, Object JavaDoc typeIdExprs,
96       Object JavaDoc queryIdExprs, Object JavaDoc dimExprs, int index, int count,
97       Object JavaDoc defaultValue) {
98     int length;
99     if ((length = getIntValue(dimExprs, index)) < 0) {
100       throw new NegativeArraySizeException JavaDoc();
101     }
102
103     Array result = new Array(length, getIntValue(typeIdExprs, index),
104         getIntValue(queryIdExprs, index), typeName);
105
106     ++index;
107     if (index < count) {
108       typeName = typeName.substring(1);
109       for (int i = 0; i < length; ++i) {
110         _set(result, i, initDims(typeName, typeIdExprs, queryIdExprs, dimExprs,
111             index, count, defaultValue));
112       }
113     } else {
114       for (int i = 0; i < length; ++i) {
115         _set(result, i, defaultValue);
116       }
117     }
118
119     return result;
120   }
121
122   public final int length;
123
124   protected final int queryId;
125
126   public Array(int length, int typeId, int queryId, String JavaDoc typeName) {
127     this.length = length;
128     this.queryId = queryId;
129
130     /*
131      * These are inherited protected fields from GWT's Object emulation class.
132      */

133     this.typeName = typeName;
134     this.typeId = typeId;
135   }
136 }
137
138 // CHECKSTYLE_NAMING_ON
139
Popular Tags