Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/ObserveAddRemoveCollection.cs @ 12062

Last change on this file since 12062 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 2.8 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.ObjectModel;
21
22namespace ICSharpCode.AvalonEdit.Utils
23{
24  /// <summary>
25  /// A collection where adding and removing items causes a callback.
26  /// It is valid for the onAdd callback to throw an exception - this will prevent the new item from
27  /// being added to the collection.
28  /// </summary>
29  sealed class ObserveAddRemoveCollection<T> : Collection<T>
30  {
31    readonly Action<T> onAdd, onRemove;
32   
33    /// <summary>
34    /// Creates a new ObserveAddRemoveCollection using the specified callbacks.
35    /// </summary>
36    public ObserveAddRemoveCollection(Action<T> onAdd, Action<T> onRemove)
37    {
38      if (onAdd == null)
39        throw new ArgumentNullException("onAdd");
40      if (onRemove == null)
41        throw new ArgumentNullException("onRemove");
42      this.onAdd = onAdd;
43      this.onRemove = onRemove;
44    }
45   
46    /// <inheritdoc/>
47    protected override void ClearItems()
48    {
49      if (onRemove != null) {
50        foreach (T val in this)
51          onRemove(val);
52      }
53      base.ClearItems();
54    }
55   
56    /// <inheritdoc/>
57    protected override void InsertItem(int index, T item)
58    {
59      if (onAdd != null)
60        onAdd(item);
61      base.InsertItem(index, item);
62    }
63   
64    /// <inheritdoc/>
65    protected override void RemoveItem(int index)
66    {
67      if (onRemove != null)
68        onRemove(this[index]);
69      base.RemoveItem(index);
70    }
71   
72    /// <inheritdoc/>
73    protected override void SetItem(int index, T item)
74    {
75      if (onRemove != null)
76        onRemove(this[index]);
77      try {
78        if (onAdd != null)
79          onAdd(item);
80      } catch {
81        // When adding the new item fails, just remove the old one
82        // (we cannot keep the old item since we already successfully called onRemove for it)
83        base.RemoveAt(index);
84        throw;
85      }
86      base.SetItem(index, item);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.