PageRenderTime 22ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/System/Collections/ArrayList.cs

https://gitlab.com/Pfhoenix/api
C# | 640 lines | 406 code | 56 blank | 178 comment | 114 complexity | eba8df485e4cd212003deab3580fb283 MD5 | raw file
  1. // Copyright (C) 2014 dot42
  2. //
  3. // Original filename: ArrayList.cs
  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 Dot42;
  17. using Dot42.Collections;
  18. using Java.Util;
  19. namespace System.Collections
  20. {
  21. public class ArrayList : IList
  22. {
  23. private readonly IList<object> list;
  24. private readonly bool isFixedSize;
  25. private readonly bool isReadOnly;
  26. private readonly bool isSynchronized;
  27. /// <summary>
  28. /// Default ctor
  29. /// </summary>
  30. public ArrayList() :
  31. this(new ArrayList<object>(), false, false, false)
  32. {
  33. }
  34. /// <summary>
  35. /// Clone ctor
  36. /// </summary>
  37. public ArrayList(ICollection source)
  38. : this(new ArrayList<object>((source != null) ? source.Count : 0), false, false, false)
  39. {
  40. if (source == null)
  41. throw new ArgumentNullException();
  42. foreach (var item in source)
  43. {
  44. list.Add(item);
  45. }
  46. }
  47. /// <summary>
  48. /// Default ctor with specified initial capacity.
  49. /// </summary>
  50. public ArrayList(int capacity) :
  51. this(new ArrayList<object>(capacity), false, false, false)
  52. {
  53. }
  54. /// <summary>
  55. /// Private ctor
  56. /// </summary>
  57. private ArrayList(IList<object> list, bool isFixedSize, bool isReadOnly, bool isSynchronized)
  58. {
  59. this.list = list;
  60. this.isFixedSize = isFixedSize;
  61. this.isReadOnly = isReadOnly;
  62. this.isSynchronized = isSynchronized;
  63. }
  64. /// <summary>
  65. /// Gets/sets the capacity of this list.
  66. /// </summary>
  67. public virtual int Capacity
  68. {
  69. get { return list.Size(); }
  70. set
  71. {
  72. /* ignore */
  73. }
  74. }
  75. /// <summary>
  76. /// Gets the number of items in this list.
  77. /// </summary>
  78. public int Count
  79. {
  80. get { return list.Count; }
  81. }
  82. /// <summary>
  83. /// Does this list have a fixed size?
  84. /// </summary>
  85. public virtual bool IsFixedSize
  86. {
  87. get { return isFixedSize; }
  88. }
  89. /// <summary>
  90. /// Is this list readonly?
  91. /// </summary>
  92. public virtual bool IsReadOnly
  93. {
  94. get { return isReadOnly; }
  95. }
  96. /// <summary>
  97. /// Is this list synchronized?
  98. /// </summary>
  99. public virtual bool IsSynchronized
  100. {
  101. get { return isSynchronized; }
  102. }
  103. /// <summary>
  104. /// Is this list readonly?
  105. /// </summary>
  106. public virtual object this[int index]
  107. {
  108. get
  109. {
  110. if ((index < 0) || (index >= list.Size()))
  111. throw new ArgumentOutOfRangeException();
  112. return list[index];
  113. }
  114. set
  115. {
  116. if (isReadOnly)
  117. throw new NotSupportedException();
  118. if ((index < 0) || (index >= list.Size()))
  119. throw new ArgumentOutOfRangeException();
  120. list[index] = value;
  121. }
  122. }
  123. /// <summary>
  124. /// Gets an object that can be used to synchronized access to this list.
  125. /// </summary>
  126. public virtual object SyncRoot
  127. {
  128. get { return this; }
  129. }
  130. /// <summary>
  131. /// Copy all elements into the given array starting at the given index (into the array).
  132. /// </summary>
  133. public virtual void CopyTo(Array array)
  134. {
  135. CopyTo(0, array, 0, list.Count);
  136. }
  137. /// <summary>
  138. /// Copy all elements into the given array starting at the given index (into the array).
  139. /// </summary>
  140. public virtual void CopyTo(Array array, int index)
  141. {
  142. CopyTo(0, array, index, list.Count);
  143. }
  144. /// <summary>
  145. /// Copy all elements into the given array starting at the given index (into the array).
  146. /// </summary>
  147. public virtual void CopyTo(int index, Array array, int arrayIndex, int count)
  148. {
  149. if (array == null)
  150. throw new ArgumentNullException();
  151. if ((index < 0) || (arrayIndex < 0) || (count < 0))
  152. throw new ArgumentOutOfRangeException();
  153. var size = list.Size();
  154. var available = array.Length- arrayIndex;
  155. if ((index > size) || (count > size) || (index + count > size) || (count > available))
  156. throw new ArgumentException();
  157. while (count > 0)
  158. {
  159. array.SetValue(list[index++], arrayIndex++);
  160. count--;
  161. }
  162. }
  163. /// <summary>
  164. /// Add an object to the end of this list
  165. /// </summary>
  166. /// <param name="value"></param>
  167. public virtual int Add(object value)
  168. {
  169. if (isFixedSize || isReadOnly)
  170. throw new NotSupportedException();
  171. list.Add(value);
  172. return list.Count - 1;
  173. }
  174. /// <summary>
  175. /// Add all elements in the given collection to the end of this list
  176. /// </summary>
  177. /// <param name="value"></param>
  178. public virtual void AddRange(ICollection c)
  179. {
  180. if (isFixedSize || isReadOnly)
  181. throw new NotSupportedException();
  182. if (c == null)
  183. throw new ArgumentNullException();
  184. foreach (var i in c)
  185. {
  186. list.Add(i);
  187. }
  188. }
  189. [NotImplemented]
  190. public virtual int BinarySearch(object element)
  191. {
  192. return Java.Util.Collections.BinarySearch(list, element, Comparer.Default);
  193. }
  194. [NotImplemented]
  195. public virtual int BinarySearch(object element, IComparer comparer)
  196. {
  197. comparer = comparer ?? Comparer.Default;
  198. return Java.Util.Collections.BinarySearch(list, element, comparer);
  199. }
  200. [NotImplemented]
  201. public virtual int BinarySearch(int index, int count, object element, IComparer comparer)
  202. {
  203. if ((index < 0) || (count < 0))
  204. throw new ArgumentOutOfRangeException();
  205. var size = list.Size();
  206. if ((index >= size) || (count > size) || (index + count > size))
  207. throw new ArgumentException();
  208. comparer = comparer ?? Comparer.Default;
  209. return Java.Util.Collections.BinarySearch(list.SubList(index, index + count), element, comparer);
  210. }
  211. /// <summary>
  212. /// Remove all elements
  213. /// </summary>
  214. public virtual void Clear()
  215. {
  216. if (isFixedSize || isReadOnly)
  217. throw new NotSupportedException();
  218. list.Clear();
  219. }
  220. /// <summary>
  221. /// Create a shallow clone of this list.
  222. /// </summary>
  223. /// <returns></returns>
  224. public virtual object Clone()
  225. {
  226. return new ArrayList(this);
  227. }
  228. /// <summary>
  229. /// Returns an enumerator for the entire list.
  230. /// </summary>
  231. /// <returns></returns>
  232. public virtual IEnumerator GetEnumerator()
  233. {
  234. return new IteratorWrapper<object>(list);
  235. }
  236. /// <summary>
  237. /// Returns an enumerator for the given range of this list.
  238. /// </summary>
  239. public virtual IEnumerator GetEnumerator(int index, int count)
  240. {
  241. if ((index < 0) || (count < 0))
  242. throw new ArgumentOutOfRangeException();
  243. var size = list.Size();
  244. if ((index >= size) || (count > size) || (index + count > size))
  245. throw new ArgumentException();
  246. return new IteratorWrapper<object>(list.SubList(index, index + count));
  247. }
  248. /// <summary>
  249. /// Is the given object part of this list?
  250. /// </summary>
  251. /// <param name="value"></param>
  252. /// <returns></returns>
  253. public virtual bool Contains(object value)
  254. {
  255. return list.Contains(value);
  256. }
  257. /// <summary>
  258. /// Create a list that is a subset of this list.
  259. /// </summary>
  260. public virtual ArrayList GetRange(int index, int count)
  261. {
  262. if ((index < 0) || (count < 0))
  263. throw new ArgumentOutOfRangeException();
  264. if (index + count >= list.Size())
  265. throw new ArgumentException();
  266. return new ArrayList(list.SubList(index, index + count), isFixedSize, isReadOnly, isSynchronized);
  267. }
  268. /// <summary>
  269. /// Gets the first index of the given element/
  270. /// </summary>
  271. /// <returns>-1 if not found</returns>
  272. public virtual int IndexOf(object element)
  273. {
  274. return list.IndexOf(element);
  275. }
  276. /// <summary>
  277. /// Gets the first index of the given element/
  278. /// </summary>
  279. /// <returns>-1 if not found</returns>
  280. public virtual int IndexOf(object element, int startIndex)
  281. {
  282. return IndexOf(element, startIndex, list.Count - startIndex);
  283. }
  284. /// <summary>
  285. /// Gets the first index of the given element/
  286. /// </summary>
  287. /// <returns>-1 if not found</returns>
  288. public virtual int IndexOf(object element, int startIndex, int count)
  289. {
  290. if ((startIndex < 0) || (count < 0) || (startIndex >= list.Size()))
  291. throw new ArgumentOutOfRangeException();
  292. var rc = list.SubList(startIndex, startIndex + count).IndexOf(element);
  293. return (rc >= 0) ? rc + startIndex : rc;
  294. }
  295. /// <summary>
  296. /// Insert the given item at the given index.
  297. /// </summary>
  298. public virtual void Insert(int index, object element)
  299. {
  300. if (isFixedSize || isReadOnly)
  301. throw new NotSupportedException();
  302. if ((index < 0) || (index > list.Size()))
  303. throw new ArgumentOutOfRangeException();
  304. list.Add(index, element);
  305. }
  306. /// <summary>
  307. /// Insert the given items at the given index.
  308. /// </summary>
  309. public virtual void InsertRange(int index, ICollection c)
  310. {
  311. if (isFixedSize || isReadOnly)
  312. throw new NotSupportedException();
  313. if (c == null)
  314. throw new ArgumentNullException();
  315. if ((index < 0) || (index > list.Size()))
  316. throw new ArgumentOutOfRangeException();
  317. foreach (var item in c)
  318. {
  319. list.Add(index++, item);
  320. }
  321. }
  322. /// <summary>
  323. /// Gets the last index of the given element/
  324. /// </summary>
  325. /// <returns>-1 if not found</returns>
  326. public virtual int LastIndexOf(object element)
  327. {
  328. return list.LastIndexOf(element);
  329. }
  330. /// <summary>
  331. /// Gets the last index of the given element/
  332. /// </summary>
  333. /// <returns>-1 if not found</returns>
  334. public virtual int LastIndexOf(object element, int startIndex)
  335. {
  336. if ((startIndex < 0) || (startIndex >= list.Size()))
  337. throw new ArgumentOutOfRangeException();
  338. return list.SubList(0, startIndex).LastIndexOf(element);
  339. }
  340. /// <summary>
  341. /// Gets the last index of the given element/
  342. /// </summary>
  343. /// <returns>-1 if not found</returns>
  344. public virtual int LastIndexOf(object element, int startIndex, int count)
  345. {
  346. var size = list.Size();
  347. if ((startIndex < 0) || (count < 0) || (startIndex >= size))
  348. throw new ArgumentOutOfRangeException();
  349. var start = (startIndex - count) + 1;
  350. if (start < 0)
  351. throw new ArgumentOutOfRangeException();
  352. var end = startIndex + 1;
  353. var rc = list.SubList(start, end).LastIndexOf(element);
  354. return (rc >= 0) ? start + rc : rc;
  355. }
  356. /// <summary>
  357. /// Remove the given item
  358. /// </summary>
  359. public virtual void Remove(object element)
  360. {
  361. if (isFixedSize || isReadOnly)
  362. throw new NotSupportedException();
  363. list.Remove(element);
  364. }
  365. /// <summary>
  366. /// Remove the item at the given index.
  367. /// </summary>
  368. public virtual void RemoveAt(int index)
  369. {
  370. if (isFixedSize || isReadOnly)
  371. throw new NotSupportedException();
  372. if ((index < 0) || (index >= list.Size()))
  373. throw new ArgumentOutOfRangeException();
  374. list.Remove(index);
  375. }
  376. /// <summary>
  377. /// Remove a range of items from list starting at the given the given index.
  378. /// </summary>
  379. public virtual void RemoveRange(int index, int count)
  380. {
  381. if (isFixedSize || isReadOnly)
  382. throw new NotSupportedException();
  383. if ((index < 0) || (count < 0))
  384. throw new ArgumentOutOfRangeException();
  385. var size = list.Size();
  386. if ((index >= size) || (count > size) || (index + count > size))
  387. throw new ArgumentException();
  388. while (count > 0)
  389. {
  390. list.Remove(index);
  391. count--;
  392. }
  393. }
  394. /// <summary>
  395. /// Reverse the order of items in the entire list.
  396. /// </summary>
  397. public virtual void Reverse()
  398. {
  399. if (isReadOnly)
  400. throw new NotSupportedException();
  401. var size = list.Size();
  402. if (size > 1)
  403. {
  404. Java.Util.Collections.Reverse(list);
  405. }
  406. }
  407. /// <summary>
  408. /// Reverse the order of items in the given range.
  409. /// </summary>
  410. public virtual void Reverse(int index, int count)
  411. {
  412. if (isReadOnly)
  413. throw new NotSupportedException();
  414. if ((index < 0) || (count < 0))
  415. throw new ArgumentOutOfRangeException();
  416. var size = list.Size();
  417. if ((index >= size) || (count > size) || (index + count > size))
  418. throw new ArgumentException();
  419. if (count > 1)
  420. {
  421. Java.Util.Collections.Reverse(list.SubList(index, index + count));
  422. }
  423. }
  424. /// <summary>
  425. /// Copies the elements in the collection over the items in this list starting at the given list index.
  426. /// </summary>
  427. public virtual void SetRange(int index, ICollection c)
  428. {
  429. if (isReadOnly)
  430. throw new NotSupportedException();
  431. if (c == null)
  432. throw new ArgumentNullException();
  433. var count = c.Count;
  434. if ((index < 0) || (index + count > list.Size()))
  435. throw new ArgumentOutOfRangeException();
  436. foreach (var item in c)
  437. {
  438. list[index++] = item;
  439. }
  440. }
  441. /// <summary>
  442. /// Sort the elements in the entire list.
  443. /// </summary>
  444. public virtual void Sort()
  445. {
  446. if (isReadOnly)
  447. throw new NotSupportedException();
  448. Java.Util.Collections.Sort(list);
  449. }
  450. /// <summary>
  451. /// Sort the elements in the entire list.
  452. /// </summary>
  453. public virtual void Sort(IComparer comparer)
  454. {
  455. if (isReadOnly)
  456. throw new NotSupportedException();
  457. comparer = comparer ?? Comparer.Default;
  458. Java.Util.Collections.Sort(list, comparer);
  459. }
  460. /// <summary>
  461. /// Sort the elements in the given range.
  462. /// </summary>
  463. public virtual void Sort(int index, int count, IComparer comparer)
  464. {
  465. if (isReadOnly)
  466. throw new NotSupportedException();
  467. if ((index < 0) || (count < 0))
  468. throw new ArgumentOutOfRangeException();
  469. var size = list.Size();
  470. if ((index > size) || (count > size) || (index + count > size))
  471. throw new ArgumentException();
  472. comparer = comparer ?? Comparer.Default;
  473. Java.Util.Collections.Sort(list.SubList(index, index + count), comparer);
  474. }
  475. /// <summary>
  476. /// Copy all items of the list into a new array.
  477. /// </summary>
  478. public virtual object[] ToArray()
  479. {
  480. return list.ToArray();
  481. }
  482. /// <summary>
  483. /// Copy all items of the list into a new array.
  484. /// </summary>
  485. public virtual Array ToArray(Type elementType)
  486. {
  487. if (elementType == null)
  488. throw new ArgumentNullException();
  489. var arr = (Array)Java.Lang.Reflect.Array.NewInstance(elementType, list.Count);
  490. CopyTo(arr);
  491. return arr;
  492. }
  493. /// <summary>
  494. /// Set the capacity of the list to the number of items in the list.
  495. /// </summary>
  496. public virtual void TrimToSize()
  497. {
  498. if (isFixedSize || isReadOnly)
  499. throw new NotSupportedException();
  500. }
  501. /// <summary>
  502. /// Create an ArrayList wrapper around the given list.
  503. /// </summary>
  504. [NotImplemented]
  505. public static ArrayList Adapter(IList list)
  506. {
  507. if (list == null)
  508. throw new ArgumentNullException();
  509. throw new NotImplementedException("System.Collections.ArrayList.Adapter");
  510. }
  511. /// <summary>
  512. /// Create a list wrapper with a fixed size that is backed by the given list.
  513. /// </summary>
  514. public static ArrayList FixedSize(ArrayList list)
  515. {
  516. if (list == null)
  517. throw new ArgumentNullException();
  518. return new ArrayList(list.list, true, list.isReadOnly, list.isSynchronized);
  519. }
  520. /// <summary>
  521. /// Create a list wrapper with a fixed size that is backed by the given list.
  522. /// </summary>
  523. [NotImplemented]
  524. public static ArrayList FixedSize(IList list)
  525. {
  526. if (list == null)
  527. throw new ArgumentNullException();
  528. throw new NotImplementedException("System.Collections.ArrayList.FixedSize");
  529. }
  530. /// <summary>
  531. /// Create a readonly list wrapper that is backed by the given list.
  532. /// </summary>
  533. public static ArrayList ReadOnly(ArrayList list)
  534. {
  535. if (list == null)
  536. throw new ArgumentNullException();
  537. return new ArrayList(list.list, list.isFixedSize, true, list.isSynchronized);
  538. }
  539. /// <summary>
  540. /// Create a readonly list wrapper that is backed by the given list.
  541. /// </summary>
  542. [NotImplemented]
  543. public static ArrayList ReadOnly(IList list)
  544. {
  545. if (list == null)
  546. throw new ArgumentNullException();
  547. throw new NotImplementedException("System.Collections.ArrayList.ReadOnly");
  548. }
  549. /// <summary>
  550. /// Return an ArrayList filled count element of the given value.
  551. /// </summary>
  552. public static ArrayList Repeat(object value, int count)
  553. {
  554. if (count < 0)
  555. throw new ArgumentOutOfRangeException();
  556. var result = new ArrayList(count);
  557. while (count > 0)
  558. {
  559. result.list.Add(value);
  560. count--;
  561. }
  562. return result;
  563. }
  564. /// <summary>
  565. /// Create a thread safe list wrapper that is backed by the given list.
  566. /// </summary>
  567. public static ArrayList Synchronized(ArrayList list)
  568. {
  569. if (list == null)
  570. throw new ArgumentNullException();
  571. return new ArrayList(Java.Util.Collections.SynchronizedList(list.list), list.isFixedSize, list.isReadOnly, true);
  572. }
  573. /// <summary>
  574. /// Create a thread safe list wrapper that is backed by the given list.
  575. /// </summary>
  576. [NotImplemented]
  577. public static ArrayList Synchronized(IList list)
  578. {
  579. if (list == null)
  580. throw new ArgumentNullException();
  581. throw new NotImplementedException("System.Collections.ArrayList.Synchronized");
  582. }
  583. }
  584. }