Beispiel #1
0
        public void RemoveAnnotation_KeyDoesNotExist()
        {
            var annotatable = new AnnotatableObject();
            var key         = new AnnotationKey <int>();

            Assert.False(annotatable.RemoveAnnotation(key));
        }
Beispiel #2
0
        public void GetAnnotation_KeyDoesNotExist()
        {
            var annotatable = new AnnotatableObject();
            var key         = new AnnotationKey <int>();

            Assert.Equal(1, annotatable.GetAnnotation(key, () => 1));
        }
Beispiel #3
0
        public void GetAnnotation_KeyDoesNotExist_NullInitializer()
        {
            var annotatable = new AnnotatableObject();
            var key         = new AnnotationKey <int>();

            Assert.Equal(0, annotatable.GetAnnotation(key));
        }
Beispiel #4
0
        public void RemoveAnnotation_KeyExists()
        {
            var annotatable = new AnnotatableObject();
            var key         = new AnnotationKey <int>();

            annotatable.SetAnnotation(key, 10);

            Assert.True(annotatable.RemoveAnnotation(key));
        }
Beispiel #5
0
        public void SetAnnotation_GetAnnotation()
        {
            var annotatable = new AnnotatableObject();
            var key         = new AnnotationKey <int>();

            annotatable.SetAnnotation(key, 10);

            Assert.Equal(10, annotatable.GetAnnotation(key));
        }
Beispiel #6
0
        /// <inheritdoc/>
        public bool RemoveAnnotation <TAnnotation>(AnnotationKey <TAnnotation> key)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            lock (_lock)
            {
                return(_annotations.Remove(key));
            }
        }
Beispiel #7
0
        /// <inheritdoc/>
        public void SetAnnotation <TAnnotation>(AnnotationKey <TAnnotation> key, TAnnotation value)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            lock (_lock)
            {
                _annotations[key] = value;
            }
        }
Beispiel #8
0
        /// <inheritdoc/>
        public TAnnotation GetAnnotation <TAnnotation>(
            AnnotationKey <TAnnotation> key, Func <TAnnotation>?initializer = null)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            lock (_lock)
            {
                if (!_annotations.TryGetValue(key, out var annotation))
                {
                    annotation = initializer is null ? default : initializer();
                                 _annotations[key] = annotation;
                }

                Debug.Assert(annotation is TAnnotation);
                return((TAnnotation)annotation);
            }
        }