AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ReadOnlyHashSet.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections;
6 using System.Collections.Generic;
7 using UnityEngine;
8 
9 namespace HoloToolkit.Unity
10 {
16  public class ReadOnlyHashSet<TElement> :
17  ICollection<TElement>,
18  IEnumerable<TElement>,
19  IEnumerable
20  {
21  private readonly HashSet<TElement> underlyingSet;
22 
23  public ReadOnlyHashSet(HashSet<TElement> underlyingSet)
24  {
25  Debug.Assert(underlyingSet != null, "underlyingSet cannot be null.");
26 
27  this.underlyingSet = underlyingSet;
28  }
29 
30  public int Count
31  {
32  get { return underlyingSet.Count; }
33  }
34 
35  bool ICollection<TElement>.IsReadOnly
36  {
37  get { return true; }
38  }
39 
40  void ICollection<TElement>.Add(TElement item)
41  {
42  throw NewWriteDeniedException();
43  }
44 
45  void ICollection<TElement>.Clear()
46  {
47  throw NewWriteDeniedException();
48  }
49 
50  public bool Contains(TElement item)
51  {
52  return underlyingSet.Contains(item);
53  }
54 
55  public void CopyTo(TElement[] array, int arrayIndex)
56  {
57  underlyingSet.CopyTo(array, arrayIndex);
58  }
59 
60  public IEnumerator<TElement> GetEnumerator()
61  {
62  return underlyingSet.GetEnumerator();
63  }
64 
65  bool ICollection<TElement>.Remove(TElement item)
66  {
67  throw NewWriteDeniedException();
68  }
69 
70  IEnumerator IEnumerable.GetEnumerator()
71  {
72  return underlyingSet.GetEnumerator();
73  }
74 
75  private NotSupportedException NewWriteDeniedException()
76  {
77  return new NotSupportedException("ReadOnlyHashSet<TElement> is not directly writable.");
78  }
79  }
80 
82  {
83  public static ReadOnlyHashSet<TElement> AsReadOnly<TElement>(this HashSet<TElement> set)
84  {
85  return new ReadOnlyHashSet<TElement>(set);
86  }
87  }
88 }
void CopyTo(TElement[] array, int arrayIndex)
IEnumerator< TElement > GetEnumerator()
A wrapper for HashSet<T> that doesn&#39;t allow modification of the set. This is useful for handing out r...
ReadOnlyHashSet(HashSet< TElement > underlyingSet)