Files
Umbraco-CMS/src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs

324 lines
12 KiB
C#
Raw Normal View History

2016-08-16 10:25:19 +02:00
using System;
2016-08-19 11:41:39 +02:00
using System.Collections.Generic;
2016-08-16 10:25:19 +02:00
2017-05-30 15:46:25 +02:00
namespace Umbraco.Core.Composing
2016-08-16 10:25:19 +02:00
{
/// <summary>
/// Implements an ordered collection builder.
/// </summary>
/// <typeparam name="TBuilder">The type of the builder.</typeparam>
/// <typeparam name="TCollection">The type of the collection.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
2016-07-29 10:58:57 +02:00
public abstract class OrderedCollectionBuilderBase<TBuilder, TCollection, TItem> : CollectionBuilderBase<TBuilder, TCollection, TItem>
2016-08-16 10:25:19 +02:00
where TBuilder : OrderedCollectionBuilderBase<TBuilder, TCollection, TItem>
where TCollection : IBuilderCollection<TItem>
{
2017-05-30 15:33:13 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="OrderedCollectionBuilderBase{TBuilder,TCollection,TItem}"/> class.
/// </summary>
/// <param name="container"></param>
protected OrderedCollectionBuilderBase(IContainer container)
2016-08-16 10:25:19 +02:00
: base (container)
{ }
protected abstract TBuilder This { get; }
2016-09-23 20:18:25 +02:00
/// <summary>
/// Clears all types in the collection.
/// </summary>
/// <returns>The buidler.</returns>
public TBuilder Clear()
{
Configure(types => types.Clear());
return This;
}
2016-08-16 10:25:19 +02:00
/// <summary>
/// Appends a type to the collection.
/// </summary>
/// <typeparam name="T">The type to append.</typeparam>
/// <returns>The builder.</returns>
public TBuilder Append<T>()
where T : TItem
{
Configure(types =>
{
var type = typeof (T);
if (types.Contains(type)) types.Remove(type);
types.Add(type);
});
return This;
}
2016-09-23 20:18:25 +02:00
/// <summary>
/// Appends a type to the collection.
/// </summary>
/// <param name="type">The type to append.</param>
/// <returns>The builder.</returns>
public TBuilder Append(Type type)
{
Configure(types =>
{
EnsureType(type, "register");
if (types.Contains(type)) types.Remove(type);
types.Add(type);
});
return This;
}
2016-08-19 11:41:39 +02:00
/// <summary>
/// Appends types to the collections.
/// </summary>
/// <param name="types">The types to append.</param>
/// <returns>The builder.</returns>
public TBuilder Append(IEnumerable<Type> types)
{
Configure(list =>
{
foreach (var type in types)
{
2016-08-22 11:40:45 +02:00
// would be detected by CollectionBuilderBase when registering, anyways, but let's fail fast
2016-09-23 20:18:25 +02:00
EnsureType(type, "register");
2016-08-19 11:41:39 +02:00
if (list.Contains(type)) list.Remove(type);
list.Add(type);
}
});
return This;
2016-08-25 15:09:51 +02:00
}
/// <summary>
/// Appends types to the collections.
/// </summary>
/// <param name="types">The types to append.</param>
/// <returns>The builder.</returns>
public TBuilder Append(Func<IContainer, IEnumerable<Type>> types)
2016-08-25 15:09:51 +02:00
{
Configure(list =>
{
foreach (var type in types(Container))
{
// would be detected by CollectionBuilderBase when registering, anyways, but let's fail fast
2016-09-23 20:18:25 +02:00
EnsureType(type, "register");
2016-08-25 15:09:51 +02:00
if (list.Contains(type)) list.Remove(type);
list.Add(type);
}
});
return This;
2016-08-19 11:41:39 +02:00
}
2016-08-16 10:25:19 +02:00
/// <summary>
/// Appends a type after another type.
/// </summary>
/// <typeparam name="TAfter">The other type.</typeparam>
/// <typeparam name="T">The type to append.</typeparam>
/// <returns>The builder.</returns>
/// <remarks>Throws if both types are identical, or if the other type does not already belong to the collection.</remarks>
public TBuilder AppendAfter<TAfter, T>()
where TAfter : TItem
where T : TItem
{
Configure(types =>
{
var typeAfter = typeof (TAfter);
var type = typeof(T);
if (typeAfter == type) throw new InvalidOperationException();
var index = types.IndexOf(typeAfter);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeAfter); // in case removing type changed index
types.Insert(index + 1, type);
});
return This;
}
/// <summary>
/// Inserts a type into the collection.
/// </summary>
/// <typeparam name="T">The type to insert.</typeparam>
2016-08-22 11:40:45 +02:00
/// <param name="index">The optional index.</param>
2016-08-16 10:25:19 +02:00
/// <returns>The builder.</returns>
2016-08-22 11:40:45 +02:00
/// <remarks>Throws if the index is out of range.</remarks>
public TBuilder Insert<T>(int index = 0)
2016-08-16 10:25:19 +02:00
where T : TItem
{
Configure(types =>
{
var type = typeof (T);
if (types.Contains(type)) types.Remove(type);
2016-08-22 11:40:45 +02:00
types.Insert(index, type);
2016-08-16 10:25:19 +02:00
});
return This;
}
2016-09-23 20:18:25 +02:00
/// <summary>
/// Inserts a type into the collection.
/// </summary>
/// <param name="type">The type to insert.</param>
/// <returns>The builder.</returns>
/// <remarks>Throws if the index is out of range.</remarks>
public TBuilder Insert(Type type)
{
return Insert(0, type);
}
/// <summary>
/// Inserts a type into the collection.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="type">The type to insert.</param>
/// <returns>The builder.</returns>
/// <remarks>Throws if the index is out of range.</remarks>
public TBuilder Insert(int index, Type type)
{
Configure(types =>
{
EnsureType(type, "register");
if (types.Contains(type)) types.Remove(type);
types.Insert(index, type);
});
return This;
}
2016-08-16 10:25:19 +02:00
/// <summary>
/// Inserts a type before another type.
/// </summary>
/// <typeparam name="TBefore">The other type.</typeparam>
/// <typeparam name="T">The type to insert.</typeparam>
/// <returns>The builder.</returns>
/// <remarks>Throws if both types are identical, or if the other type does not already belong to the collection.</remarks>
public TBuilder InsertBefore<TBefore, T>()
where TBefore : TItem
where T : TItem
{
Configure(types =>
{
var typeBefore = typeof(TBefore);
var type = typeof(T);
if (typeBefore == type) throw new InvalidOperationException();
var index = types.IndexOf(typeBefore);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeBefore); // in case removing type changed index
types.Insert(index, type);
});
return This;
}
2016-09-23 20:18:25 +02:00
/// <summary>
/// Inserts a type before another type.
/// </summary>
/// <param name="typeBefore">The other type.</param>
/// <param name="type">The type to insert.</param>
/// <returns>The builder.</returns>
/// <remarks>Throws if both types are identical, or if the other type does not already belong to the collection.</remarks>
public TBuilder InsertBefore(Type typeBefore, Type type)
{
Configure(types =>
{
EnsureType(typeBefore, "find");
EnsureType(type, "register");
if (typeBefore == type) throw new InvalidOperationException();
var index = types.IndexOf(typeBefore);
if (index < 0) throw new InvalidOperationException();
if (types.Contains(type)) types.Remove(type);
index = types.IndexOf(typeBefore); // in case removing type changed index
types.Insert(index, type);
});
return This;
}
2016-08-16 10:25:19 +02:00
/// <summary>
/// Removes a type from the collection.
/// </summary>
/// <typeparam name="T">The type to remove.</typeparam>
/// <returns>The builder.</returns>
public TBuilder Remove<T>()
where T : TItem
{
Configure(types =>
{
var type = typeof (T);
if (types.Contains(type)) types.Remove(type);
});
return This;
}
2016-09-23 20:18:25 +02:00
/// <summary>
/// Removes a type from the collection.
/// </summary>
/// <param name="type">The type to remove.</param>
/// <returns>The builder.</returns>
public TBuilder Remove(Type type)
{
Configure(types =>
{
EnsureType(type, "remove");
if (types.Contains(type)) types.Remove(type);
});
return This;
}
2016-08-16 10:25:19 +02:00
/// <summary>
/// Replaces a type in the collection.
/// </summary>
/// <typeparam name="TReplaced">The type to replace.</typeparam>
/// <typeparam name="T">The type to insert.</typeparam>
/// <returns>The builder.</returns>
/// <remarks>Throws if the type to replace does not already belong to the collection.</remarks>
public TBuilder Replace<TReplaced, T>()
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;
}
2016-08-22 11:40:45 +02:00
/// <summary>
2016-09-23 20:18:25 +02:00
/// Replaces a type in the collection.
2016-08-22 11:40:45 +02:00
/// </summary>
2016-09-23 20:18:25 +02:00
/// <param name="typeReplaced">The type to replace.</param>
/// <param name="type">The type to insert.</param>
/// <returns>The builder.</returns>
/// <remarks>Throws if the type to replace does not already belong to the collection.</remarks>
public TBuilder Replace(Type typeReplaced, Type type)
2016-08-22 11:40:45 +02:00
{
2016-09-23 20:18:25 +02:00
Configure(types =>
{
EnsureType(typeReplaced, "find");
EnsureType(type, "register");
2016-08-22 11:40:45 +02:00
2016-09-23 20:18:25 +02:00
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);
});
2016-08-22 11:40:45 +02:00
return This;
}
2016-08-16 10:25:19 +02:00
}
}