001 
002 /*
003  *  JScripter Simulation 1.0 - For Java To Script
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.sim;
021 
022 import org.jscripter.sim.core.Core;
023 
024 import js.ArrayLike;
025 import js.Vars;
026 import js.core.JsArray;
027 import js.core.JsFunction;
028 import js.core.JsGlobal;
029 
030 class SimArrayLike<T> implements ArrayLike<T>
031 {
032     private ArrayLike<T> arr;
033 
034     public SimArrayLike(ArrayLike<T> arr) {
035         this.arr = arr;
036     }
037 
038     public SimArrayLike() {
039         this(new SimArrayList<T>());
040     }
041 
042     //@Override
043     public final synchronized ArrayLike<T> concat(Vars<?> args) {
044         try {
045             arr = arr.concat(args);
046             return this;
047         } catch (UnsupportedOperationException uoe) {
048             arr = new SimArrayList<T>(arr);
049             arr = arr.concat(args);
050             return this;
051         }
052     }
053 
054     //@Override
055     public final synchronized ArrayLike<T> concat(Object arg) {
056         try {
057             arr = arr.concat(arg);
058             return this;
059         } catch (UnsupportedOperationException uoe) {
060             arr = new SimArrayList<T>(arr);
061             arr = arr.concat(arg);
062             return this;
063         }
064     }
065 
066     //@Override
067     public final synchronized boolean delete(int i) {
068         try {
069             return arr.delete(i);
070         } catch (UnsupportedOperationException uoe) {
071             arr = new SimArray<T>(arr);
072             return arr.delete(i);
073         }
074     }
075 
076     //@Override
077     public final synchronized T get(int i) {
078         return arr.get(i);
079     }
080 
081     //@Override
082     public final synchronized String join() {
083         return arr.join();
084     }
085 
086     //@Override
087     public final synchronized String join(Object separator) {
088         return arr.join(separator);
089     }
090 
091     //@Override
092     public final synchronized int length() {
093         return arr.length();
094     }
095 
096     //@Override
097     public final synchronized int length(int len) {
098         try {
099             return arr.length(len);
100         } catch (UnsupportedOperationException uoe) {
101             arr = new SimArray<T>(arr);
102             return arr.length(len);
103         }
104     }
105 
106     //@Override
107     public Integer index() {
108         return null;
109     }
110 
111     //@Override
112     public String input() {
113         return null;
114     }
115 
116     //@Override
117     public final synchronized T pop() {
118         try {
119             return arr.pop();
120         } catch (UnsupportedOperationException uoe) {
121             arr = new SimArrayList<T>(arr);
122             return arr.pop();
123         }
124     }
125 
126     //@Override
127     public final synchronized int push(Object v) {
128         try {
129             return arr.push(v);
130         } catch (UnsupportedOperationException uoe) {
131             arr = new SimArrayList<T>(arr);
132             return arr.push(v);
133         }
134     }
135 
136     //@Override
137     public final synchronized int push(Vars<?> args) {
138         try {
139             return arr.push(args);
140         } catch (UnsupportedOperationException uoe) {
141             arr = new SimArrayList<T>(arr);
142             return arr.push(args);
143         }
144     }
145 
146     //@Override
147     public final synchronized void reverse() {
148         arr.reverse();
149     }
150 
151     //@Override
152     public final synchronized T set(int i, T v) {
153         try {
154             return arr.set(i, v);
155         } catch (UnsupportedOperationException uoe) {
156             arr = new SimArray<T>(arr);
157             return arr.set(i, v);
158         }
159     }
160 
161     //@Override
162     public final synchronized T shift() {
163         try {
164             return arr.shift();
165         } catch (UnsupportedOperationException uoe) {
166             arr = new SimArray<T>(arr);
167             return arr.shift();
168         }
169     }
170 
171     //@Override
172     public final synchronized ArrayLike<T> slice(Vars<?> args) {
173         return new SimArrayLike<T>(arr.slice(args));
174     }
175 
176     //@Override
177     public final synchronized ArrayLike<T> slice(Object start) {
178         return new SimArrayLike<T>(arr.slice(start));
179     }
180 
181     //@Override
182     public final synchronized ArrayLike<T> slice(Object start, Object end) {
183         return new SimArrayLike<T>(arr.slice(start, end));
184     }
185 
186     //@Override
187     public final synchronized ArrayLike<T> sort() {
188         try {
189             arr = arr.sort();
190             return this;
191         } catch (UnsupportedOperationException uoe) {
192             arr = new SimArrayList<T>(arr).sort();
193             return this;
194         }
195     }
196 
197     //@Override
198     public final synchronized ArrayLike<T> sort(JsFunction<? extends Number> orderfunc) {
199         try {
200             arr = arr.sort(orderfunc);
201             return this;
202         } catch (UnsupportedOperationException uoe) {
203             arr = new SimArrayList<T>(arr).sort(orderfunc);
204             return this;
205         }
206     }
207 
208     //@Override
209     public final synchronized ArrayLike<T> splice(Vars<?> args) {
210         try {
211             return new SimArrayLike<T>(arr.splice(args));
212         } catch (UnsupportedOperationException uoe) {
213             arr = new SimArray<T>(arr);
214             return new SimArrayLike<T>(arr.splice(args));
215         }
216     }
217 
218     //@Override
219     public final synchronized ArrayLike<T> splice(Object start) {
220         try {
221             return new SimArrayLike<T>(arr.splice(start));
222         } catch (UnsupportedOperationException uoe) {
223             arr = new SimArray<T>(arr);
224             return new SimArrayLike<T>(arr.splice(start));
225         }
226     }
227 
228     //@Override
229     public final synchronized ArrayLike<T> splice(Object start, Object deleteCount) {
230         try {
231             return new SimArrayLike<T>(arr.splice(start, deleteCount));
232         } catch (UnsupportedOperationException uoe) {
233             arr = new SimArray<T>(arr);
234             return new SimArrayLike<T>(arr.splice(start, deleteCount));
235         }
236     }
237 
238     //@Override
239     public final synchronized ArrayLike<T> splice(Object start, Object deleteCount, Object value) {
240         try {
241             return new SimArrayLike<T>(arr.splice(start, deleteCount, value));
242         } catch (UnsupportedOperationException uoe) {
243             arr = new SimArray<T>(arr);
244             return new SimArrayLike<T>(arr.splice(start, deleteCount, value));
245         }
246     }
247 
248     //@Override
249     public final synchronized ArrayLike<T> splice(Object start, Object deleteCount, Vars<?> args) {
250         try {
251             return new SimArrayLike<T>(arr.splice(start, deleteCount, args));
252         } catch (UnsupportedOperationException uoe) {
253             arr = new SimArray<T>(arr);
254             return new SimArrayLike<T>(arr.splice(start, deleteCount, args));
255         }
256     }
257 
258     //@Override
259     public final synchronized int unshift(Vars<?> args) {
260         try {
261             return arr.unshift(args);
262         } catch (UnsupportedOperationException uoe) {
263             arr = new SimArray<T>(arr);
264             return arr.unshift(args);
265         }
266     }
267 
268     //@Override
269     public final synchronized int unshift(Object value) {
270         try {
271             return arr.unshift(value);
272         } catch (UnsupportedOperationException uoe) {
273             arr = new SimArray<T>(arr);
274             return arr.unshift(value);
275         }
276     }
277 
278     //@Override
279     public final String toString() {
280         return arr.toString();
281     }
282 
283     //@Override
284     public final synchronized ArrayLike<T> valueOf() {
285         return this;
286     }
287 
288     //@Override
289     public final synchronized JsArray var() {
290         if (Core.isRunning()) {
291             return JsGlobal.Array.with().create(this);
292         } else {
293             JsArray arr = JsGlobal.Array.with().create();
294             for (int i = 0, len = length(); i < len; i++) {
295                 arr.push(get(i));
296             }
297             return arr;
298         }
299     }
300 }