/
com/planet_ink/coffee_mud/Abilities/Common/
com/planet_ink/coffee_mud/Abilities/Diseases/
com/planet_ink/coffee_mud/Abilities/Druid/
com/planet_ink/coffee_mud/Abilities/Fighter/
com/planet_ink/coffee_mud/Abilities/Languages/
com/planet_ink/coffee_mud/Abilities/Misc/
com/planet_ink/coffee_mud/Abilities/Prayers/
com/planet_ink/coffee_mud/Abilities/Properties/
com/planet_ink/coffee_mud/Abilities/Skills/
com/planet_ink/coffee_mud/Abilities/Songs/
com/planet_ink/coffee_mud/Abilities/Spells/
com/planet_ink/coffee_mud/Abilities/Thief/
com/planet_ink/coffee_mud/Abilities/Traps/
com/planet_ink/coffee_mud/Behaviors/
com/planet_ink/coffee_mud/CharClasses/interfaces/
com/planet_ink/coffee_mud/Commands/
com/planet_ink/coffee_mud/Commands/interfaces/
com/planet_ink/coffee_mud/Common/
com/planet_ink/coffee_mud/Common/interfaces/
com/planet_ink/coffee_mud/Exits/interfaces/
com/planet_ink/coffee_mud/Items/Armor/
com/planet_ink/coffee_mud/Items/Basic/
com/planet_ink/coffee_mud/Items/CompTech/
com/planet_ink/coffee_mud/Items/MiscMagic/
com/planet_ink/coffee_mud/Items/Weapons/
com/planet_ink/coffee_mud/Items/interfaces/
com/planet_ink/coffee_mud/Libraries/
com/planet_ink/coffee_mud/Libraries/interfaces/
com/planet_ink/coffee_mud/Locales/
com/planet_ink/coffee_mud/MOBS/
com/planet_ink/coffee_mud/Races/
com/planet_ink/coffee_mud/Races/interfaces/
com/planet_ink/coffee_mud/WebMacros/
com/planet_ink/coffee_mud/WebMacros/interfaces/
com/planet_ink/coffee_mud/core/
com/planet_ink/coffee_mud/core/collections/
com/planet_ink/coffee_mud/core/interfaces/
com/planet_ink/coffee_mud/core/intermud/
com/planet_ink/coffee_mud/core/intermud/i3/
com/planet_ink/coffee_web/server/
com/planet_ink/siplet/applet/
lib/
resources/factions/
resources/fakedb/
resources/progs/autoplayer/
resources/quests/holidays/
web/
web/admin.templates/
web/admin/grinder/
web/admin/images/
web/clan.templates/
web/pub.templates/
web/pub/images/mxp/
web/pub/sounds/
web/pub/textedit/
package com.planet_ink.coffee_mud.core.collections;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Vector;

import com.planet_ink.coffee_mud.core.CMParms;
import com.planet_ink.coffee_mud.core.CMath;
import com.planet_ink.coffee_mud.core.Log;
import com.planet_ink.coffee_mud.core.interfaces.CMObject;

/*
   Copyright 2011-2016 Bo Zimmerman

   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.
 */
public class ReusableObjectPool<T extends CMObject>
{
	private final NotifyingCMObjectVector<T>		masterList;
	private final Stack<NotifyingCMObjectVector<T>>	masterPool	= new Stack<NotifyingCMObjectVector<T>>();
	private volatile int							created		= 0;
	private volatile int							requests	= 0;
	private volatile int							distributed	= 0;
	private volatile int							returned	= 0;
	private final int								minEntries;
	private final Object							sync		= new Object();
	private final PoolFixer							fixer		= new PoolFixer();

	private class PoolFixer implements Runnable
	{
		private boolean	isRunning	= false;

		public void startFixing()
		{
			if (isRunning)
			{
				fix();
				return;
			}
			synchronized (this)
			{
				if (!isRunning)
				{
					isRunning = true;
					new Thread(Thread.currentThread().getThreadGroup(), this, "PoolFixer" + Thread.currentThread().getThreadGroup().getName().charAt(0)).start();
					return;
				}
			}
			fix();
		}

		public void fix()
		{
			Runtime.getRuntime().runFinalization();
			System.gc();
			try
			{
				Thread.sleep(100);
			}
			catch (final Exception e)
			{
			}
			Runtime.getRuntime().runFinalization();
			try
			{
				Thread.sleep(100);
			}
			catch (final Exception e)
			{
			}
			System.gc();
		}

		@Override
		public void run()
		{
			try
			{
				int iters = 0;
				while (masterPool.size() < minEntries)
				{
					fix();
					if ((masterPool.size() < minEntries) && (iters > 5))
					{
						iters = 0;
						synchronized (sync)
						{
							masterPool.add(makeNewEntry());
						}
					}
					iters++;
				}
			}
			finally
			{
				synchronized (this)
				{
					isRunning = false;
				}
			}
		}
	}

	private class NotifyingCMObjectVector<K extends CMObject> extends Vector<T>
	{
		private static final long	serialVersionUID	= 1L;

		public NotifyingCMObjectVector(final List<T> V)
		{
			super(V);
		}

		public NotifyingCMObjectVector(int size)
		{
			super(size);
		}

		@Override
		protected void finalize() throws Throwable
		{
			final NotifyingCMObjectVector<T> V = new NotifyingCMObjectVector<T>(this);
			synchronized (sync)
			{
				returned++;
				masterPool.push(V);
			}
			super.finalize();
		}
	}

	public ReusableObjectPool(final List<T> initialEntry, final int minEntries)
	{
		if (initialEntry.size() == 0)
			this.masterList = new NotifyingCMObjectVector<T>(new ReadOnlyList<T>(initialEntry));
		else
			this.masterList = new NotifyingCMObjectVector<T>(initialEntry);
		this.minEntries = (minEntries < 2) ? 2 : minEntries;
		for (int i = 0; i < minEntries; i++)
			masterPool.add(makeNewEntry());
	}

	public int getMasterPoolSize()
	{
		return masterPool.size();
	}

	public int getListSize()
	{
		return masterList.size();
	}

	@SuppressWarnings("unchecked")
	private final NotifyingCMObjectVector<T> makeNewEntry()
	{
		final NotifyingCMObjectVector<T> myList = new NotifyingCMObjectVector<T>(masterList.size());
		for (final T o : masterList)
			myList.add((T) o.copyOf());
		created++;
		if (created == minEntries * 1000)
			Log.errOut("ReuseOP", "Reusable Object Pool pass all reason: " + CMParms.toListString(masterList));
		return myList;
	}

	public final List<T> get()
	{
		if (masterList.size() == 0)
			return masterList;
		requests++;
		if (masterPool.size() < minEntries / 2)
			fixer.startFixing();
		synchronized (sync)
		{
			if (!masterPool.isEmpty())
			{
				final NotifyingCMObjectVector<T> myList = masterPool.pop();
				if (myList != null)
				{
					distributed++;
					return myList;
				}
			}
		}
		fixer.fix();
		return makeNewEntry();
		// final List<T> myList=new Vector<T>(masterList.size());
		// for(final T o : masterList)
		// myList.add((T)o.copyOf());
		// return myList;
	}
}