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 java.util.ArrayList;
023 import java.util.Collections;
024 import java.util.Comparator;
025 
026 import js.ArrayLike;
027 import js.Js;
028 import js.Vars;
029 import js.core.JsArray;
030 import js.core.JsFunction;
031 
032 final class SimArrayList<T> extends ArrayList<T> implements ArrayLike<T>
033 {
034     private static final long serialVersionUID = -7941589999600923231L;
035 
036     public SimArrayList() {}
037 
038     public SimArrayList(ArrayLike<T> a) {
039         synchronized(a) {
040             for (int i = 0, len = a.length(); i < len; i++) {
041                 push(a.get(i));
042             }
043         }
044     }
045 
046     //@Override
047     public final synchronized ArrayLike<T> concat(Vars<?> args) {
048         return SimArrays.concat(new SimArrayList<T>(), this, args);
049     }
050 
051     //@Override
052     public final synchronized ArrayLike<T> concat(Object arg) {
053         return SimArrays.concat(new SimArrayList<T>(), this, arg);
054     }
055 
056     //@Override
057     public final synchronized boolean delete(int i) {
058         if (Js.be(get(i))) {
059             if (i == size() - 1) {
060                 remove(i);
061             } else {
062                 super.set(i, null);
063             }
064             return true;
065         }
066         return false;
067     }
068 
069     //@Override
070     public final synchronized T get(int i) {
071         return i < size() ? super.get(i) : null;
072     }
073 
074     //@Override
075     public final synchronized String join() {
076         return SimArrays.join(this);
077     }
078 
079     //@Override
080     public final synchronized String join(Object separator) {
081         return SimArrays.join(this, separator);
082     }
083 
084     //@Override
085     public final synchronized int length() {
086         return size();
087     }
088 
089     //@Override
090     public final synchronized int length(int len) {
091         throw new UnsupportedOperationException();
092     }
093 
094     //@Override
095     public final Integer index() {
096         return null;
097     }
098 
099     //@Override
100     public final String input() {
101         return null;
102     }
103 
104     //@Override
105     public final synchronized T pop() {
106         return isEmpty() ? null : remove(size() - 1);
107     }
108 
109     @SuppressWarnings("unchecked")
110     //@Override
111     public final synchronized int push(Object v) {
112         add((T)v);
113         return size();
114     }
115 
116     @SuppressWarnings("unchecked")
117     //@Override
118     public final synchronized int push(Vars<?> args) {
119         ArrayLike<?> a = args.var();
120         for (int i = 0, len = a.length(); i < len; i++) {
121             add((T)a.get(i));
122         }
123         return size();
124     }
125 
126     //@Override
127     public final synchronized void reverse() {
128         SimArrays.reverse(this);
129     }
130 
131     //@Override
132     public final synchronized T set(int i, T v) {
133         if (i >= size()) {
134             throw new UnsupportedOperationException();
135         }
136         super.set(i, v);
137         return v;
138     }
139 
140     //@Override
141     public final synchronized T shift() {
142         throw new UnsupportedOperationException();
143     }
144 
145     //@Override
146     public final synchronized ArrayLike<T> slice(Vars<?> args) {
147         return SimArrays.slice(this, args);
148     }
149 
150     //@Override
151     public final synchronized ArrayLike<T> slice(Object start) {
152         return SimArrays.slice(this, start);
153     }
154 
155     //@Override
156     public final synchronized ArrayLike<T> slice(Object start, Object end) {
157         return SimArrays.slice(this, start, end);
158     }
159 
160     //@Override
161     public final synchronized ArrayLike<T> sort() {
162         Collections.sort(this, new Comparator<T>() {
163             //@Override
164             public int compare(T arg0, T arg1) {
165                 double a = SimUtil.getNumber(arg0).doubleValue(),
166                        b = SimUtil.getNumber(arg1).doubleValue();
167             return a > b ? 1 : a < b ? -1 : 0;
168             }
169         });
170         return this;
171     }
172 
173     //@Override
174     public final synchronized ArrayLike<T> sort(final JsFunction<? extends Number> orderfunc) {
175         Collections.sort(this, new Comparator<T>() {
176             //@Override
177             public int compare(T arg0, T arg1) {
178                 return orderfunc.invoke(new Vars<T>().add(arg0).add(arg1)).intValue();
179             }
180         });
181         return this;
182     }
183 
184     //@Override
185     public final synchronized ArrayLike<T> splice(Vars<?> args) {
186         throw new UnsupportedOperationException();
187     }
188 
189     //@Override
190     public final synchronized ArrayLike<T> splice(Object start) {
191         throw new UnsupportedOperationException();
192     }
193 
194     //@Override
195     public final synchronized ArrayLike<T> splice(Object start, Object deleteCount) {
196         throw new UnsupportedOperationException();
197     }
198 
199     //@Override
200     public final synchronized ArrayLike<T> splice(Object start, Object deleteCount, Object value) {
201         throw new UnsupportedOperationException();
202     }
203 
204     //@Override
205     public final synchronized ArrayLike<T> splice(Object start, Object deleteCount, Vars<?> args) {
206         throw new UnsupportedOperationException();
207     }
208 
209     //@Override
210     public final synchronized int unshift(Vars<?> args) {
211         throw new UnsupportedOperationException();
212     }
213 
214     //@Override
215     public final synchronized int unshift(Object value) {
216         throw new UnsupportedOperationException();
217     }
218 
219     //@Override
220     public final String toString() {
221         return SimArrays.toString(this);
222     }
223 
224     //@Override
225     public final synchronized ArrayLike<T> valueOf() {
226         return this;
227     }
228 
229     //@Override
230     public final JsArray var() {
231         throw new UnsupportedOperationException();
232     }
233 }