Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CollectionsHelper.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Collections;
24 using System.Collections.Generic;
25 using System.Linq;
26 using System.Text;
27 
28 namespace Lucene.Net.Index.Memory
29 {
30  internal static class CollectionsHelper<T>
31  {
32  private static readonly T[] EmptyArray = new T[0];
33 
34  /// <summary>
35  /// Returns an empty list of type T
36  /// </summary>
37  public static IList<T> EmptyList()
38  {
39  return EmptyArray;
40  }
41  }
42 
43  public static class CollectionsExtensions
44  {
45  public static ICollection<T> AsReadOnly<T>(this ICollection<T> collection)
46  {
47  return new ReadOnlyCollection<T>(collection);
48  }
49 
50  private sealed class ReadOnlyCollection<T> : ICollection<T>
51  {
52  private readonly ICollection<T> _other;
53 
54  public ReadOnlyCollection(ICollection<T> other)
55  {
56  _other = other;
57  }
58 
59  public IEnumerator<T> GetEnumerator()
60  {
61  return _other.GetEnumerator();
62  }
63 
64  IEnumerator IEnumerable.GetEnumerator()
65  {
66  return GetEnumerator();
67  }
68 
69  public void Add(T item)
70  {
71  throw new NotSupportedException("Collection is read only!");
72  }
73 
74  public void Clear()
75  {
76  throw new NotSupportedException("Collection is read only!");
77  }
78 
79  public bool Contains(T item)
80  {
81  return _other.Contains(item);
82  }
83 
84  public void CopyTo(T[] array, int arrayIndex)
85  {
86  _other.CopyTo(array, arrayIndex);
87  }
88 
89  public bool Remove(T item)
90  {
91  throw new NotSupportedException("Collection is read only!");
92  }
93 
94  public int Count
95  {
96  get { return _other.Count; }
97  }
98 
99  public bool IsReadOnly
100  {
101  get { return true; }
102  }
103  }
104  }
105 }