Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/IFreezable.cs @ 18242

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

#2077: created branch and added first version

File size: 3.4 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.Generic;
21using System.Collections.ObjectModel;
22using System.Linq;
23
24namespace ICSharpCode.AvalonEdit.Utils
25{
26  interface IFreezable
27  {
28    /// <summary>
29    /// Gets if this instance is frozen. Frozen instances are immutable and thus thread-safe.
30    /// </summary>
31    bool IsFrozen { get; }
32   
33    /// <summary>
34    /// Freezes this instance.
35    /// </summary>
36    void Freeze();
37  }
38 
39  static class FreezableHelper
40  {
41    public static void ThrowIfFrozen(IFreezable freezable)
42    {
43      if (freezable.IsFrozen)
44        throw new InvalidOperationException("Cannot mutate frozen " + freezable.GetType().Name);
45    }
46   
47    public static IList<T> FreezeListAndElements<T>(IList<T> list)
48    {
49      if (list != null) {
50        foreach (T item in list)
51          Freeze(item);
52      }
53      return FreezeList(list);
54    }
55   
56    public static IList<T> FreezeList<T>(IList<T> list)
57    {
58      if (list == null || list.Count == 0)
59        return Empty<T>.Array;
60      if (list.IsReadOnly) {
61        // If the list is already read-only, return it directly.
62        // This is important, otherwise we might undo the effects of interning.
63        return list;
64      } else {
65        return new ReadOnlyCollection<T>(list.ToArray());
66      }
67    }
68   
69    public static void Freeze(object item)
70    {
71      IFreezable f = item as IFreezable;
72      if (f != null)
73        f.Freeze();
74    }
75   
76    public static T FreezeAndReturn<T>(T item) where T : IFreezable
77    {
78      item.Freeze();
79      return item;
80    }
81   
82    /// <summary>
83    /// If the item is not frozen, this method creates and returns a frozen clone.
84    /// If the item is already frozen, it is returned without creating a clone.
85    /// </summary>
86    public static T GetFrozenClone<T>(T item) where T : IFreezable, ICloneable
87    {
88      if (!item.IsFrozen) {
89        item = (T)item.Clone();
90        item.Freeze();
91      }
92      return item;
93    }
94  }
95
96  [Serializable]
97  abstract class AbstractFreezable : IFreezable
98  {
99    bool isFrozen;
100   
101    /// <summary>
102    /// Gets if this instance is frozen. Frozen instances are immutable and thus thread-safe.
103    /// </summary>
104    public bool IsFrozen {
105      get { return isFrozen; }
106    }
107   
108    /// <summary>
109    /// Freezes this instance.
110    /// </summary>
111    public void Freeze()
112    {
113      if (!isFrozen) {
114        FreezeInternal();
115        isFrozen = true;
116      }
117    }
118   
119    protected virtual void FreezeInternal()
120    {
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.