#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you 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.
//
#endregion

using System;
using System.Collections;

namespace log4net.Util;

/// <summary>
/// An always empty <see cref="ICollection"/>.
/// </summary>
/// <remarks>
/// <para>
/// A singleton implementation of the <see cref="ICollection"/>
/// interface that always represents an empty collection.
/// </para>
/// </remarks>
/// <author>Nicko Cadell</author>
/// <author>Gert Driesen</author>
[Log4NetSerializable]
public sealed class EmptyCollection : ICollection
{
  /// <summary>
  /// Initializes a new instance of the <see cref="EmptyCollection" /> class. 
  /// </summary>
  /// <remarks>
  /// <para>
  /// Uses a private access modifier to enforce the singleton pattern.
  /// </para>
  /// </remarks>
  private EmptyCollection()
  {
  }

  /// <summary>
  /// Gets the singleton instance of the empty collection.
  /// </summary>
  /// <returns>The singleton instance of the empty collection.</returns>
  /// <remarks>
  /// <para>
  /// Gets the singleton instance of the empty collection.
  /// </para>
  /// </remarks>
  public static EmptyCollection Instance { get; } = new();

  /// <summary>
  /// Copies the elements of the <see cref="ICollection"/> to an 
  /// <see cref="Array"/>, starting at a particular Array index.
  /// </summary>
  /// <param name="array">The one-dimensional <see cref="Array"/> 
  /// that is the destination of the elements copied from 
  /// <see cref="ICollection"/>. The Array must have zero-based 
  /// indexing.</param>
  /// <param name="index">The zero-based index in array at which 
  /// copying begins.</param>
  /// <remarks>
  /// <para>
  /// As the collection is empty no values are copied into the array.
  /// </para>
  /// </remarks>
  public void CopyTo(Array array, int index)
  {
    // copy nothing
  }

  /// <summary>
  /// Gets a value indicating if access to the <see cref="ICollection"/> is synchronized (thread-safe).
  /// </summary>
  /// <value>
  /// <b>true</b> if access to the <see cref="ICollection"/> is synchronized (thread-safe); otherwise, <b>false</b>.
  /// </value>
  /// <remarks>
  /// <para>
  /// For the <see cref="EmptyCollection"/> this property is always <see langword="true"/>.
  /// </para>
  /// </remarks>
  public bool IsSynchronized => true;

  /// <summary>
  /// Gets the number of elements contained in the <see cref="ICollection"/>.
  /// </summary>
  /// <value>
  /// The number of elements contained in the <see cref="ICollection"/>.
  /// </value>
  /// <remarks>
  /// <para>
  /// As the collection is empty the <see cref="Count"/> is always <c>0</c>.
  /// </para>
  /// </remarks>
  public int Count => 0;

  /// <summary>
  /// Gets an object that can be used to synchronize access to the <see cref="ICollection"/>.
  /// </summary>
  /// <value>
  /// An object that can be used to synchronize access to the <see cref="ICollection"/>.
  /// </value>
  /// <remarks>
  /// <para>
  /// As the collection is empty and thread safe and synchronized this instance is also
  /// the <see cref="SyncRoot"/> object.
  /// </para>
  /// </remarks>
  public object SyncRoot => this;

  /// <summary>
  /// Returns an enumerator that can iterate through a collection.
  /// </summary>
  /// <returns>
  /// An <see cref="IEnumerator"/> that can be used to 
  /// iterate through the collection.
  /// </returns>
  /// <remarks>
  /// <para>
  /// As the collection is empty a <see cref="NullEnumerator"/> is returned.
  /// </para>
  /// </remarks>
  public IEnumerator GetEnumerator() => NullEnumerator.Instance;
}
