Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/TypeSystem/Implementation/AbstractFreezable.cs @ 11700

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

#2077: created branch and added first version

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