• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 using System;
34 using System.Collections.Generic;
35 using System.Linq;
36 
37 using ICollection = System.Collections.ICollection;
38 using IEnumerable = System.Collections.IEnumerable;
39 using IList = System.Collections.IList;
40 
41 namespace Antlr.Runtime.JavaExtensions
42 {
43     public static class ListExtensions
44     {
45 #if DEBUG
46         [Obsolete]
add( this IList list, object value )47         public static bool add( this IList list, object value )
48         {
49             int count = list.Count;
50             list.Add( value );
51             return list.Count == count + 1;
52         }
53 
54         [Obsolete]
add( this ICollection<T> list, T value )55         public static void add<T>( this ICollection<T> list, T value )
56         {
57             list.Add( value );
58         }
59 
60         [Obsolete]
add( this List<T> list, T value )61         public static void add<T>( this List<T> list, T value )
62         {
63             list.Add( value );
64         }
65 
66         [Obsolete]
add( this IList list, int index, object value )67         public static void add( this IList list, int index, object value )
68         {
69             list.Insert( index, value );
70         }
71 
72         [Obsolete]
addAll( this List<object> list, IEnumerable items )73         public static void addAll( this List<object> list, IEnumerable items )
74         {
75             list.AddRange( items.Cast<object>() );
76         }
77 #endif
78 
addAll( this IList list, IEnumerable items )79         public static void addAll( this IList list, IEnumerable items )
80         {
81             foreach ( object item in items )
82                 list.Add( item );
83         }
84 
addAll( this ICollection<T> list, IEnumerable<T> items )85         public static void addAll<T>( this ICollection<T> list, IEnumerable<T> items )
86         {
87             foreach ( T item in items )
88                 list.Add( item );
89         }
90 
91 #if DEBUG
92         [Obsolete]
addElement( this List<object> list, object value )93         public static void addElement( this List<object> list, object value )
94         {
95             list.Add( value );
96         }
97 
98         [Obsolete]
clear( this IList list )99         public static void clear( this IList list )
100         {
101             list.Clear();
102         }
103 
104         [Obsolete]
contains( this IList list, object value )105         public static bool contains( this IList list, object value )
106         {
107             return list.Contains( value );
108         }
109 
110         [Obsolete]
contains( this ICollection<T> list, T value )111         public static bool contains<T>( this ICollection<T> list, T value )
112         {
113             return list.Contains( value );
114         }
115 
116         [Obsolete]
elementAt( this IList<T> list, int index )117         public static T elementAt<T>( this IList<T> list, int index )
118         {
119             return list[index];
120         }
121 
122         [Obsolete]
get( this IList list, int index )123         public static object get( this IList list, int index )
124         {
125             return list[index];
126         }
127 
128         [Obsolete]
get( this IList<T> list, int index )129         public static T get<T>( this IList<T> list, int index )
130         {
131             return list[index];
132         }
133 
134         // disambiguate
135         [Obsolete]
get( this List<T> list, int index )136         public static T get<T>( this List<T> list, int index )
137         {
138             return list[index];
139         }
140 
141         [Obsolete]
remove( this IList list, int index )142         public static object remove( this IList list, int index )
143         {
144             object o = list[index];
145             list.RemoveAt( index );
146             return o;
147         }
148 
149         [Obsolete]
remove( this IList<T> list, T item )150         public static void remove<T>( this IList<T> list, T item )
151         {
152             list.Remove( item );
153         }
154 
155         [Obsolete]
set( this IList list, int index, object value )156         public static void set( this IList list, int index, object value )
157         {
158             list[index] = value;
159         }
160 
161         [Obsolete]
set( this IList<T> list, int index, T value )162         public static void set<T>( this IList<T> list, int index, T value )
163         {
164             list[index] = value;
165         }
166 
167         [Obsolete]
set( this List<T> list, int index, T value )168         public static void set<T>( this List<T> list, int index, T value )
169         {
170             list[index] = value;
171         }
172 #endif
173 
setSize( this List<T> list, int size )174         public static void setSize<T>( this List<T> list, int size )
175         {
176             if ( list.Count < size )
177             {
178                 list.AddRange( Enumerable.Repeat( default( T ), size - list.Count ) );
179             }
180             else if ( list.Count > size )
181             {
182                 list.RemoveRange(size, list.Count - size);
183             }
184         }
185 
186 #if DEBUG
187         [Obsolete]
size( this ICollection collection )188         public static int size( this ICollection collection )
189         {
190             return collection.Count;
191         }
192 
193         [Obsolete]
size( this ICollection<T> collection )194         public static int size<T>( this ICollection<T> collection )
195         {
196             return collection.Count;
197         }
198 
199         [Obsolete]
size( this List<T> list )200         public static int size<T>( this List<T> list )
201         {
202             return list.Count;
203         }
204 #endif
205 
subList( this IList list, int fromIndex, int toIndex )206         public static IList subList( this IList list, int fromIndex, int toIndex )
207         {
208             return new SubList( list, fromIndex, toIndex );
209             //return
210             //    list
211             //    .Cast<object>()
212             //    .Skip( fromIndex )
213             //    .Take( toIndex - fromIndex + 1 )
214             //    .ToList();
215         }
216 
subList( this IList<T> list, int fromIndex, int toIndex )217         public static IList<T> subList<T>( this IList<T> list, int fromIndex, int toIndex )
218         {
219             return new SubList<T>( list, fromIndex, toIndex );
220             //return
221             //    list
222             //    .Skip( fromIndex )
223             //    .Take( toIndex - fromIndex + 1 )
224             //    .ToList();
225         }
226 
subList( this List<T> list, int fromIndex, int toIndex )227         public static IList<T> subList<T>( this List<T> list, int fromIndex, int toIndex )
228         {
229             return new SubList<T>( list, fromIndex, toIndex );
230             //return
231             //    list
232             //    .Skip( fromIndex )
233             //    .Take( toIndex - fromIndex + 1 )
234             //    .ToList();
235         }
236     }
237 }
238