Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="keepAlive"></param>
        /// <returns></returns>
        public ITextUndoHistory RegisterHistory(object context, bool keepAlive)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (_strongContextMapping.TryGetValue(context, out var result))
            {
                if (!keepAlive)
                {
                    _strongContextMapping.Remove(context);
                    _weakContextMapping.Add(new KeyWeakReference(context), result);
                }

                return(result);
            }

            var reference = new KeyWeakReference(context);

            if (_weakContextMapping.TryGetValue(reference, out result))
            {
                if (keepAlive)
                {
                    _weakContextMapping.Remove(reference);
                    _strongContextMapping.Add(context, result);
                }

                return(result);
            }

            result = new MockTextUndoHistory(this);
            _histories.Add(result, 1);

            if (keepAlive)
            {
                _strongContextMapping.Add(context, result);
            }
            else
            {
                _weakContextMapping.Add(reference, result);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public bool Equals(KeyWeakReference other)
        {
            if (other == null)
            {
                return(false);
            }

            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            if (IsAlive && other.IsAlive)
            {
                return(Equals(Target, other.Target));
            }

            return(!IsAlive && !other.IsAlive && HashCode == other.HashCode);
        }