using System;
using System.Collections.Generic;

namespace ObjectExtensions.Copy.Internal.TypeCopyHandling
{
    internal static class TypeCopyHandlerCache
    {
        private static readonly object SyncRoot = new object();
        private static readonly Dictionary<Type, TypeCopyHandler> Cache = new Dictionary<Type, TypeCopyHandler>();

        internal static TypeCopyHandler GetOrCreate(Type instanceType)
        {
            lock (SyncRoot)
            {
                TypeCopyHandler cacheEntry;
                if (Cache.TryGetValue(instanceType, out cacheEntry)) 
                    return cacheEntry;

                cacheEntry = new TypeCopyHandler(instanceType);
                Cache.Add(instanceType, cacheEntry);
                return cacheEntry;
            }
        }
    }
}