001 
002 /*
003  *  Descripter 1.0 - Java Script Engines
004  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
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.descripter.js.api.global;
021 
022 import org.descripter.js.api.Core;
023 import org.descripter.js.api.Function;
024 import org.descripter.js.api.Functor;
025 import org.descripter.js.api.Script;
026 import org.descripter.js.api.core.CObject;
027 import org.descripter.js.api.core.CRegExp;
028 import org.descripter.js.api.core.CString;
029 
030 /**
031  * <p>Emulates the global String function in JavaScript.</p>
032  * 
033  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.descripter.org" target="_blank">http://www.descripter.org</a>
034  * @since Descripter 1.0
035  */
036 public class String_ extends Function<Core>
037 {
038     /**
039      * <p>Creates the singleton of the global {@link Function} object of this type.</p>
040      * @param core The {@link Core} global script context
041      * @since Descripter 1.0
042      */
043     public static final void create(Core core) {
044         if (core.get(core._String) == null) {
045             core.put(core._String, new String_(core));
046         }
047     }
048 
049     private String_(Core core) {
050         super(core);
051         CObject prototype = prototype();
052         prototype.put(core._charAt, new Function<Core>(core) {
053             @Override
054             protected Functor<Core> functor() {
055                 return new Functor<Core>(this) {
056                     @Override
057                     public Object function() {
058                         return ((CString)this_()).charAt(arguments().get(0));
059                     }
060                 };
061             }
062         });
063         prototype.put(core._charCodeAt, new Function<Core>(core) {
064             @Override
065             protected Functor<Core> functor() {
066                 return new Functor<Core>(this) {
067                     @Override
068                     public Object function() {
069                         return ((CString)this_()).charCodeAt(arguments().get(0));
070                     }
071                 };
072             }
073         });
074         prototype.put(core._concat, new Function<Core>(core) {
075             @Override
076             protected Functor<Core> functor() {
077                 return new Functor<Core>(this) {
078                     @Override
079                     public Object function() {
080                         return ((CString)this_()).concat(arguments());
081                     }
082                 };
083             }
084         });
085         prototype.put(core._indexOf, new Function<Core>(core) {
086             @Override
087             protected Functor<Core> functor() {
088                 return new Functor<Core>(this) {
089                     @Override
090                     public Object function() {
091                         return ((CString)this_()).indexOf(arguments());
092                     }
093                 };
094             }
095         });
096         prototype.put(core._lastIndexOf, new Function<Core>(core) {
097             @Override
098             protected Functor<Core> functor() {
099                 return new Functor<Core>(this) {
100                     @Override
101                     public Object function() {
102                         return ((CString)this_()).lastIndexOf(arguments());
103                     }
104                 };
105             }
106         });
107         prototype.put(core._localeCompare, new Function<Core>(core) {
108             @Override
109             protected Functor<Core> functor() {
110                 return new Functor<Core>(this) {
111                     @Override
112                     public Object function() {
113                         return ((CString)this_()).localeCompare(arguments().get(0));
114                     }
115                 };
116             }
117         });
118         prototype.put(core._match, new Function<Core>(core) {
119             @Override
120             protected Functor<Core> functor() {
121                 return new Functor<Core>(this) {
122                     @Override
123                     public Object function() {
124                         return ((CString)this_()).match((CRegExp)arguments().get(0));
125                     }
126                 };
127             }
128         });
129         prototype.put(core._replace, new Function<Core>(core) {
130             @Override
131             protected Functor<Core> functor() {
132                 return new Functor<Core>(this) {
133                     @Override
134                     public Object function() {
135                         return ((CString)this_()).replace(arguments());
136                     }
137                 };
138             }
139         });
140         prototype.put(core._search, new Function<Core>(core) {
141             @Override
142             protected Functor<Core> functor() {
143                 return new Functor<Core>(this) {
144                     @Override
145                     public Object function() {
146                         return ((CString)this_()).search((CRegExp)arguments().get(0));
147                     }
148                 };
149             }
150         });
151         prototype.put(core._slice, new Function<Core>(core) {
152             @Override
153             protected Functor<Core> functor() {
154                 return new Functor<Core>(this) {
155                     @Override
156                     public Object function() {
157                         return ((CString)this_()).slice(arguments());
158                     }
159                 };
160             }
161         });
162         prototype.put(core._split, new Function<Core>(core) {
163             @Override
164             protected Functor<Core> functor() {
165                 return new Functor<Core>(this) {
166                     @Override
167                     public Object function() {
168                         return ((CString)this_()).split(arguments());
169                     }
170                 };
171             }
172         });
173         prototype.put(core._substr, new Function<Core>(core) {
174             @Override
175             protected Functor<Core> functor() {
176                 return new Functor<Core>(this) {
177                     @Override
178                     public Object function() {
179                         return ((CString)this_()).substr(arguments());
180                     }
181                 };
182             }
183         });
184         prototype.put(core._substring, new Function<Core>(core) {
185             @Override
186             protected Functor<Core> functor() {
187                 return new Functor<Core>(this) {
188                     @Override
189                     public Object function() {
190                         return ((CString)this_()).substring(arguments());
191                     }
192                 };
193             }
194         });
195         prototype.put(core._toLocaleLowerCase, new Function<Core>(core) {
196             @Override
197             protected Functor<Core> functor() {
198                 return new Functor<Core>(this) {
199                     @Override
200                     public Object function() {
201                         return ((CString)this_()).toLocaleLowerCase();
202                     }
203                 };
204             }
205         });
206         prototype.put(core._toLocaleUpperCase, new Function<Core>(core) {
207             @Override
208             protected Functor<Core> functor() {
209                 return new Functor<Core>(this) {
210                     @Override
211                     public Object function() {
212                         return ((CString)this_()).toLocaleUpperCase();
213                     }
214                 };
215             }
216         });
217         prototype.put(core._toLowerCase, new Function<Core>(core) {
218             @Override
219             protected Functor<Core> functor() {
220                 return new Functor<Core>(this) {
221                     @Override
222                     public Object function() {
223                         return ((CString)this_()).toLowerCase();
224                     }
225                 };
226             }
227         });
228         prototype.put(core._toUpperCase, new Function<Core>(core) {
229             @Override
230             protected Functor<Core> functor() {
231                 return new Functor<Core>(this) {
232                     @Override
233                     public Object function() {
234                         return ((CString)this_()).toUpperCase();
235                     }
236                 };
237             }
238         });
239     }
240 
241     /**
242      * <p>Allocates a new object with this {@link Function}.</p>
243      * @param script The script context that invoked this service
244      * @param args An array of the arguments passed by the invocation
245      * @return The newly allocated object
246      * @since Descripter 1.0
247      */
248     @Override
249     public <S extends Script<?>> CString alloc(S script, Object ...args) {
250         return new CString(this, args != null && args.length > 0 ? args[0] : null);
251     }
252 
253     /**
254      * <p>Gets the {@link Functor} of the {@link Function} object.</p>
255      * <p>This is a concrete implementation of the super abstract method.</p>
256      * @return The {@link Functor} of the {@link Function} object
257      * @since Descripter 1.0
258      */
259     @Override
260     protected Functor<Core> functor() {
261         return new Functor<Core>(this) {
262             @Override
263             public Object function() {
264                 return new CString(String_.this, arguments().get(0));
265             }
266         };
267     }
268 }