Fixed issue #5. Fixed issue #48. Fixed issue #58. Fixed issue #194. Fixed issue #195. Fixed issue #201. Fixed issue #137. git-svn-id: https://duplicati.googlecode.com/svn/trunk@400 59da171f-624f-0410-aa54-27559c288bec
192 lines
6.4 KiB
C#
192 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Duplicati.Library.Core
|
|
{
|
|
//TODO: Remove this delegate if we upgrade to .Net Framework 3.5, as it is built in
|
|
public delegate TResult Func<T, TResult>(T input);
|
|
|
|
/// <summary>
|
|
/// This class puts a filter over an IEnumerable,
|
|
/// giving the illusion that the sequence only contains certain values.
|
|
/// This is semantically the same as the FindAll function,
|
|
/// but does not create a copy of the source
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of enumerable to filter</typeparam>
|
|
public class PlugableEnumerable<T> : IEnumerable<T>
|
|
{
|
|
/// <summary>
|
|
/// An optional call back function that determines if an element is visible or not
|
|
/// </summary>
|
|
public Predicate<T> Predicate { get; set; }
|
|
|
|
/// <summary>
|
|
/// An optional callback method that converts the element returned by the Current method of the enumerator
|
|
/// </summary>
|
|
public Func<T, T> Converter { get; set; }
|
|
|
|
/// <summary>
|
|
/// The inner list being wrapped
|
|
/// </summary>
|
|
public IEnumerable<T> WrappedList { get; set; }
|
|
|
|
/// <summary>
|
|
/// Constructs a new filtered enumeration
|
|
/// </summary>
|
|
/// <param name="predicate">The filter function</param>
|
|
/// <param name="converter">The converter function</param>
|
|
/// <param name="wrappedlist">The sequence to filter</param>
|
|
public PlugableEnumerable(Predicate<T> predicate, Func<T, T> converter, IEnumerable<T> wrappedlist)
|
|
{
|
|
Predicate = predicate;
|
|
Converter = converter;
|
|
WrappedList = wrappedlist;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a new filtered enumeration
|
|
/// </summary>
|
|
/// <param name="sequence">The sequence of items to filter</param>
|
|
public PlugableEnumerable(IEnumerable<T> sequence)
|
|
{
|
|
WrappedList = sequence;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a new filtered enumeration
|
|
/// </summary>
|
|
/// <param name="predicate">The filter function</param>
|
|
/// <param name="converter">The converter function</param>
|
|
/// <param name="wrappedlist">The sequence to filter</param>
|
|
public PlugableEnumerable(Predicate<T> predicate, Func<T, T> converter)
|
|
{
|
|
Predicate = predicate;
|
|
Converter = converter;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Applies the filtering from this instance to another list
|
|
/// </summary>
|
|
/// <param name="sequence">The sequence to wrap</param>
|
|
/// <returns>A filtered sequence</returns>
|
|
public PlugableEnumerable<T> WrapList(IEnumerable<T> sequence)
|
|
{
|
|
return new PlugableEnumerable<T>(Predicate, Converter, sequence);
|
|
}
|
|
|
|
#region IEnumerable<T> Members
|
|
|
|
/// <summary>
|
|
/// Gets a new enumerator
|
|
/// </summary>
|
|
/// <returns>An enumerator</returns>
|
|
public IEnumerator<T> GetEnumerator()
|
|
{
|
|
return new FilterableIEnumerableEnumerator(Predicate, Converter, WrappedList.GetEnumerator());
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IEnumerable Members
|
|
|
|
/// <summary>
|
|
/// Gets a new enumerator
|
|
/// </summary>
|
|
/// <returns>An enumerator</returns>
|
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
|
{
|
|
return new FilterableIEnumerableEnumerator(Predicate, Converter, WrappedList.GetEnumerator());
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Constructs an enumerable that wraps another enumerator, filtering elements in the process
|
|
/// </summary>
|
|
public class FilterableIEnumerableEnumerator : IEnumerator<T>
|
|
{
|
|
private IEnumerator<T> m_parent;
|
|
private Predicate<T> m_predicate;
|
|
private Func<T, T> m_converter;
|
|
|
|
/// <summary>
|
|
/// Constructs a new enumerator wrapping the parent enumerator
|
|
/// </summary>
|
|
/// <param name="predicate">The predicate each item must match</param>
|
|
/// <param name="parent">The parent enumerator</param>
|
|
public FilterableIEnumerableEnumerator(Predicate<T> predicate, Func<T, T> converter, IEnumerator<T> parent)
|
|
{
|
|
m_predicate = predicate;
|
|
m_converter = converter;
|
|
m_parent = parent;
|
|
}
|
|
|
|
#region IEnumerator<T> Members
|
|
|
|
/// <summary>
|
|
/// Gets the current element
|
|
/// </summary>
|
|
public T Current
|
|
{
|
|
get
|
|
{
|
|
if (m_converter == null)
|
|
return m_parent.Current;
|
|
else
|
|
return m_converter(m_parent.Current);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose()
|
|
{
|
|
m_parent.Dispose();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IEnumerator Members
|
|
|
|
/// <summary>
|
|
/// Gets the current element
|
|
/// </summary>
|
|
object System.Collections.IEnumerator.Current
|
|
{
|
|
get { return this.Current; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Advances the enumerator one element, this may be multiple elements, depending on the filter
|
|
/// </summary>
|
|
/// <returns>True if the operation suceeded, false if there are no more elements</returns>
|
|
public bool MoveNext()
|
|
{
|
|
if (m_predicate == null)
|
|
return m_parent.MoveNext();
|
|
|
|
bool more;
|
|
while (more = m_parent.MoveNext())
|
|
if (m_predicate(m_parent.Current))
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resest the enumerator to its start position, this is directly mapped to the wrapping enumerator
|
|
/// </summary>
|
|
public void Reset()
|
|
{
|
|
m_parent.Reset();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
}
|