KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > ArrayFactories


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Nov 19, 2004
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec;
14
15 /**
16  * Provide some standard ArrayFactory implementations.
17  * @author Ben Yu
18  *
19  * Nov 19, 2004
20  */

21 public final class ArrayFactories {
22   /**
23    * gets and instance of ArrayFactory that simply create Object[].
24    * @return the ArrayFactory instance.
25    */

26   public static ArrayFactory<Object JavaDoc> defaultFactory(){
27     return singleton;
28   }
29   /**
30    * gets and instanceof ArrayFactory that creates T[] for a given element type T.
31    * @param etype the element type.
32    * @return the ArrayFactory instance.
33    */

34   public static <E> ArrayFactory<E> typedFactory(final Class JavaDoc<E> etype){
35     return new ArrayFactory<E>(){
36       public E[] createArray(int l){
37         return (E[])java.lang.reflect.Array.newInstance(etype, l);
38       }
39     };
40   }
41   private static final ArrayFactory<Object JavaDoc> singleton = new ArrayFactory<Object JavaDoc>(){
42     public Object JavaDoc[] createArray(int l){
43       return new Object JavaDoc[l];
44     }
45   };
46 }
47
Popular Tags