public override Difference CompareTo(Element oldElement)
        {
            if (oldElement == null)
                throw new ArgumentNullException("oldElement");
            if (!oldElement.Identifier.Equals(this.Identifier))
                throw new MergeException("Cannot compare elements that does not share the same identifier.");

            if (oldElement is ValueElement)
            {
                return new ValueDifference(
                            this.Identifier,
                            OperationOnParent.Removed,
                            ((ValueElement)oldElement).Value,
                            null);
            }
            else if (oldElement is NodeElement)
            {
                return new NodeDifference(
                            this.Identifier,
                            OperationOnParent.Removed,
                            null);
            }
            else
            {
                throw new MergeException(string.Format("Cannot compare a {0} to a {1}.", oldElement.GetType().Name, this.GetType().Name));
            }
        }
Beispiel #2
0
        public override Difference CompareTo(Element oldElement)
        {
            if (oldElement == null)
                throw new ArgumentNullException("oldElement");
            if (!oldElement.Identifier.Equals(this.Identifier))
                throw new MergeException("Cannot compare elements that does not share the same identifier.");

            OperationOnParent operationOnParent;
            ElementHashList oldChilds;
            if (oldElement is EmptyElement)
            {
                operationOnParent = OperationOnParent.Added;
                oldChilds = new ElementHashList();
            }
            else if (oldElement is NodeElement)
            {
                operationOnParent = OperationOnParent.Modified;
                oldChilds = ((NodeElement)oldElement).Childs;
            }
            else
            {
                throw new MergeException(string.Format("Cannot compare a {0} to a {1}.", oldElement.GetType().Name, this.GetType().Name));
            }

            var differences = new List<Difference>();
            var newChilds = this.Childs;
            foreach (var oldChild in oldChilds)
            {
                var newChild = newChilds.Contains(oldChild.Identifier)
                            ? newChilds[oldChild.Identifier]
                            : oldChild.Identifier.CreateEmptyElement();

                var difference = newChild.CompareTo(oldChild);
                if (difference != null)
                {
                    differences.Add(difference);
                }
            }
            foreach (var newChild in newChilds)
            {
                if (!oldChilds.Contains(newChild.Identifier))
                {
                    Element oldChild = newChild.Identifier.CreateEmptyElement();
                    differences.Add(newChild.CompareTo(oldChild));
                }
            }

            return differences.Count > 0
                        ? new NodeDifference(this.Identifier, operationOnParent, differences)
                        : null;
        }
 public abstract Difference CompareTo(Element oldElement);