diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs index d37225b..a9066aa 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs @@ -58,20 +58,31 @@ namespace ProjectLiner.CollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { - if (Count >= _maxCount) - throw new CollectionOverflowException(Count); + if (Count == _maxCount) throw new CollectionOverflowException(); + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyExistsException(obj); + } + } _collection.Add(obj); return Count; } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - if (Count == _maxCount) - throw new CollectionOverflowException(Count); - if (position < 0 || position > Count) - throw new PositionOutOfCollectionException(position); ; + if (Count == _maxCount) throw new CollectionOverflowException(); + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); + if (comparer != null) + { + if (_collection.Contains(obj, comparer)) + { + throw new ObjectAlreadyExistsException(obj); + } + } _collection.Insert(position, obj); return position; } diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs index 99f8a17..e8b40f5 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs @@ -61,8 +61,18 @@ namespace ProjectLiner.CollectionGenericObjects return _collection[position]; } - public int Insert(T obj) + public int Insert(T obj, IEqualityComparer? comparer = null) { + if (comparer != null) + { + foreach (T? i in _collection) + { + if (comparer.Equals(i, obj)) + { + throw new ObjectAlreadyExistsException(i); + } + } + } for (int i = 0; i < Count; i++) { if (_collection[i] == null) @@ -71,20 +81,29 @@ namespace ProjectLiner.CollectionGenericObjects return i; } } - throw new CollectionOverflowException(Count); + throw new CollectionOverflowException(); } - public int Insert(T obj, int position) + public int Insert(T obj, int position, IEqualityComparer? comparer = null) { - if (position >= Count || position < 0) - throw new PositionOutOfCollectionException(position); + if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); + + if (comparer != null) + { + foreach (T? i in _collection) + { + if (comparer.Equals(i, obj)) + { + throw new ObjectAlreadyExistsException(i); + } + } + } if (_collection[position] == null) { _collection[position] = obj; return position; } - int temp = position + 1; while (temp < Count) { @@ -93,21 +112,19 @@ namespace ProjectLiner.CollectionGenericObjects _collection[temp] = obj; return temp; } - temp++; + ++temp; } - temp = position - 1; - while (temp > 0) + while (temp >= 0) { if (_collection[temp] == null) { _collection[temp] = obj; return temp; } - temp--; + --temp; } - - throw new CollectionOverflowException(Count); + throw new CollectionOverflowException(); } public T? Remove(int position) diff --git a/ProjectLiner/ProjectLiner/Exceptions/ObjectAlreadyExistsException.cs b/ProjectLiner/ProjectLiner/Exceptions/ObjectAlreadyExistsException.cs new file mode 100644 index 0000000..8cbb498 --- /dev/null +++ b/ProjectLiner/ProjectLiner/Exceptions/ObjectAlreadyExistsException.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectLiner.Exceptions; + +/// +/// Класс, описывающий ошибку, что в коллекции уже есть такой элемент +/// +[Serializable] +public class ObjectAlreadyExistsException : ApplicationException +{ + public ObjectAlreadyExistsException(object i) : base("В коллекции уже есть такой элемент " + i) { } + public ObjectAlreadyExistsException() : base() { } + public ObjectAlreadyExistsException(string message) : base(message) { } + public ObjectAlreadyExistsException(string message, Exception exception) : base(message, exception) + { } + protected ObjectAlreadyExistsException(SerializationInfo info, StreamingContext context) : base(info, context) { } +}