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 js.ArrayLike;
023 import js.ArrayObject;
024 import js.Js;
025 import js.Vars;
026 
027 final class SimArrays
028 {
029     private SimArrays() {}
030 
031     private final static void concat(ArrayLike<?> dst, ArrayObject<?> src) {
032         for (int i = 0, len = src.length(); i < len; i++) {
033             dst.push(src.get(i));
034         }
035     }
036 
037     public final static <T> ArrayLike<T> concat(
038             ArrayLike<T> dst, ArrayLike<T> src, Vars<?> args) {
039         concat(dst, src);
040         ArrayLike<?> aa = args.var();
041         for (int i = 0, len = aa.length(); i < len; i++) {
042             Object arg = aa.get(i);
043             if (arg instanceof ArrayObject<?>) {
044                 concat(dst, (ArrayObject<?>)arg);
045             } else {
046                 dst.push(arg);
047             }
048         }
049         return dst;
050     }
051 
052     public final static <T> ArrayLike<T> concat(
053             ArrayLike<T> dst, ArrayLike<T> src, Object arg) {
054         concat(dst, src);
055         if (arg instanceof ArrayObject<?>) {
056             concat(dst, (ArrayObject<?>)arg);
057         } else {
058             dst.push(arg);
059         }
060         return dst;
061     }
062 
063     public final static String join(ArrayLike<?> arr) {
064         StringBuilder sb = new StringBuilder();
065         for (int i = 0, len = arr.length(); i < len; i++) {
066             if (i > 0) {
067                 sb.append(',');
068             }
069             sb.append(Js.toString(arr.get(i)));
070         }
071         return sb.toString();
072     }
073 
074     public final static String join(ArrayLike<?> arr, Object separator) {
075         if (Js.undefined(separator)) {
076             return join(arr);
077         }
078         StringBuilder sb = new StringBuilder();
079         for (int i = 0, len = arr.length(); i < len; i++) {
080             if (i > 0) {
081                 sb.append(Js.toString(separator));
082             }
083             sb.append(Js.toString(arr.get(i)));
084         }
085         return sb.toString();
086     }
087 
088     public final static <T> void reverse(ArrayLike<T> arr) {
089         for (int i = 0, len = arr.length(); i < len / 2; i++) {
090             T t = arr.get(i);
091             arr.set(i, arr.get(len - i));
092             arr.set(len - i, t);
093         }
094     }
095 
096     public final static <T> ArrayLike<T> slice(ArrayLike<T> arr, Vars<?> args) {
097         ArrayLike<?> a = args.var();
098         if (a.length() < 2) {
099             return slice(arr, a.get(0));
100         } else {
101             return slice(arr, a.get(0), a.get(1));
102         }
103     }
104 
105     public final static <T> ArrayLike<T> slice(ArrayLike<T> arr, Object start) {
106         ArrayLike<T> a = new SimArrayList<T>();
107         int from = SimUtil.getNumber(start).intValue();
108         int len = arr.length();
109         from = from < 0 ? from + len : from;
110         for (int i = from; i < len; i++) {
111             a.push(arr.get(i));
112         }
113         return a;
114     }
115 
116     public final static <T> ArrayLike<T> slice(ArrayLike<T> arr, Object start, Object end) {
117         ArrayLike<T> a = new SimArrayList<T>();
118         int from = SimUtil.getNumber(start).intValue();
119         int to   = SimUtil.getNumber(end  ).intValue();
120         int len = arr.length();
121         from = from < 0 ? from + len : from;
122         to   = to   < 0 ? to   + len : to;
123         for (int i = from; i < len; i++) {
124             a.push(arr.get(i));
125         }
126         return a;
127     }
128 
129     public final static String toString(ArrayLike<?> arr) {
130         StringBuilder sb = new StringBuilder();
131         for (int i = 0, len = arr.length(); i < len; i++) {
132             if (i > 0) {
133                 sb.append(",");
134             }
135             sb.append(Js.toString(arr.get(i)));
136         }
137         return sb.toString();
138     }
139 }