aboutsummaryrefslogtreecommitdiff
path: root/csharp/ProtocolBuffers/Collections/Lists.cs
blob: 0fba3edb43f0d8292d22f15f85efb91351e650b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Google.ProtocolBuffers.Collections {

  public static class Lists {

    public static IList<T> AsReadOnly<T>(IList<T> list) {
      return Lists<T>.AsReadOnly(list);
    }
  }

  /// <summary>
  /// Utilities class for dealing with lists.
  /// </summary>
  public static class Lists<T> {

    static readonly ReadOnlyCollection<T> empty = new ReadOnlyCollection<T>(new T[0]);

    /// <summary>
    /// Returns an immutable empty list.
    /// </summary>
    public static ReadOnlyCollection<T> Empty {
      get { return empty; }
    }

    /// <summary>
    /// Returns either the original reference if it's already read-only,
    /// or a new ReadOnlyCollection wrapping the original list.
    /// </summary>
    public static IList<T> AsReadOnly(IList<T> list) {
      return list.IsReadOnly ? list : new ReadOnlyCollection<T>(list);
    }
  }
}