Source Code Cross Referenced for AnyList.java in  » Web-Framework » anvil » anvil » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » anvil » anvil.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: AnyList.java,v 1.45 2002/09/16 08:05:02 jkl Exp $
003:         *
004:         * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005:         *
006:         * Use is subject to license terms, as defined in
007:         * Anvil Sofware License, Version 1.1. See LICENSE 
008:         * file, or http://njet.org/license-1.1.txt
009:         */
010:        package anvil.core;
011:
012:        import java.io.IOException;
013:        import java.io.OutputStream;
014:        import java.io.Writer;
015:        import java.util.ArrayList;
016:        import anvil.core.runtime.AnyFunction;
017:        import anvil.script.Context;
018:        import anvil.script.Function;
019:        import anvil.java.util.BindingEnumeration;
020:        import java.util.Comparator;
021:
022:        /// @class list
023:        /// List is a mutable sequence of values.
024:
025:        /**
026:         * class AnyList
027:         *
028:         * @author: Jani Lehtimäki
029:         */
030:        public class AnyList extends AnySequence {
031:
032:            /// @constructor list
033:            /// Constructs list. Same as builtin <code>\\{elements, ...}</code> constructor.
034:            /// @synopsis list(object element, ...)
035:            public static Any newInstance(Any[] list) {
036:                return new AnyList(list);
037:            }
038:
039:            transient public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
040:                    "list", AnyList.class,
041:                    AnySequence.__class__,
042:                    //DOC{{
043:                    ""
044:                            + " @class list\n"
045:                            + " List is a mutable sequence of values.\n"
046:                            + " @constructor list\n"
047:                            + " Constructs list. Same as builtin <code>\\{elements, ...}</code> constructor.\n"
048:                            + " @synopsis list(object element, ...)\n"
049:            //}}DOC
050:            );
051:
052:            protected Any[] _array;
053:            protected int _size;
054:
055:            public AnyList() {
056:                _array = ARRAY0;
057:                _size = 0;
058:            }
059:
060:            public AnyList(Any[] array) {
061:                _array = array;
062:                _size = array.length;
063:            }
064:
065:            public AnyList(Any[] array, int size) {
066:                _array = array;
067:                _size = size;
068:            }
069:
070:            public boolean isCompatible(Any element) {
071:                int t = element.typeOf();
072:                return (t == IS_TUPLE) || (t == IS_LIST);
073:            }
074:
075:            public int getSize() {
076:                return _size;
077:            }
078:
079:            public AnySequence setSize(int size) {
080:                if (size < _size) {
081:                    java.util.Arrays.fill(_array, size, _size, null);
082:                }
083:                _size = size;
084:                return this ;
085:            }
086:
087:            public AnySequence clear() {
088:                java.util.Arrays.fill(_array, 0, _size, null);
089:                _size = 0;
090:                return this ;
091:            }
092:
093:            public void ensureCapacity(int size) {
094:                if (_array.length < size) {
095:                    Any[] array = new Any[size + (size / 8)];
096:                    System.arraycopy(_array, 0, array, 0, _size);
097:                    _array = array;
098:                }
099:            }
100:
101:            public Any getElement(int index) {
102:                return Any.create(_array[index]);
103:            }
104:
105:            public AnySequence setElement(int index, Any element) {
106:                _array[index] = element;
107:                return this ;
108:            }
109:
110:            public AnySequence crop(int start, int length) {
111:                System.arraycopy(_array, start, _array, 0, length);
112:                setSize(length);
113:                return this ;
114:            }
115:
116:            public AnySequence getSlice(int start, int length) {
117:                Any[] slice = new Any[length];
118:                System.arraycopy(_array, start, slice, 0, length);
119:                return new AnyList(slice);
120:            }
121:
122:            public AnySequence deleteSlice(int start, int length) {
123:                Any[] array = _array;
124:                int size = _size;
125:                if (start + length < size) {
126:                    System.arraycopy(array, start + length, array, start, size
127:                            - (start + length));
128:                }
129:                setSize(size - length);
130:                return this ;
131:            }
132:
133:            public AnySequence setSlice(int start, int length, Any element) {
134:                int size = _size;
135:                ensureCapacity(size - length + 1);
136:                Any[] array = _array;
137:                if (start + length < size) {
138:                    System.arraycopy(array, start + length, array, start + 1,
139:                            size - (start + length));
140:                }
141:                array[start] = element;
142:                setSize(size + 1 - length);
143:                return this ;
144:            }
145:
146:            public AnySequence setSlice(int start, int length,
147:                    AnySequence sequence) {
148:                Any[] seq = sequence.toList();
149:                int seq_length = sequence.getSize();
150:                int size = _size;
151:                ensureCapacity(size - length + seq_length);
152:                Any[] array = _array;
153:                if (start + length < size) {
154:                    System.arraycopy(array, start + length, array, start
155:                            + seq_length, size - (start + length));
156:                }
157:                System.arraycopy(seq, 0, array, start, seq_length);
158:                setSize(size + seq_length - length);
159:                return this ;
160:            }
161:
162:            public AnySequence append(AnySequence sequence) {
163:                Any[] seq = sequence.toList();
164:                int seq_length = sequence.getSize();
165:                int size = _size;
166:                ensureCapacity(size + seq_length);
167:                Any[] array = _array;
168:                System.arraycopy(seq, 0, array, size, seq_length);
169:                setSize(size + seq_length);
170:                return this ;
171:            }
172:
173:            public AnySequence append(Any element) {
174:                int size = _size;
175:                ensureCapacity(size + 1);
176:                _array[size] = element;
177:                setSize(size + 1);
178:                return this ;
179:            }
180:
181:            public AnySequence createSequence(Any element) {
182:                return new AnyList(new Any[] { element, null, null, null }, 1);
183:            }
184:
185:            public AnySequence createEmptySequence() {
186:                return new AnyList(ARRAY0);
187:            }
188:
189:            public int compareAt(AnySequence sequence, int start, int length) {
190:                Any[] haystack = _array;
191:                Any[] needle = sequence.toList();
192:                int delta;
193:                for (int i = 0; i < length; i++) {
194:                    delta = haystack[start + i].compareTo(needle[i]);
195:                    if (delta != 0) {
196:                        return (delta < 0) ? -1 : ((delta > 0) ? 1 : 0);
197:                    }
198:                }
199:                return 0;
200:            }
201:
202:            public int compareAt(Any element, int start) {
203:                int delta = _array[start].compareTo(element);
204:                return (delta < 0) ? -1 : ((delta > 0) ? 1 : 0);
205:            }
206:
207:            public AnySequence fill(Any fill, int start, int length) {
208:                java.util.Arrays.fill(_array, start, start + length, fill);
209:                return this ;
210:            }
211:
212:            public AnySequence sort(int start, int length, Comparator comparator) {
213:                if (comparator != null) {
214:                    java.util.Arrays.sort(_array, start, start + length,
215:                            comparator);
216:                } else {
217:                    java.util.Arrays.sort(_array, start, start + length);
218:                }
219:                return this ;
220:            }
221:
222:            public int search(Any element, Comparator comparator) {
223:                if (comparator != null) {
224:                    return java.util.Arrays.binarySearch(_array, element,
225:                            comparator);
226:                } else {
227:                    return java.util.Arrays.binarySearch(_array, element);
228:                }
229:            }
230:
231:            public AnySequence swap(int index1, int index2) {
232:                Any[] array = _array;
233:                Any any = array[index1];
234:                array[index1] = array[index2];
235:                array[index2] = any;
236:                return this ;
237:            }
238:
239:            public anvil.script.ClassType classOf() {
240:                return __class__;
241:            }
242:
243:            public int typeOf() {
244:                return IS_LIST;
245:            }
246:
247:            public boolean isList() {
248:                return true;
249:            }
250:
251:            public Any[] toTuple() {
252:                return _array;
253:            }
254:
255:            public Any[] toList() {
256:                return _array;
257:            }
258:
259:            public Object toObject() {
260:                return _array;
261:            }
262:
263:            public String toString() {
264:                StringBuffer buffer = new StringBuffer();
265:                buffer.append('{');
266:                int n = _size;
267:                for (int i = 0; i < n; i++) {
268:                    if (i > 0) {
269:                        buffer.append(',');
270:                        buffer.append(' ');
271:                    }
272:                    buffer.append(_array[i].toString());
273:                }
274:                buffer.append('}');
275:                return buffer.toString();
276:            }
277:
278:            public Writer toAnvil(Writer writer) throws IOException {
279:                Any[] array = _array;
280:                final int n = _size;
281:                writer.write('{');
282:                for (int i = 0; i < n; i++) {
283:                    if (i > 0) {
284:                        writer.write(',');
285:                        writer.write(' ');
286:                    }
287:                    array[i].toAnvil(writer);
288:                }
289:                writer.write('}');
290:                return writer;
291:            }
292:
293:            public Writer toJava(Writer writer) throws IOException {
294:                Any[] array = _array;
295:                final int n = _size;
296:                writer.write("new anvil.core.AnyList(new Any[]{");
297:                for (int i = 0; i < n; i++) {
298:                    if (i > 0) {
299:                        writer.write(',');
300:                        writer.write(' ');
301:                    }
302:                    array[i].toJava(writer);
303:                }
304:                writer.write('}');
305:                writer.write(')');
306:                return writer;
307:            }
308:
309:            public anvil.codec.Code toCode(anvil.codec.Code code) {
310:                anvil.codec.ConstantPool pool = code.getPool();
311:                int clazz = pool.addClass("anvil/core/AnyList");
312:                code.anew(clazz);
313:                code.dup();
314:                Any[] array = _array;
315:                final int n = _size;
316:                code.iconst(n);
317:                code.anewarray(pool.addClass("anvil/core/Any"));
318:                for (int i = 0; i < n; i++) {
319:                    code.dup();
320:                    code.iconst(i);
321:                    _array[i].toCode(code);
322:                    code.aastore();
323:                }
324:                code.invokespecial(pool.addMethodRef(clazz, "<init>",
325:                        "([Lanvil/core/Any;)V"));
326:                return code;
327:            }
328:
329:            public Object clone() {
330:                final int length = _size;
331:                Any[] array = new Any[length];
332:                System.arraycopy(_array, 0, array, 0, length);
333:                return new AnyList(array);
334:            }
335:
336:            public Any copy() {
337:                final int length = _size;
338:                if (length == 0) {
339:                    return new AnyList(_array);
340:                }
341:                Any[] array = new Any[length];
342:                for (int i = 0; i < length; i++) {
343:                    array[i] = _array[i].copy();
344:                }
345:                return new AnyList(array);
346:            }
347:
348:            public void serialize(Serializer serializer) throws IOException {
349:                if (serializer.register(this )) {
350:                    return;
351:                }
352:                Any[] array = _array;
353:                final int n = _size;
354:                serializer.write('L');
355:                serializer.write(n);
356:                serializer.write(':');
357:                for (int i = 0; i < n; i++) {
358:                    _array[i].serialize(serializer);
359:                }
360:            }
361:
362:            public static AnyList unserialize(Unserializer unserializer)
363:                    throws UnserializationException {
364:                int length = (int) unserializer.getLong();
365:                if (length < 0) {
366:                    throw new UnserializationException();
367:                }
368:                AnyList list = new AnyList();
369:                unserializer.register(list);
370:                if (length > 0) {
371:                    Any[] array = new Any[length];
372:                    for (int i = 0; i < length; i++) {
373:                        array[i] = unserializer.unserialize();
374:                    }
375:                    list._array = array;
376:                    list._size = length;
377:                }
378:                return list;
379:            }
380:
381:            public Any execute(Context context, Any[] parameters) {
382:                Any rv = Any.UNDEFINED;
383:                final int n = _size;
384:                for (int i = 0; i < n; i++) {
385:                    rv = _array[i].execute(context, parameters);
386:                }
387:                return rv;
388:            }
389:
390:            public Any execute(Context context) {
391:                Any rv = Any.UNDEFINED;
392:                final int n = _size;
393:                for (int i = 0; i < n; i++) {
394:                    rv = _array[i].execute(context);
395:                }
396:                return rv;
397:            }
398:
399:            public Any execute(Context context, Any param1) {
400:                Any rv = Any.UNDEFINED;
401:                final int n = _size;
402:                for (int i = 0; i < n; i++) {
403:                    rv = _array[i].execute(context, param1);
404:                }
405:                return rv;
406:            }
407:
408:            public Any execute(Context context, Any param1, Any param2) {
409:                Any rv = Any.UNDEFINED;
410:                final int n = _size;
411:                for (int i = 0; i < n; i++) {
412:                    rv = _array[i].execute(context, param1, param2);
413:                }
414:                return rv;
415:            }
416:
417:            public Any execute(Context context, Any param1, Any param2,
418:                    Any param3) {
419:                Any rv = Any.UNDEFINED;
420:                final int n = _size;
421:                for (int i = 0; i < n; i++) {
422:                    rv = _array[i].execute(context, param1, param2, param3);
423:                }
424:                return rv;
425:            }
426:
427:            public Any execute(Context context, Any param1, Any param2,
428:                    Any param3, Any param4) {
429:                Any rv = Any.UNDEFINED;
430:                final int n = _size;
431:                for (int i = 0; i < n; i++) {
432:                    rv = _array[i].execute(context, param1, param2, param3,
433:                            param4);
434:                }
435:                return rv;
436:            }
437:
438:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.