Source Code Cross Referenced for ArrayFactory.java in  » Science » javolution-5.2 » javolution » context » 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 » Science » javolution 5.2 » javolution.context 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003:         * Copyright (C) 2007 - Javolution (http://javolution.org/)
004:         * All rights reserved.
005:         * 
006:         * Permission to use, copy, modify, and distribute this software is
007:         * freely granted, provided that this notice is preserved.
008:         */
009:        package javolution.context;
010:
011:        /**
012:         * <p> This class holds factories to produces arrays of variable length.
013:         *     It allows for object recycling, pre-allocation and {@link StackContext
014:         *     stack} allocations:[code]
015:         *     // Primitive types.
016:         *     char[] buffer = ArrayFactory.CHARS_FACTORY.array(1024); // Possibly recycled.
017:         *     for (int i = reader.read(buffer, 0, buffer.length); i > 0;) {
018:         *         ...
019:         *     }
020:         *     ArrayFactory.CHARS_FACTORY.recycle(buffer); //  
021:         *
022:         *     // Custom types.
023:         *     static ArrayFactory<Vertex[]> VERTICES_FACTORY = new ArrayFactory<Vertex[]> {
024:         *         protected Vertex[] create(int size) {
025:         *             return new Vertex[size];
026:         *         }
027:         *     };
028:         *     ...
029:         *     Vertex[] vertices = VERTICES_FACTORY.array(256);
030:         *     [/code]</p>
031:         *          
032:         * @author  <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
033:         * @version 5.0, May 5, 2007
034:         */
035:        public abstract class ArrayFactory/*<T>*/{
036:
037:            /**
038:             * Holds factory for <code>boolean</code> arrays.
039:             */
040:            public static final ArrayFactory/*<boolean[]>*/BOOLEANS_FACTORY = new ArrayFactory() {
041:                protected Object create(int size) {
042:                    return new boolean[size];
043:                }
044:
045:                public void recycle(Object array) {
046:                    recycle(array, ((boolean[]) array).length);
047:                }
048:            };
049:
050:            /**
051:             * Holds factory for <code>byte</code> arrays.
052:             */
053:            public static final ArrayFactory/*<byte[]>*/BYTES_FACTORY = new ArrayFactory() {
054:                protected Object create(int size) {
055:                    return new byte[size];
056:                }
057:
058:                public void recycle(Object array) {
059:                    recycle(array, ((byte[]) array).length);
060:                }
061:            };
062:
063:            /**
064:             * Holds factory for <code>char</code> arrays.
065:             */
066:            public static final ArrayFactory/*<char[]>*/CHARS_FACTORY = new ArrayFactory() {
067:                protected Object create(int size) {
068:                    return new char[size];
069:                }
070:
071:                public void recycle(Object array) {
072:                    recycle(array, ((char[]) array).length);
073:                }
074:            };
075:
076:            /**
077:             * Holds factory for <code>short</code> arrays.
078:             */
079:            public static final ArrayFactory/*<short[]>*/SHORTS_FACTORY = new ArrayFactory() {
080:                protected Object create(int size) {
081:                    return new short[size];
082:                }
083:
084:                public void recycle(Object array) {
085:                    recycle(array, ((short[]) array).length);
086:                }
087:            };
088:
089:            /**
090:             * Holds factory for <code>int</code> arrays.
091:             */
092:            public static final ArrayFactory/*<int[]>*/INTS_FACTORY = new ArrayFactory() {
093:                protected Object create(int size) {
094:                    return new int[size];
095:                }
096:
097:                public void recycle(Object array) {
098:                    recycle(array, ((int[]) array).length);
099:                }
100:            };
101:
102:            /**
103:             * Holds factory for <code>long</code> arrays.
104:             */
105:            public static final ArrayFactory/*<long[]>*/LONGS_FACTORY = new ArrayFactory() {
106:                protected Object create(int size) {
107:                    return new long[size];
108:                }
109:
110:                public void recycle(Object array) {
111:                    recycle(array, ((long[]) array).length);
112:                }
113:            };
114:
115:            /**
116:             * Holds factory for <code>float</code> arrays.
117:             */
118:            /*@JVM-1.5+@
119:             public static final ArrayFactory
120:             /*<float[]>*/
121:            /*@JVM-1.5+@
122:             FLOATS_FACTORY = new ArrayFactory() {
123:             protected Object create(int size) {
124:             return new float[size];
125:             }
126:             public void recycle(Object array) {
127:             recycle(array, ((float[])array).length);
128:             }        
129:             };
130:             /**/
131:
132:            /**
133:             * Holds factory for <code>double</code> arrays.
134:             */
135:            /*@JVM-1.5+@
136:             public static final ArrayFactory
137:             /*<double[]>*/
138:            /*@JVM-1.5+@
139:             DOUBLES_FACTORY = new ArrayFactory() {
140:             protected Object create(int size) {
141:             return new double[size];
142:             }
143:             public void recycle(Object array) {
144:             recycle(array, ((double[])array).length);
145:             }        
146:             };
147:             /**/
148:
149:            /**
150:             * Holds factory for generic <code>Object</code> arrays.
151:             */
152:            public static final ArrayFactory/*<Object[]>*/OBJECTS_FACTORY = new ArrayFactory() {
153:                protected Object create(int size) {
154:                    return new Object[size];
155:                }
156:
157:                public void recycle(Object array) {
158:                    recycle(array, ((Object[]) array).length);
159:                }
160:            };
161:
162:            /**
163:             * Holds factory for arrays up to size 4.
164:             */
165:            private final ObjectFactory _factory4 = new ObjectFactory() {
166:                protected Object create() {
167:                    return ArrayFactory.this .create(4);
168:                }
169:            };
170:
171:            /**
172:             * Holds factory for arrays up to size 8.
173:             */
174:            private final ObjectFactory _factory8 = new ObjectFactory() {
175:                protected Object create() {
176:                    return ArrayFactory.this .create(8);
177:                }
178:            };
179:
180:            /**
181:             * Holds factory for arrays up to size 16.
182:             */
183:            private final ObjectFactory _factory16 = new ObjectFactory() {
184:                protected Object create() {
185:                    return ArrayFactory.this .create(16);
186:                }
187:            };
188:
189:            /**
190:             * Holds factory for arrays up to size 32.
191:             */
192:            private final ObjectFactory _factory32 = new ObjectFactory() {
193:                protected Object create() {
194:                    return ArrayFactory.this .create(32);
195:                }
196:            };
197:
198:            /**
199:             * Holds factory for arrays up to size 64.
200:             */
201:            private final ObjectFactory _factory64 = new ObjectFactory() {
202:                protected Object create() {
203:                    return ArrayFactory.this .create(64);
204:                }
205:            };
206:
207:            /**
208:             * Holds factory for arrays up to size 128.
209:             */
210:            private final ObjectFactory _factory128 = new ObjectFactory() {
211:                protected Object create() {
212:                    return ArrayFactory.this .create(128);
213:                }
214:            };
215:
216:            /**
217:             * Holds factory for arrays up to size 256.
218:             */
219:            private final ObjectFactory _factory256 = new ObjectFactory() {
220:                protected Object create() {
221:                    return ArrayFactory.this .create(256);
222:                }
223:            };
224:
225:            /**
226:             * Holds factory for arrays up to size 512.
227:             */
228:            private final ObjectFactory _factory512 = new ObjectFactory() {
229:                protected Object create() {
230:                    return ArrayFactory.this .create(512);
231:                }
232:            };
233:
234:            /**
235:             * Holds factory for arrays up to size 1024.
236:             */
237:            private final ObjectFactory _factory1024 = new ObjectFactory() {
238:                protected Object create() {
239:                    return ArrayFactory.this .create(1024);
240:                }
241:            };
242:
243:            /**
244:             * Holds factory for arrays up to size 2048.
245:             */
246:            private final ObjectFactory _factory2048 = new ObjectFactory() {
247:                protected Object create() {
248:                    return ArrayFactory.this .create(2048);
249:                }
250:            };
251:
252:            /**
253:             * Holds factory for arrays up to size 4096.
254:             */
255:            private final ObjectFactory _factory4096 = new ObjectFactory() {
256:                protected Object create() {
257:                    return ArrayFactory.this .create(4096);
258:                }
259:            };
260:
261:            /**
262:             * Holds factory for arrays up to size 8192.
263:             */
264:            private final ObjectFactory _factory8192 = new ObjectFactory() {
265:                protected Object create() {
266:                    return ArrayFactory.this .create(8192);
267:                }
268:            };
269:
270:            /**
271:             * Holds factory for arrays up to size 16384.
272:             */
273:            private final ObjectFactory _factory16384 = new ObjectFactory() {
274:                protected Object create() {
275:                    return ArrayFactory.this .create(16384);
276:                }
277:            };
278:
279:            /**
280:             * Holds factory for arrays up to size 32768.
281:             */
282:            private final ObjectFactory _factory32768 = new ObjectFactory() {
283:                protected Object create() {
284:                    return ArrayFactory.this .create(32768);
285:                }
286:            };
287:
288:            /**
289:             * Holds factory for arrays up to size 65536.
290:             */
291:            private final ObjectFactory _factory65536 = new ObjectFactory() {
292:                protected Object create() {
293:                    return ArrayFactory.this .create(65536);
294:                }
295:            };
296:
297:            // Above 65536 we use the heap exclusively. 
298:
299:            /**
300:             * Default constructor.
301:             */
302:            public ArrayFactory() {
303:            }
304:
305:            /**
306:             * Returns an array possibly recycled or preallocated of specified 
307:             * minimum size.
308:             * 
309:             * @param capacity the minimum size of the array to be returned.
310:             * @return a recycled, pre-allocated or new factory array.
311:             */
312:            public final Object/*{T}*/array(int capacity) {
313:                return (capacity <= 4) ? (Object/*{T}*/) _factory4.object()
314:                        : largeArray(capacity);
315:            }
316:
317:            private final Object/*{T}*/largeArray(int capacity) {
318:                if (capacity <= 8)
319:                    return (Object/*{T}*/) _factory8.object();
320:                if (capacity <= 16)
321:                    return (Object/*{T}*/) _factory16.object();
322:                if (capacity <= 32)
323:                    return (Object/*{T}*/) _factory32.object();
324:                if (capacity <= 64)
325:                    return (Object/*{T}*/) _factory64.object();
326:                if (capacity <= 128)
327:                    return (Object/*{T}*/) _factory128.object();
328:                if (capacity <= 256)
329:                    return (Object/*{T}*/) _factory256.object();
330:                if (capacity <= 512)
331:                    return (Object/*{T}*/) _factory512.object();
332:                if (capacity <= 1024)
333:                    return (Object/*{T}*/) _factory1024.object();
334:                if (capacity <= 2048)
335:                    return (Object/*{T}*/) _factory2048.object();
336:                if (capacity <= 4096)
337:                    return (Object/*{T}*/) _factory4096.object();
338:                if (capacity <= 8192)
339:                    return (Object/*{T}*/) _factory8192.object();
340:                if (capacity <= 16384)
341:                    return (Object/*{T}*/) _factory16384.object();
342:                if (capacity <= 32768)
343:                    return (Object/*{T}*/) _factory32768.object();
344:                if (capacity <= 65536)
345:                    return (Object/*{T}*/) _factory65536.object();
346:                return create(capacity);
347:            }
348:
349:            /**
350:             * Recycles the specified arrays.
351:             * 
352:             * @param array the array to be recycled.
353:             */
354:            public void recycle(Object/*{T}*/array) {
355:                recycle(array, ((Object[]) array).length);
356:            }
357:
358:            final void recycle(Object array, int length) {
359:                if (length <= 4) {
360:                    _factory4.recycle(array);
361:                } else if (length <= 8) {
362:                    _factory8.recycle(array);
363:                } else if (length <= 16) {
364:                    _factory16.recycle(array);
365:                } else if (length <= 32) {
366:                    _factory32.recycle(array);
367:                } else if (length <= 64) {
368:                    _factory64.recycle(array);
369:                } else if (length <= 128) {
370:                    _factory128.recycle(array);
371:                } else if (length <= 256) {
372:                    _factory256.recycle(array);
373:                } else if (length <= 512) {
374:                    _factory512.recycle(array);
375:                } else if (length <= 1024) {
376:                    _factory1024.recycle(array);
377:                } else if (length <= 2048) {
378:                    _factory2048.recycle(array);
379:                } else if (length <= 4096) {
380:                    _factory4096.recycle(array);
381:                } else if (length <= 8192) {
382:                    _factory8192.recycle(array);
383:                } else if (length <= 16384) {
384:                    _factory16384.recycle(array);
385:                } else if (length <= 32768) {
386:                    _factory32768.recycle(array);
387:                } else if (length <= 65536) {
388:                    _factory65536.recycle(array);
389:                }
390:            }
391:
392:            /**
393:             * Constructs a new array of specified size from this factory 
394:             * (using the <code>new</code> keyword).
395:             *
396:             * @param size the size of the array.
397:             * @return a new factory array.
398:             */
399:            protected abstract Object/*{T}*/create(int size);
400:
401:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.