using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Kreta.Framework.Collections.Generic
{
///
/// Entitás lista felülete.
///
public class EntityCollection : IEntityCollection, IEntityCollection, IList
where EntityType : Kreta.Framework.Entities.Entity
{
protected List m_Entities = new List();
public EntityCollection()
{
}
///
/// Külön bejárat a DA számára
///
/// Entitás példány
protected internal void InternalAdd(EntityType entity)
{
m_Entities.Add(entity);
}
#region Internal operations
protected virtual void DoRemove(EntityType entity)
{
if (entity.ID == -1)
{
m_Entities.Remove(entity);
}
else
{
m_Entities.RemoveAll(e => e.ID == entity.ID);
}
}
protected virtual void DoAdd(EntityType entity)
{
if (!Contains(entity))
{
m_Entities.Add(entity);
}
}
protected virtual void DoClear()
{
m_Entities.Clear();
}
#endregion
#region IList Members
int IList.IndexOf(EntityType item)
{
return IndexOf(item);
}
void IList.Insert(int index, EntityType item)
{
throw new NotSupportedException("IList.Insert");
}
void IList.RemoveAt(int index)
{
DoRemove(m_Entities[index]);
}
EntityType IList.this[int index]
{
get
{
return m_Entities[index];
}
set
{
throw new NotSupportedException("EntityType IList.this[int index] setter");
}
}
#endregion
#region ICollection Members
void ICollection.Add(EntityType item)
{
DoAdd(item);
}
void ICollection.Clear()
{
DoClear();
}
bool ICollection.Contains(EntityType item)
{
return Contains(item);
}
void ICollection.CopyTo(EntityType[] array, int arrayIndex)
{
m_Entities.CopyTo(array, arrayIndex);
}
int ICollection.Count
{
get { return m_Entities.Count; }
}
bool ICollection.IsReadOnly
{
get { return false; }
}
bool ICollection.Remove(EntityType item)
{
if (Contains(item))
{
DoRemove(item);
return true;
}
return false;
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return m_Entities.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return m_Entities.GetEnumerator();
}
#endregion
#region IList Members
int IList.Add(object value)
{
DoAdd((EntityType)value);
return IndexOf((EntityType)value);
}
void IList.Clear()
{
DoClear();
}
bool IList.Contains(object value)
{
return Contains((EntityType)value);
}
int IList.IndexOf(object value)
{
return IndexOf((EntityType)value);
}
void IList.Insert(int index, object value)
{
throw new NotSupportedException("void IList.Insert(int index, object value)");
}
bool IList.IsFixedSize
{
get { return false; }
}
bool IList.IsReadOnly
{
get { return false; }
}
void IList.Remove(object value)
{
DoRemove((EntityType)value);
}
void IList.RemoveAt(int index)
{
DoRemove(m_Entities[index]);
}
object IList.this[int index]
{
get
{
return m_Entities[index];
}
set
{
throw new NotSupportedException("object IList.this[int index] setter");
}
}
#endregion
#region ICollection Members
void ICollection.CopyTo(Array array, int index)
{
((ICollection)m_Entities).CopyTo(array, index);
}
int ICollection.Count
{
get { return m_Entities.Count; }
}
bool ICollection.IsSynchronized
{
get { return ((ICollection)m_Entities).IsSynchronized; }
}
object ICollection.SyncRoot
{
get { return ((ICollection)m_Entities).SyncRoot; }
}
#endregion
#region IEntityCollection Members
public int Count
{
get { return m_Entities.Count; }
}
public EntityType this[int index] => m_Entities[index];
public int Add(EntityType entity)
{
DoAdd(entity);
return IndexOf(entity);
}
public bool Contains(EntityType entity)
{
if (entity.ID == -1)
{
return m_Entities.Contains(entity);
}
return m_Entities.Any(e => e.ID == entity.ID);
}
public void DeleteAll(bool logikai = true)
{
foreach (EntityType entity in m_Entities)
{
entity.Delete(false, logikai);
}
m_Entities.Clear();
}
public void DeleteMany(IList ids, bool logikai = true)
{
foreach (EntityType entity in m_Entities)
{
if (!ids.Contains(entity.ID))
{
continue;
}
entity.Delete(false, logikai);
}
m_Entities.Clear();
}
public void DeleteAll(bool runHandler, bool logikai = true)
{
foreach (EntityType entity in m_Entities)
{
entity.Delete(runHandler, logikai);
}
m_Entities.Clear();
}
public void CascadeDeleteAll(bool logikai = true)
{
foreach (EntityType entity in m_Entities)
{
entity.CascadeDelete(logikai);
}
m_Entities.Clear();
}
public void CascadeDeleteAll(bool runHandler, bool logikai = true)
{
foreach (EntityType entity in m_Entities)
{
entity.CascadeDelete(runHandler, logikai);
}
m_Entities.Clear();
}
public EntityType FindEntityById(int entityId)
{
foreach (EntityType entity in m_Entities)
{
if (entity.ID == entityId)
{
return entity;
}
}
return null;
}
public void Clear()
{
DoClear();
}
public int IndexOf(EntityType entity)
{
if (entity.ID == -1)
{
return m_Entities.IndexOf(entity);
}
var found = m_Entities.Find(e => e.ID == entity.ID);
return found == null ? -1 : m_Entities.IndexOf(found);
}
#endregion
#region IEntityCollection Members
int IEntityCollection.Count
{
get { return Count; }
}
Kreta.Framework.Entities.Entity IEntityCollection.this[int index]
{
get { return this[index]; }
}
int IEntityCollection.Add(Kreta.Framework.Entities.Entity entity) => Add((EntityType)entity);
bool IEntityCollection.Contains(Kreta.Framework.Entities.Entity entity)
{
return Contains((EntityType)entity);
}
void IEntityCollection.DeleteAll()
{
DeleteAll();
}
void IEntityCollection.DeleteAll(bool runHandler)
{
DeleteAll(runHandler);
}
void IEntityCollection.CascadeDeleteAll()
{
CascadeDeleteAll();
}
void IEntityCollection.CascadeDeleteAll(bool runHandler)
{
CascadeDeleteAll(runHandler);
}
Kreta.Framework.Entities.Entity IEntityCollection.FindEntityById(int entityId)
{
return FindEntityById(entityId);
}
void IEntityCollection.Clear()
{
Clear();
}
int IEntityCollection.IndexOf(Kreta.Framework.Entities.Entity entity)
{
return IndexOf((EntityType)entity);
}
#endregion
}
}