Home | History | Annotate | Download | only in arrays
      1 /*******************************************************************************
      2  * Copyright (c) 2013, Daniel Murphy
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without modification,
      6  * are permitted provided that the following conditions are met:
      7  * 	* Redistributions of source code must retain the above copyright notice,
      8  * 	  this list of conditions and the following disclaimer.
      9  * 	* Redistributions in binary form must reproduce the above copyright notice,
     10  * 	  this list of conditions and the following disclaimer in the documentation
     11  * 	  and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     16  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     19  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     20  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     22  * POSSIBILITY OF SUCH DAMAGE.
     23  ******************************************************************************/
     24 package org.jbox2d.pooling.arrays;
     25 
     26 import java.util.HashMap;
     27 
     28 import org.jbox2d.common.Vec2;
     29 
     30 /**
     31  * not thread safe Vec2[] pool
     32  * @author dmurph
     33  *
     34  */
     35 public class Vec2Array {
     36 
     37 	private final HashMap<Integer, Vec2[]> map = new HashMap<Integer, Vec2[]>();
     38 
     39 	public Vec2[] get( int argLength){
     40 		assert(argLength > 0);
     41 
     42 		if(!map.containsKey(argLength)){
     43 			map.put(argLength, getInitializedArray(argLength));
     44 		}
     45 
     46 		assert(map.get(argLength).length == argLength) : "Array not built of correct length";
     47 		return map.get(argLength);
     48 	}
     49 
     50 	protected Vec2[] getInitializedArray(int argLength){
     51 		final Vec2[] ray = new Vec2[argLength];
     52 		for (int i = 0; i < ray.length; i++) {
     53 			ray[i] = new Vec2();
     54 		}
     55 		return ray;
     56 	}
     57 }
     58