/*
 * This file is part of CitizensNpcProvider for NucleusFramework, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.jcwhatever.nucleus.providers.citizensnpc.traits;

import com.jcwhatever.nucleus.providers.npc.traits.NpcTrait;
import com.jcwhatever.nucleus.providers.npc.traits.NpcTraitType;
import com.jcwhatever.nucleus.utils.PreCon;

import java.util.LinkedList;
import java.util.Map;
import java.util.WeakHashMap;
import javax.annotation.Nullable;

/**
 * Allows reusing trait instances to prevent buildup of long lived objects in heap
 * due to the transient nature of script generated NPC's.
 */
public class TraitPool {

    private Map<NpcTraitType, LinkedList<NpcTrait>> _pools = new WeakHashMap<>(35);

    /**
     * Get a pooled trait.
     *
     * @param type  The type of trait to get from the pool.
     *
     * @return  A trait removed from the pool or null if the pool is empty.
     */
    @Nullable
    public NpcTrait getPooled(NpcTraitType type) {
        PreCon.notNull(type);

        LinkedList<NpcTrait> pooled = _pools.get(type);
        if (pooled == null || pooled.isEmpty())
            return null;

        return pooled.remove();
    }

    /**
     * Add a trait to the pool.
     *
     * @param trait  The unused trait to add.
     *
     * @return  True if the trait is added, false if rejected.
     */
    public boolean pool(NpcTrait trait) {
        PreCon.notNull(trait);

        if (!trait.isReusable())
            return false;

        LinkedList<NpcTrait> pooled = _pools.get(trait.getType());
        if (pooled == null) {
            pooled = new LinkedList<>();
            _pools.put(trait.getType(), pooled);
        }

        if (!pooled.contains(trait))
            pooled.add(trait);

        return true;
    }
}

@@ -1468,37 +1468,147 @@
 on;%0A
-%0A
 import 
-java.util.LinkedList;
+com.jcwhatever.nucleus.utils.performance.pool.IPoolElementFactory;%0Aimport com.jcwhatever.nucleus.utils.performance.pool.SimplePool;%0A
 %0Aimp
@@ -1882,36 +1882,267 @@
 ate 
-Map%3CNpcTraitType, LinkedList
+static IPoolElementFactory%3CNpcTrait%3E ELEMENT_FACTORY = new IPoolElementFactory%3CNpcTrait%3E() %7B%0A        @Override%0A        public NpcTrait create() %7B%0A            throw new UnsupportedOperationException();%0A        %7D%0A    %7D;%0A%0A    private Map%3CNpcTraitType, SimplePool
 %3CNpc
@@ -2475,34 +2475,34 @@
 );%0A%0A        
-LinkedList
+SimplePool
 %3CNpcTrait%3E p
@@ -2568,17 +2568,19 @@
 led.
-isEmpty()
+size() == 0
 )%0A  
@@ -2627,18 +2627,20 @@
 ooled.re
-mo
+trie
 ve();%0A  
@@ -2957,26 +2957,26 @@
         
-LinkedList
+SimplePool
 %3CNpcTrai
@@ -3075,21 +3075,64 @@
 new 
-LinkedList%3C%3E(
+SimplePool%3CNpcTrait%3E(NpcTrait.class, 25, ELEMENT_FACTORY
 );%0A 
@@ -3246,19 +3246,23 @@
  pooled.
-add
+recycle
 (trait);

replace LinkedList with SimplePool in TraitPool