using System;
using System.Collections.Generic;
namespace Umbraco.Core.Composing
{
///
/// Implements an un-ordered collection builder.
///
/// The type of the builder.
/// The type of the collection.
/// The type of the items.
///
/// A set collection builder is the most basic collection builder,
/// where items are not ordered.
///
public abstract class SetCollectionBuilderBase : CollectionBuilderBase
where TBuilder : SetCollectionBuilderBase
where TCollection : class, IBuilderCollection
{
protected abstract TBuilder This { get; }
///
/// Clears all types in the collection.
///
/// The builder.
public TBuilder Clear()
{
Configure(types => types.Clear());
return This;
}
///
/// Adds a type to the collection.
///
/// The type to append.
/// The builder.
public TBuilder Add()
where T : TItem
{
Configure(types =>
{
var type = typeof(T);
if (types.Contains(type)) types.Remove(type);
types.Add(type);
});
return This;
}
///
/// Adds a type to the collection.
///
/// The type to append.
/// The builder.
public TBuilder Add(Type type)
{
Configure(types =>
{
EnsureType(type, "register");
if (types.Contains(type)) types.Remove(type);
types.Add(type);
});
return This;
}
///
/// Adds types to the collections.
///
/// The types to append.
/// The builder.
public TBuilder Add(IEnumerable types)
{
Configure(list =>
{
foreach (var type in types)
{
// would be detected by CollectionBuilderBase when registering, anyways, but let's fail fast
EnsureType(type, "register");
if (list.Contains(type)) list.Remove(type);
list.Add(type);
}
});
return This;
}
///
/// Removes a type from the collection.
///
/// The type to remove.
/// The builder.
public TBuilder Remove()
where T : TItem
{
Configure(types =>
{
var type = typeof(T);
if (types.Contains(type)) types.Remove(type);
});
return This;
}
///
/// Removes a type from the collection.
///
/// The type to remove.
/// The builder.
public TBuilder Remove(Type type)
{
Configure(types =>
{
EnsureType(type, "remove");
if (types.Contains(type)) types.Remove(type);
});
return This;
}
///
/// Replaces a type in the collection.
///
/// The type to replace.
/// The type to insert.
/// The builder.
/// Throws if the type to replace does not already belong to the collection.
public TBuilder Replace()
where TReplaced : TItem
where T : TItem
{
Configure(types =>
{
var typeReplaced = typeof(TReplaced);
var type = typeof(T);
if (typeReplaced == type) return;
var index = types.IndexOf(typeReplaced);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeReplaced); // in case removing type changed index
types.Insert(index, type);
types.Remove(typeReplaced);
});
return This;
}
///
/// Replaces a type in the collection.
///
/// The type to replace.
/// The type to insert.
/// The builder.
/// Throws if the type to replace does not already belong to the collection.
public TBuilder Replace(Type typeReplaced, Type type)
{
Configure(types =>
{
EnsureType(typeReplaced, "find");
EnsureType(type, "register");
if (typeReplaced == type) return;
var index = types.IndexOf(typeReplaced);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeReplaced); // in case removing type changed index
types.Insert(index, type);
types.Remove(typeReplaced);
});
return This;
}
}
}