/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.utils;

/** Stores a map of {@link ReflectionPool}s by type for convenient static access.
 * @author Nathan Sweet */
public class Pools {
	static private final ObjectMap<Class, ReflectionPool> typePools = new ObjectMap();

	/** Returns a new or existing pool for the specified type, stored in a a Class to {@link ReflectionPool} map. The max size of
	 * the pool used is 100. */
	static public <T> Pool<T> get (Class<T> type) {
		ReflectionPool pool = typePools.get(type);
		if (pool == null) {
			pool = new ReflectionPool(type, 4, 100);
			typePools.put(type, pool);
		}
		return pool;
	}

	/** Obtains an object from the {@link #get(Class) pool}. */
	static public <T> T obtain (Class<T> type) {
		return (T)get(type).obtain();
	}

	/** Frees an object from the {@link #get(Class) pool}. */
	static public void free (Object object) {
		if (object == null) throw new IllegalArgumentException("object cannot be null.");
		ReflectionPool pool = typePools.get(object.getClass());
		if (pool == null) return; // Ignore freeing an object that was never retained.
		pool.free(object);
	}

	/** Frees the specified objects from the {@link #get(Class) pool}. Null objects within the array are silently ignored. Objects
	 * don't need to be from the same pool. */
	static public void freeAll (Array objects) {
		freeAll(objects, false);
	}

	/** Frees the specified objects from the {@link #get(Class) pool}. Null objects within the array are silently ignored.
	 * @param samePool If true, objects don't need to be from the same pool but the pool must be looked up for each object. */
	static public void freeAll (Array objects, boolean samePool) {
		if (objects == null) throw new IllegalArgumentException("objects cannot be null.");
		ReflectionPool pool = null;
		for (int i = 0, n = objects.size; i < n; i++) {
			Object object = objects.get(i);
			if (object == null) continue;
			if (pool == null) {
				pool = typePools.get(object.getClass());
				if (pool == null) continue; // Ignore freeing an object that was never retained.
			}
			pool.free(object);
			if (!samePool) pool = null;
		}
	}

	private Pools () {
	}
}

@@ -1133,17 +1133,22 @@
 l%7D map. 
-T
+Note t
 he max s
@@ -1154,37 +1154,83 @@
 size
- of
 %0D%0A%09 * 
-the pool used is 100
+is ignored if this is not the first time this pool has been requested
 . */
@@ -1272,24 +1272,33 @@
 lass%3CT%3E type
+, int max
 ) %7B%0D%0A%09%09Refle
@@ -1401,11 +1401,11 @@
  4, 
-100
+max
 );%0D%0A
@@ -1458,24 +1458,264 @@
 ool;%0D%0A%09%7D%0D%0A%0D%0A
+%09/** Returns a new or existing pool for the specified type, stored in a a Class to %7B@link ReflectionPool%7D map. The max size of%0D%0A%09 * the pool used is 100. */%0D%0A%09static public %3CT%3E Pool%3CT%3E get (Class%3CT%3E type) %7B%0D%0A%09%09return get(type, 100);%0D%0A%09%7D%0D%0A%0D%0A
 %09/** Obtains

Allow setting pool size, with caveat it must be done first.