--- /dev/null
+/*\r
+ * Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the\r
+ * License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"\r
+ * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language\r
+ * governing permissions and limitations under the License.\r
+ */\r
+\r
+package com.badlogic.gdx.utils;\r
+\r
+/**\r
+ * An ordered, resizable array of objects that reuses element instances. The {@link #add()} and {@link #insert(int)} methods add\r
+ * objects to the pool. The objects are created by {@link #newObject()}. Any other methods that would add objects to the pool are\r
+ * not supported. When an object is removed from the pool, a reference to it is retained for reuse.\r
+ * @author Nathan Sweet\r
+ */\r
+abstract public class Pool<T> extends Array<T> {\r
+ public final int max;\r
+\r
+ public Pool (boolean ordered, int capacity) {\r
+ this(ordered, capacity, -1);\r
+ }\r
+\r
+ /**\r
+ * @param max The maximum size of this pool. -1 for no max size. See {@link #add()}.\r
+ */\r
+ public Pool (boolean ordered, int capacity, int max) {\r
+ super(ordered, capacity);\r
+ this.max = max;\r
+ }\r
+\r
+ public Pool (boolean ordered, int capacity, Class<T> arrayType) {\r
+ this(ordered, capacity, arrayType, -1);\r
+ }\r
+\r
+ /**\r
+ * @param max The maximum size of this pool. -1 for no max size. See {@link #add()}.\r
+ */\r
+ public Pool (boolean ordered, int capacity, Class<T> arrayType, int max) {\r
+ super(ordered, capacity, arrayType);\r
+ this.max = max;\r
+ }\r
+\r
+ abstract protected T newObject ();\r
+\r
+ /**\r
+ * Returns an object from this pool. The object may be new (from {@link #newObject()}) or reused (previously\r
+ * {@link #removeValue(Object, boolean) removed} from the pool). If this pool already contains {@link #max} objects, a new\r
+ * object is returned, but it is not added to the pool (it will be garbage collected when removed).\r
+ */\r
+ public T add () {\r
+ if (size == max) return newObject();\r
+ T[] items = this.items;\r
+ if (size == items.length) {\r
+ T item = newObject();\r
+ resize(Math.max(8, (int)(size * 1.75f)))[size++] = item;\r
+ return item;\r
+ }\r
+ T item = items[size];\r
+ if (item == null) item = newObject();\r
+ items[size++] = item;\r
+ return item;\r
+ }\r
+\r
+ public T insert (int index) {\r
+ if (size == items.length) {\r
+ T item = newObject();\r
+ resize(Math.max(8, (int)(size * 1.75f)))[size++] = item;\r
+ return item;\r
+ }\r
+ T item = items[size];\r
+ if (item == null) item = newObject();\r
+ System.arraycopy(items, index, items, index + 1, size - index);\r
+ size++;\r
+ items[index] = item;\r
+ return item;\r
+ }\r
+\r
+ public void removeIndex (int index) {\r
+ if (index >= size) throw new IndexOutOfBoundsException(String.valueOf(index));\r
+ size--;\r
+ Object[] items = this.items;\r
+ T old = (T)items[index];\r
+ System.arraycopy(items, index + 1, items, index, size - index);\r
+ items[size] = old;\r
+ }\r
+\r
+ /**\r
+ * Removes and returns the item at the specified index.\r
+ */\r
+ public T pop (int index) {\r
+ if (index >= size) throw new IndexOutOfBoundsException(String.valueOf(index));\r
+ size--;\r
+ Object[] items = this.items;\r
+ T old = (T)items[index];\r
+ System.arraycopy(items, index + 1, items, index, size - index);\r
+ items[size] = old;\r
+ return old;\r
+ }\r
+\r
+ /**\r
+ * Not supported for a pool. Use {@link #add()}.\r
+ */\r
+ public void add (T value) {\r
+ throw new UnsupportedOperationException("Not supported for a pool.");\r
+ }\r
+\r
+ /**\r
+ * Not supported for a pool. Use {@link #add()}.\r
+ */\r
+ public void addAll (Array array) {\r
+ throw new UnsupportedOperationException("Not supported for a pool.");\r
+ }\r
+\r
+ /**\r
+ * Not supported for a pool. Use {@link #add()}.\r
+ */\r
+ public void set (int index, T value) {\r
+ throw new UnsupportedOperationException("Not supported for a pool.");\r
+ }\r
+\r
+ /**\r
+ * Not supported for a pool. Use {@link #add()}.\r
+ */\r
+ public void insert (int index, T value) {\r
+ throw new UnsupportedOperationException("Not supported for a pool.");\r
+ }\r
+}\r