PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/CSharp3/Sources/Antlr3.Runtime.JavaExtensions/ListExtensions.cs

https://bitbucket.org/jwalton/antlr3
C# | 237 lines | 160 code | 29 blank | 48 comment | 4 complexity | 6d1c7f1735d670d3405f4a6d98dd499f MD5 | raw file
  1. /*
  2. * [The "BSD licence"]
  3. * Copyright (c) 2005-2008 Terence Parr
  4. * All rights reserved.
  5. *
  6. * Conversion to C#:
  7. * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Linq;
  35. using ICollection = System.Collections.ICollection;
  36. using IEnumerable = System.Collections.IEnumerable;
  37. using IList = System.Collections.IList;
  38. namespace Antlr.Runtime.JavaExtensions
  39. {
  40. public static class ListExtensions
  41. {
  42. #if DEBUG
  43. [Obsolete]
  44. public static bool add( this IList list, object value )
  45. {
  46. int count = list.Count;
  47. list.Add( value );
  48. return list.Count == count + 1;
  49. }
  50. [Obsolete]
  51. public static void add<T>( this ICollection<T> list, T value )
  52. {
  53. list.Add( value );
  54. }
  55. [Obsolete]
  56. public static void add<T>( this List<T> list, T value )
  57. {
  58. list.Add( value );
  59. }
  60. [Obsolete]
  61. public static void add( this IList list, int index, object value )
  62. {
  63. list.Insert( index, value );
  64. }
  65. [Obsolete]
  66. public static void addAll( this List<object> list, IEnumerable items )
  67. {
  68. list.AddRange( items.Cast<object>() );
  69. }
  70. #endif
  71. public static void addAll( this IList list, IEnumerable items )
  72. {
  73. foreach ( object item in items )
  74. list.Add( item );
  75. }
  76. public static void addAll<T>( this ICollection<T> list, IEnumerable<T> items )
  77. {
  78. foreach ( T item in items )
  79. list.Add( item );
  80. }
  81. #if DEBUG
  82. [Obsolete]
  83. public static void addElement( this List<object> list, object value )
  84. {
  85. list.Add( value );
  86. }
  87. [Obsolete]
  88. public static void clear( this IList list )
  89. {
  90. list.Clear();
  91. }
  92. [Obsolete]
  93. public static bool contains( this IList list, object value )
  94. {
  95. return list.Contains( value );
  96. }
  97. [Obsolete]
  98. public static bool contains<T>( this ICollection<T> list, T value )
  99. {
  100. return list.Contains( value );
  101. }
  102. [Obsolete]
  103. public static T elementAt<T>( this IList<T> list, int index )
  104. {
  105. return list[index];
  106. }
  107. [Obsolete]
  108. public static object get( this IList list, int index )
  109. {
  110. return list[index];
  111. }
  112. [Obsolete]
  113. public static T get<T>( this IList<T> list, int index )
  114. {
  115. return list[index];
  116. }
  117. // disambiguate
  118. [Obsolete]
  119. public static T get<T>( this List<T> list, int index )
  120. {
  121. return list[index];
  122. }
  123. [Obsolete]
  124. public static object remove( this IList list, int index )
  125. {
  126. object o = list[index];
  127. list.RemoveAt( index );
  128. return o;
  129. }
  130. [Obsolete]
  131. public static void remove<T>( this IList<T> list, T item )
  132. {
  133. list.Remove( item );
  134. }
  135. [Obsolete]
  136. public static void set( this IList list, int index, object value )
  137. {
  138. list[index] = value;
  139. }
  140. [Obsolete]
  141. public static void set<T>( this IList<T> list, int index, T value )
  142. {
  143. list[index] = value;
  144. }
  145. [Obsolete]
  146. public static void set<T>( this List<T> list, int index, T value )
  147. {
  148. list[index] = value;
  149. }
  150. #endif
  151. public static void setSize<T>( this List<T> list, int size )
  152. {
  153. if ( list.Count < size )
  154. {
  155. list.AddRange( Enumerable.Repeat( default( T ), size - list.Count ) );
  156. }
  157. else if ( list.Count > size )
  158. {
  159. list.RemoveRange(size, list.Count - size);
  160. }
  161. }
  162. #if DEBUG
  163. [Obsolete]
  164. public static int size( this ICollection collection )
  165. {
  166. return collection.Count;
  167. }
  168. [Obsolete]
  169. public static int size<T>( this ICollection<T> collection )
  170. {
  171. return collection.Count;
  172. }
  173. [Obsolete]
  174. public static int size<T>( this List<T> list )
  175. {
  176. return list.Count;
  177. }
  178. #endif
  179. public static IList subList( this IList list, int fromIndex, int toIndex )
  180. {
  181. return new SubList( list, fromIndex, toIndex );
  182. //return
  183. // list
  184. // .Cast<object>()
  185. // .Skip( fromIndex )
  186. // .Take( toIndex - fromIndex + 1 )
  187. // .ToList();
  188. }
  189. public static IList<T> subList<T>( this IList<T> list, int fromIndex, int toIndex )
  190. {
  191. return new SubList<T>( list, fromIndex, toIndex );
  192. //return
  193. // list
  194. // .Skip( fromIndex )
  195. // .Take( toIndex - fromIndex + 1 )
  196. // .ToList();
  197. }
  198. public static IList<T> subList<T>( this List<T> list, int fromIndex, int toIndex )
  199. {
  200. return new SubList<T>( list, fromIndex, toIndex );
  201. //return
  202. // list
  203. // .Skip( fromIndex )
  204. // .Take( toIndex - fromIndex + 1 )
  205. // .ToList();
  206. }
  207. }
  208. }