<commit_before>// Copyright 2004-2007 Castle Project - http://www.castleproject.org/
// 
// 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.

namespace Castle.MicroKernel.Resolvers
{
	using System;
	using System.Collections;

	using Castle.Core;

	using Castle.MicroKernel.Util;
	using Castle.MicroKernel.SubSystems.Conversion;

	/// <summary>
	/// Default implementation for <see cref="IDependencyResolver"/>.
	/// This implementation is quite simple, but still should be useful
	/// for 99% of situations. 
	/// </summary>
	[Serializable]
	public class DefaultDependencyResolver : IDependencyResolver
	{
		private readonly IKernel kernel;
		private readonly ITypeConverter converter;
		private DependencyDelegate dependencyResolvingDelegate;
		private IList subResolvers = new ArrayList();

		/// <summary>
		/// Initializes a new instance of the <see cref="DefaultDependencyResolver"/> class.
		/// </summary>
		/// <param name="kernel">The kernel.</param>
		public DefaultDependencyResolver(IKernel kernel)
		{
			this.kernel = kernel;

			converter = (ITypeConverter) kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
		}

		/// <summary>
		/// Initializes this instance with the specified dependency delegate.
		/// </summary>
		/// <param name="dependencyDelegate">The dependency delegate.</param>
		public void Initialize(DependencyDelegate dependencyDelegate)
		{
			dependencyResolvingDelegate = dependencyDelegate;
		}

		/// <summary>
		/// Registers a sub resolver instance
		/// </summary>
		/// <param name="subResolver">The subresolver instance</param>
		public void AddSubResolver(ISubDependencyResolver subResolver)
		{
			if (subResolver == null) throw new ArgumentNullException("subResolver");

			subResolvers.Add(subResolver);
		}

		/// <summary>
		/// Unregisters a sub resolver instance previously registered
		/// </summary>
		/// <param name="subResolver">The subresolver instance</param>
		public void RemoveSubResolver(ISubDependencyResolver subResolver)
		{
			if (subResolver == null) throw new ArgumentNullException("subResolver");

			subResolvers.Remove(subResolver);
		}

		/// <summary>
		/// Returns true if the resolver is able to satisfy the specified dependency.
		/// </summary>
		/// <param name="context">Creation context, which is a resolver itself</param>
		/// <param name="parentResolver">Parent resolver</param>
		/// <param name="model">Model of the component that is requesting the dependency</param>
		/// <param name="dependency">The dependency model</param>
		/// <returns><c>true</c> if the dependency can be satisfied</returns>
		public bool CanResolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency)
		{
			// 1 - check for the dependency on CreationContext, if present

			if (context != null)
			{
				if (context.CanResolve(context, parentResolver, model, dependency))
				{
					return true;
				}
			}

			// 2 - check within parent resolver, if present

			if (parentResolver != null)
			{
				if (parentResolver.CanResolve(context, parentResolver, model, dependency))
				{
					return true;
				}
			}

			// 3 - check within subresolvers

			foreach(ISubDependencyResolver subResolver in subResolvers)
			{
				if (subResolver.CanResolve(context, parentResolver, model, dependency))
				{
					return true;
				}
			}

			// 4 - normal flow, checking against the kernel

			if (dependency.DependencyType == DependencyType.Service || 
			    dependency.DependencyType == DependencyType.ServiceOverride)
			{
				return CanResolveServiceDependency(model, dependency);
			}
			else
			{
				return CanResolveParameterDependency(model, dependency);
			}
		}

		/// <summary><commit_msg>Check the models handler for custom dependencies during an attempt to determine of the dependency can be resolved (CanResolve)<commit_after>// Copyright 2004-2007 Castle Project - http://www.castleproject.org/
// 
// 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.

namespace Castle.MicroKernel.Resolvers
{
	using System;
	using System.Collections;

	using Castle.Core;

	using Castle.MicroKernel.Util;
	using Castle.MicroKernel.SubSystems.Conversion;

	/// <summary>
	/// Default implementation for <see cref="IDependencyResolver"/>.
	/// This implementation is quite simple, but still should be useful
	/// for 99% of situations. 
	/// </summary>
	[Serializable]
	public class DefaultDependencyResolver : IDependencyResolver
	{
		private readonly IKernel kernel;
		private readonly ITypeConverter converter;
		private DependencyDelegate dependencyResolvingDelegate;
		private IList subResolvers = new ArrayList();

		/// <summary>
		/// Initializes a new instance of the <see cref="DefaultDependencyResolver"/> class.
		/// </summary>
		/// <param name="kernel">The kernel.</param>
		public DefaultDependencyResolver(IKernel kernel)
		{
			this.kernel = kernel;

			converter = (ITypeConverter) kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
		}

		/// <summary>
		/// Initializes this instance with the specified dependency delegate.
		/// </summary>
		/// <param name="dependencyDelegate">The dependency delegate.</param>
		public void Initialize(DependencyDelegate dependencyDelegate)
		{
			dependencyResolvingDelegate = dependencyDelegate;
		}

		/// <summary>
		/// Registers a sub resolver instance
		/// </summary>
		/// <param name="subResolver">The subresolver instance</param>
		public void AddSubResolver(ISubDependencyResolver subResolver)
		{
			if (subResolver == null) throw new ArgumentNullException("subResolver");

			subResolvers.Add(subResolver);
		}

		/// <summary>
		/// Unregisters a sub resolver instance previously registered
		/// </summary>
		/// <param name="subResolver">The subresolver instance</param>
		public void RemoveSubResolver(ISubDependencyResolver subResolver)
		{
			if (subResolver == null) throw new ArgumentNullException("subResolver");

			subResolvers.Remove(subResolver);
		}

		/// <summary>
		/// Returns true if the resolver is able to satisfy the specified dependency.
		/// </summary>
		/// <param name="context">Creation context, which is a resolver itself</param>
		/// <param name="parentResolver">Parent resolver</param>
		/// <param name="model">Model of the component that is requesting the dependency</param>
		/// <param name="dependency">The dependency model</param>
		/// <returns><c>true</c> if the dependency can be satisfied</returns>
		public bool CanResolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency)
		{
			// 1 - check for the dependency on CreationContext, if present

			if (context != null)
			{
				if (context.CanResolve(context, parentResolver, model, dependency))
				{
					return true;
				}
			}

            // 2 - check with the model's handler, if not the same as the parent resolver
            IHandler handler = kernel.GetHandler(model.Name);

            if (handler != null && handler != parentResolver)
            {
                if (handler.CanResolve(context, parentResolver, model, dependency))
                {
                    return true;
                }
            }

			// 3 - check within parent resolver, if present

			if (parentResolver != null)
			{
				if (parentResolver.CanResolve(context, parentResolver, model, dependency))
				{
					return true;
				}
			}

			// 4 - check within subresolvers

			foreach(ISubDependencyResolver subResolver in subResolvers)
			{
				if (subResolver.CanResolve(context, parentResolver, model, dependency))
				{
					return true;
				}
			}

			// 5 - normal flow, checking against the kernel

			if (dependency.DependencyType == DependencyType.Service || 
			    dependency.DependencyType == DependencyType.ServiceOverride)
			{
				return CanResolveServiceDependency(model, dependency);
			}
			else
			{
				return CanResolveParameterDependency(model, dependency);
			}
		}

		/// <summary>
{'fraction_non_alphanumeric': 0.1052396878483835, 'fraction_numerical': 0.004124860646599777, 'mean_word_length': 3.4543197616683217, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3635943', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2317, 'n_words': 752}