View Javadoc
1   /*
2    * Copyright 2010 Daniel Kurka
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package com.googlecode.mgwt.collection.shared;
17  
18  import com.google.gwt.core.client.GWT;
19  import com.googlecode.mgwt.collection.client.JsLightArray;
20  import com.googlecode.mgwt.collection.client.JsLightArrayInteger;
21  import com.googlecode.mgwt.collection.client.JsLightMap;
22  import com.googlecode.mgwt.collection.shared.java.JavaLightArray;
23  import com.googlecode.mgwt.collection.shared.java.JavaLightArrayInt;
24  import com.googlecode.mgwt.collection.shared.java.JavaLightMap;
25  
26  /**
27   * A factory to create instances of the light collections api.
28   * 
29   * If this is run in production code javascript objects are used to speed up collections. If run
30   * inside a JVM / dev mode the java implementation is used
31   * 
32   * @author Daniel Kurka
33   * 
34   */
35  public class CollectionFactory {
36    /**
37     * Construct a LightMap
38     * 
39     * @param <V> the type of the map
40     * @return the map
41     */
42  	public static <V> LightMap<V> constructMap() {
43  		if (GWT.isProdMode()) {
44  			return new JsLightMap<V>();
45  		} else {
46  			return new JavaLightMap<V>();
47  		}
48  	}
49  
50    /**
51     * Construct a {@link LightArray}
52     * 
53     * @param <V> the type of objects that go into the array
54     * @return the array
55     */
56  	public static <V> LightArray<V> constructArray() {
57  		if (GWT.isProdMode()) {
58  			return new JsLightArray<V>();
59  		} else {
60  			return new JavaLightArray<V>();
61  		}
62  	}
63  
64    /**
65     * Construct an array of integers
66     * 
67     * @return the array
68     */
69  	public static LightArrayInt constructIntegerArray() {
70  		if (GWT.isProdMode()) {
71  			return new JsLightArrayInteger();
72  		} else {
73  			return new JavaLightArrayInt();
74  		}
75  	}
76  }