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