Lists.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. namespace Google.ProtocolBuffers.Collections {
  19. /// <summary>
  20. /// Utility non-generic class for calling into Lists{T} using type inference.
  21. /// </summary>
  22. public static class Lists {
  23. /// <summary>
  24. /// Returns a read-only view of the specified list.
  25. /// </summary>
  26. public static IList<T> AsReadOnly<T>(IList<T> list) {
  27. return Lists<T>.AsReadOnly(list);
  28. }
  29. }
  30. /// <summary>
  31. /// Utility class for dealing with lists.
  32. /// </summary>
  33. public static class Lists<T> {
  34. static readonly ReadOnlyCollection<T> empty = new ReadOnlyCollection<T>(new T[0]);
  35. /// <summary>
  36. /// Returns an immutable empty list.
  37. /// </summary>
  38. public static ReadOnlyCollection<T> Empty {
  39. get { return empty; }
  40. }
  41. /// <summary>
  42. /// Returns either the original reference if it's already read-only,
  43. /// or a new ReadOnlyCollection wrapping the original list.
  44. /// </summary>
  45. public static IList<T> AsReadOnly(IList<T> list) {
  46. return list.IsReadOnly ? list : new ReadOnlyCollection<T>(list);
  47. }
  48. }
  49. }