using System;
using System.Collections;
using System.Collections.Generic;
namespace umbraco.DataLayer.Utility.Table
{
///
/// Default implementation of the interface.
///
public class DefaultTable : ITable
{
#region Private Fields
/// List of table fields.
private List m_Fields;
#endregion
#region Public Constructors
///
/// Initializes a new instance of the class.
///
/// The table name.
public DefaultTable(string name)
{
if (String.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
Name = name;
m_Fields = new List();
}
#endregion
#region ITable Members
///
/// Gets the name of the table.
///
/// The name of the table.
public virtual string Name { get; protected set; }
///
/// Adds the field to the table.
///
/// The field.
public virtual void AddField(IField field)
{
if (field == null)
throw new ArgumentNullException("field");
if (FindField(field.Name) != null)
throw new ArgumentException(String.Format("A field named '{0}' already exists in table '{1}'.",
field.Name, this.Name));
m_Fields.Add(field);
}
///
/// Creates a new field.
///
/// The name.
/// The field data type.
/// A new field.
public virtual IField CreateField(string name, Type dataType)
{
return CreateField(name, dataType, 0);
}
///
/// Creates a new field.
///
/// The name.
/// The field data type.
/// The properties.
/// A new field.
public virtual IField CreateField(string name, Type dataType, FieldProperties properties)
{
return CreateField(name, dataType, 0, properties);
}
///
/// Creates a new field.
///
/// The name.
/// The field data type.
/// The size.
/// A new field.
public virtual IField CreateField(string name, Type dataType, int size)
{
return CreateField(name, dataType, size, FieldProperties.None);
}
///
/// Creates a new field.
///
/// The name.
/// The field data type.
/// The size.
/// The properties.
/// A new field.
public virtual IField CreateField(string name, Type dataType, int size, FieldProperties properties)
{
return new DefaultField(name, dataType, size, properties);
}
///
/// Finds the field with the specified name.
///
/// The name.
/// The field, or null if a field with the specified name doesn't exist.
public IField FindField(string name)
{
return FindField(f => f.Name == name);
}
///
/// Finds the first field satisfiying the matcher.
///
/// The matcher.
///
/// The first field found, or null if no field matches.
///
public IField FindField(Predicate matcher)
{
foreach (IField field in this)
if (matcher(field))
return field;
return null;
}
///
/// Finds all fields satisfiying the matcher.
///
/// The matcher.
/// A list of all matching fields.
public IList FindFields(Predicate matcher)
{
List matchingFields = new List();
foreach (IField field in this)
if (matcher(field))
matchingFields.Add(field);
return matchingFields;
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator that iterates through the collection.
///
///
/// A that can be used to iterate through the collection.
///
public virtual IEnumerator GetEnumerator()
{
return m_Fields.GetEnumerator();
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a collection.
///
///
/// An object that can be used to iterate through the collection.
///
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region object Members
///
/// Returns a that represents the current .
///
///
/// A that represents the current .
///
public override string ToString()
{
return String.Format("{0} ({1} fields)", Name, m_Fields.Count);
}
#endregion
}
}