Exemplo n.º 1
0
        public virtual Result <bool> MoveAuto(int fromIndex, ICollection toCol, int moveAmount, CollectionContext context = null)
        {
            if (isReadOnly)
            {
                return(new Result <bool>(false, Errors.CollectionIsReadOnly));
            }

            if (moveAmount > GetAmount(fromIndex))
            {
                return(new Result <bool>(false, Errors.CollectionDoesNotContainItem));
            }

            if (this == toCol)
            {
                // Moving to a new auto. slot inside the same collection
                return(true);
            }

            context = context ?? new CollectionContext();
            var canAdd = toCol.CanAddBoxed(this[fromIndex], moveAmount, context);

            if (canAdd.result == false)
            {
                return(canAdd);
            }

            var canSet = CanSet(fromIndex, this[fromIndex], GetAmount(fromIndex) - moveAmount);

            if (canSet.result == false)
            {
                return(canSet);
            }

            var moveItem = this[fromIndex];

            if (moveAmount < GetAmount(fromIndex))
            {
                // Not moving entire stack, clone object.
                moveItem = CreateElementClone(this[fromIndex]);
            }

            toCol.AddBoxed(moveItem, moveAmount);
            Set(fromIndex, this[fromIndex], GetAmount(fromIndex) - moveAmount, context);

            return(true);
        }