PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/Java.NET/JavApi/java.util.Collections.cs

https://github.com/gadfly/nofs
C# | 1706 lines | 1261 code | 248 blank | 197 comment | 49 complexity | 35300a92b3df10b159ef7b343162eb9b MD5 | raw file
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. */
  15. using System;
  16. using java = biz.ritter.javapi;
  17. namespace biz.ritter.javapi.util
  18. {
  19. /// <summary>
  20. /// <c>Collections</c> contains static methods which operate on <c>Collection</c> classes.
  21. /// </summary>
  22. public class Collections
  23. {
  24. /**
  25. * Returns a wrapper on the specified sorted map which synchronizes all
  26. * access to the sorted map.
  27. *
  28. * @param map
  29. * the sorted map to wrap in a synchronized sorted map.
  30. * @return a synchronized sorted map.
  31. */
  32. public static SortedMap<K, V> synchronizedSortedMap<K, V>(
  33. SortedMap<K, V> map) {
  34. if (map == null) {
  35. throw new java.lang.NullPointerException();
  36. }
  37. return new SynchronizedSortedMap<K, V>(map);
  38. }
  39. [Serializable]
  40. class SynchronizedSortedMap<K, V> : SynchronizedMap<K, V>
  41. , SortedMap<K, V> {
  42. private static readonly long serialVersionUID = -8798146769416483793L;
  43. private readonly SortedMap<K, V> sm;
  44. internal SynchronizedSortedMap(SortedMap<K, V> map) :base(map){
  45. sm = map;
  46. }
  47. internal SynchronizedSortedMap(SortedMap<K, V> map, Object mutex) :base(map,mutex){
  48. sm = map;
  49. }
  50. public Comparator<K> comparator() {
  51. lock (mutex) {
  52. return sm.comparator();
  53. }
  54. }
  55. public K firstKey() {
  56. lock (mutex) {
  57. return sm.firstKey();
  58. }
  59. }
  60. public SortedMap<K, V> headMap(K endKey) {
  61. lock (mutex) {
  62. return new SynchronizedSortedMap<K, V>(sm.headMap(endKey),
  63. mutex);
  64. }
  65. }
  66. public K lastKey() {
  67. lock (mutex) {
  68. return sm.lastKey();
  69. }
  70. }
  71. public SortedMap<K, V> subMap(K startKey, K endKey) {
  72. lock (mutex) {
  73. return new SynchronizedSortedMap<K, V>(sm.subMap(startKey,
  74. endKey), mutex);
  75. }
  76. }
  77. public SortedMap<K, V> tailMap(K startKey) {
  78. lock (mutex) {
  79. return new SynchronizedSortedMap<K, V>(sm.tailMap(startKey),
  80. mutex);
  81. }
  82. }
  83. private void writeObject(java.io.ObjectOutputStream stream) {//throws IOException {
  84. lock (mutex) {
  85. stream.defaultWriteObject();
  86. }
  87. }
  88. }
  89. /**
  90. * Returns a wrapper on the specified map which synchronizes all access to
  91. * the map.
  92. *
  93. * @param map
  94. * the map to wrap in a lock map.
  95. * @return a lock Map.
  96. */
  97. public static Map<K, V> synchronizedMap<K, V>(Map<K, V> map)
  98. {
  99. if (map == null)
  100. {
  101. throw new java.lang.NullPointerException();
  102. }
  103. return new SynchronizedMap<K, V>(map);
  104. }
  105. class SynchronizedMap<K, V> : Map<K, V>, java.io.Serializable
  106. {
  107. private static readonly long serialVersionUID = 1978198479659022715L;
  108. private readonly Map<K, V> m;
  109. internal readonly Object mutex;
  110. internal SynchronizedMap(Map<K, V> map)
  111. {
  112. m = map;
  113. mutex = this;
  114. }
  115. internal SynchronizedMap(Map<K, V> map, Object mutex)
  116. {
  117. m = map;
  118. this.mutex = mutex;
  119. }
  120. public void clear()
  121. {
  122. lock (mutex)
  123. {
  124. m.clear();
  125. }
  126. }
  127. public bool containsKey(Object key)
  128. {
  129. lock (mutex)
  130. {
  131. return m.containsKey(key);
  132. }
  133. }
  134. public bool containsValue(Object value)
  135. {
  136. lock (mutex)
  137. {
  138. return m.containsValue(value);
  139. }
  140. }
  141. public Set<MapNS.Entry<K, V>> entrySet()
  142. {
  143. lock (mutex)
  144. {
  145. return new SynchronizedSet<MapNS.Entry<K, V>>(m.entrySet(), mutex);
  146. }
  147. }
  148. public override bool Equals(Object obj)
  149. {
  150. lock (mutex)
  151. {
  152. return m.equals(obj);
  153. }
  154. }
  155. public V get(Object key)
  156. {
  157. lock (mutex)
  158. {
  159. return m.get(key);
  160. }
  161. }
  162. public override int GetHashCode()
  163. {
  164. lock (mutex)
  165. {
  166. return m.GetHashCode();
  167. }
  168. }
  169. public bool isEmpty()
  170. {
  171. lock (mutex)
  172. {
  173. return m.isEmpty();
  174. }
  175. }
  176. public Set<K> keySet()
  177. {
  178. lock (mutex)
  179. {
  180. return new SynchronizedSet<K>(m.keySet(), mutex);
  181. }
  182. }
  183. public V put(K key, V value)
  184. {
  185. lock (mutex)
  186. {
  187. return m.put(key, value);
  188. }
  189. }
  190. public void putAll(Map<K, V> map)
  191. {
  192. lock (mutex)
  193. {
  194. m.putAll(map);
  195. }
  196. }
  197. public V remove(Object key)
  198. {
  199. lock (mutex)
  200. {
  201. return m.remove(key);
  202. }
  203. }
  204. public int size()
  205. {
  206. lock (mutex)
  207. {
  208. return m.size();
  209. }
  210. }
  211. public Collection<V> values()
  212. {
  213. lock (mutex)
  214. {
  215. return new SynchronizedCollection<V>(m.values(), mutex);
  216. }
  217. }
  218. public override String ToString()
  219. {
  220. lock (mutex)
  221. {
  222. return m.toString();
  223. }
  224. }
  225. private void writeObject(java.io.ObjectOutputStream stream)
  226. {//throws IOException {
  227. lock (mutex)
  228. {
  229. stream.defaultWriteObject();
  230. }
  231. }
  232. }
  233. [Serializable]
  234. class SynchronizedSet<E> : SynchronizedCollection<E>,
  235. Set<E>
  236. {
  237. private static readonly long serialVersionUID = 487447009682186044L;
  238. internal SynchronizedSet(Set<E> set)
  239. : base(set)
  240. {
  241. }
  242. internal SynchronizedSet(Set<E> set, Object mutex)
  243. : base(set, mutex)
  244. {
  245. }
  246. public override bool Equals(Object obj)
  247. {
  248. lock (mutex)
  249. {
  250. return c.equals(obj);
  251. }
  252. }
  253. public override int GetHashCode()
  254. {
  255. lock (mutex)
  256. {
  257. return c.GetHashCode();
  258. }
  259. }
  260. private void writeObject(java.io.ObjectOutputStream stream)
  261. {//throws IOException {
  262. lock (mutex)
  263. {
  264. stream.defaultWriteObject();
  265. }
  266. }
  267. }
  268. /**
  269. * An empty immutable instance of {@link Set}.
  270. */
  271. public static readonly Set<Object> EMPTY_SET = new EmptySet();
  272. [Serializable]
  273. sealed class EmptySet : AbstractSet<Object>, java.io.Serializable
  274. {
  275. private static readonly long serialVersionUID = 1582296315990362920L;
  276. public override bool contains(Object obj)
  277. {
  278. return false;
  279. }
  280. public override int size()
  281. {
  282. return 0;
  283. }
  284. public override Iterator<Object> iterator()
  285. {
  286. return new EmptySetIterator();
  287. }
  288. private Object readResolve()
  289. {
  290. return Collections.EMPTY_SET;
  291. }
  292. //+ TODO this class is an template for en EmptyIterator
  293. sealed class EmptySetIterator : Iterator<Object>
  294. {
  295. public bool hasNext()
  296. {
  297. return false;
  298. }
  299. public Object next()
  300. {
  301. throw new NoSuchElementException();
  302. }
  303. public void remove()
  304. {
  305. throw new java.lang.UnsupportedOperationException();
  306. }
  307. }
  308. }
  309. /**
  310. * An empty immutable instance of {@link List}.
  311. */
  312. public static readonly List<Object> EMPTY_LIST = new EmptyList();
  313. /**
  314. * Returns a list containing the specified number of the specified element.
  315. * The list cannot be modified. The list is serializable.
  316. *
  317. * @param length
  318. * the size of the returned list.
  319. * @param object
  320. * the element to be added {@code length} times to a list.
  321. * @return a list containing {@code length} copies of the element.
  322. * @throws IllegalArgumentException
  323. * when {@code length < 0}.
  324. */
  325. public static List<T> nCopies<T>(int length, T obj)
  326. {
  327. return new CopiesList<T>(length, obj);
  328. }
  329. /**
  330. * Returns a wrapper on the specified list which throws an
  331. * {@code UnsupportedOperationException} whenever an attempt is made to
  332. * modify the list.
  333. *
  334. * @param list
  335. * the list to wrap in an unmodifiable list.
  336. * @return an unmodifiable List.
  337. */
  338. public static List<E> unmodifiableList<E>(List<E> list)
  339. {
  340. if (list == null)
  341. {
  342. throw new java.lang.NullPointerException();
  343. }
  344. if (list is RandomAccess)
  345. {
  346. return new UnmodifiableRandomAccessList<E>((List<E>)list);
  347. }
  348. return new UnmodifiableList<E>((List<E>)list);
  349. }
  350. /**
  351. * Returns a wrapper on the specified collection which throws an
  352. * {@code UnsupportedOperationException} whenever an attempt is made to
  353. * modify the collection.
  354. *
  355. * @param collection
  356. * the collection to wrap in an unmodifiable collection.
  357. * @return an unmodifiable collection.
  358. */
  359. public static Collection<E> unmodifiableCollection<E>(Collection<E> collection)
  360. {
  361. if (collection == null)
  362. {
  363. throw new java.lang.NullPointerException();
  364. }
  365. return new UnmodifiableCollection<E>(collection);
  366. }
  367. /**
  368. * Returns a wrapper on the specified sorted map which throws an
  369. * {@code UnsupportedOperationException} whenever an attempt is made to
  370. * modify the sorted map.
  371. *
  372. * @param map
  373. * the sorted map to wrap in an unmodifiable sorted map.
  374. * @return a unmodifiable sorted map
  375. */
  376. public static SortedMap<K, V> unmodifiableSortedMap<K, V>(SortedMap<K, V> map)
  377. {
  378. if (map == null)
  379. {
  380. throw new java.lang.NullPointerException();
  381. }
  382. return new UnmodifiableSortedMap<K, V>((SortedMap<K, V>)map);
  383. }
  384. /**
  385. * Returns a wrapper on the specified sorted set which throws an
  386. * {@code UnsupportedOperationException} whenever an attempt is made to
  387. * modify the sorted set.
  388. *
  389. * @param set
  390. * the sorted set to wrap in an unmodifiable sorted set.
  391. * @return a unmodifiable sorted set.
  392. */
  393. public static SortedSet<E> unmodifiableSortedSet<E>(SortedSet<E> set)
  394. {
  395. if (set == null)
  396. {
  397. throw new java.lang.NullPointerException();
  398. }
  399. return new UnmodifiableSortedSet<E>(set);
  400. }
  401. /**
  402. * Returns a wrapper on the specified map which throws an
  403. * {@code UnsupportedOperationException} whenever an attempt is made to
  404. * modify the map.
  405. *
  406. * @param map
  407. * the map to wrap in an unmodifiable map.
  408. * @return a unmodifiable map.
  409. */
  410. public static Map<K, V> unmodifiableMap<K, V>(Map<K, V> map)
  411. {
  412. if (map == null)
  413. {
  414. throw new java.lang.NullPointerException();
  415. }
  416. return new UnmodifiableMap<K, V>((Map<K, V>)map);
  417. }
  418. /**
  419. * Returns a wrapper on the specified set which throws an
  420. * {@code UnsupportedOperationException} whenever an attempt is made to
  421. * modify the set.
  422. *
  423. * @param set
  424. * the set to wrap in an unmodifiable set.
  425. * @return a unmodifiable set
  426. */
  427. public static Set<E> unmodifiableSet<E>(Set<E> set)
  428. {
  429. if (set == null)
  430. {
  431. throw new java.lang.NullPointerException();
  432. }
  433. return new UnmodifiableSet<E>((Set<E>)set);
  434. }
  435. /**
  436. * Returns a set containing the specified element. The set cannot be
  437. * modified. The set is serializable.
  438. *
  439. * @param object
  440. * the element.
  441. * @return a set containing the element.
  442. */
  443. public static Set<E> singleton<E>(E obj)
  444. {
  445. return new SingletonSet<E>(obj);
  446. }
  447. /**
  448. * Returns a {@link Comparator} that reverses the order of the
  449. * {@code Comparator} passed. If the {@code Comparator} passed is
  450. * {@code null}, then this method is equivalent to {@link #reverseOrder()}.
  451. * <p>
  452. * The {@code Comparator} that's returned is {@link Serializable} if the
  453. * {@code Comparator} passed is serializable or {@code null}.
  454. *
  455. * @param c
  456. * the {@code Comparator} to reverse or {@code null}.
  457. * @return a {@code Comparator} instance.
  458. * @see Comparator
  459. * @since 1.5
  460. */
  461. public static Comparator<T> reverseOrder<T>(Comparator<T> c)
  462. {
  463. if (c == null)
  464. {
  465. return reverseOrder<T>();
  466. }
  467. if (c is ReverseComparatorWithComparator<T>)
  468. {
  469. return ((ReverseComparatorWithComparator<T>)c).comparator;
  470. }
  471. return new ReverseComparatorWithComparator<T>(c);
  472. }
  473. /**
  474. * A comparator which reverses the natural order of the elements. The
  475. * {@code Comparator} that's returned is {@link Serializable}.
  476. *
  477. * @return a {@code Comparator} instance.
  478. * @see Comparator
  479. * @see Comparable
  480. * @see Serializable
  481. */
  482. public static Comparator<T> reverseOrder<T>()
  483. {
  484. return (Comparator<T>)ReverseComparator<T>.INSTANCE;
  485. }
  486. /**
  487. * This class is a singleton so that equals() and hashCode() work properly.
  488. */
  489. [Serializable]
  490. private sealed class ReverseComparator<T> : Comparator<T>, java.io.Serializable
  491. {
  492. internal static readonly ReverseComparator<T> INSTANCE
  493. = new ReverseComparator<T>();
  494. private static readonly long serialVersionUID = 7207038068494060240L;
  495. public int compare(T o1, T o2)
  496. {
  497. java.lang.Comparable<T> c2 = (java.lang.Comparable<T>)o2;
  498. return c2.compareTo(o1);
  499. }
  500. private Object readResolve()
  501. {//throws ObjectStreamException {
  502. return INSTANCE;
  503. }
  504. public bool equals(Object o)
  505. {
  506. return Equals(o);
  507. }
  508. }
  509. [Serializable]
  510. private sealed class ReverseComparatorWithComparator<T> :
  511. Comparator<T>, java.io.Serializable
  512. {
  513. private static readonly long serialVersionUID = 4374092139857L;
  514. internal readonly Comparator<T> comparator;
  515. internal ReverseComparatorWithComparator(Comparator<T> comparator)
  516. : base()
  517. {
  518. this.comparator = comparator;
  519. }
  520. public int compare(T o1, T o2)
  521. {
  522. return comparator.compare(o2, o1);
  523. }
  524. public override bool Equals(Object o)
  525. {
  526. return o is ReverseComparatorWithComparator<T>
  527. && ((ReverseComparatorWithComparator<T>)o).comparator
  528. .equals(comparator);
  529. }
  530. public override int GetHashCode()
  531. {
  532. return ~comparator.GetHashCode();
  533. }
  534. public bool equals(Object o)
  535. {
  536. return Equals(o);
  537. }
  538. }
  539. [Serializable]
  540. sealed class EmptyList : AbstractList<Object>,
  541. RandomAccess, java.io.Serializable
  542. {
  543. private static readonly long serialVersionUID = 8842843931221139166L;
  544. public override bool contains(Object obj)
  545. {
  546. return false;
  547. }
  548. public override int size()
  549. {
  550. return 0;
  551. }
  552. public override Object get(int location)
  553. {
  554. throw new java.lang.IndexOutOfBoundsException();
  555. }
  556. private Object readResolve()
  557. {
  558. return Collections.EMPTY_LIST;
  559. }
  560. }
  561. }
  562. [Serializable]
  563. internal class UnmodifiableCollection<E> : Collection<E>, java.io.Serializable
  564. {
  565. private static readonly long serialVersionUID = 1820017752578914078L;
  566. readonly protected internal Collection<E> c;
  567. protected internal UnmodifiableCollection(Collection<E> collection)
  568. {
  569. c = collection;
  570. }
  571. public virtual bool add(E obj)
  572. {
  573. throw new java.lang.UnsupportedOperationException();
  574. }
  575. public virtual bool addAll(Collection<E> collection)
  576. {
  577. throw new java.lang.UnsupportedOperationException();
  578. }
  579. public virtual void clear()
  580. {
  581. throw new java.lang.UnsupportedOperationException();
  582. }
  583. public virtual bool contains(Object obj)
  584. {
  585. return c.contains(obj);
  586. }
  587. public virtual bool containsAll(Collection<E> collection)
  588. {
  589. return c.containsAll(collection);
  590. }
  591. public virtual bool isEmpty()
  592. {
  593. return c.isEmpty();
  594. }
  595. public virtual Iterator<E> iterator()
  596. {
  597. UnmodifiableCollectionWrapperIterator<Collection<E>> w;
  598. w = new UnmodifiableCollectionWrapperIterator<Collection<E>>(c);
  599. return (Iterator<E>)w;
  600. }
  601. public virtual bool remove(Object obj)
  602. {
  603. throw new java.lang.UnsupportedOperationException();
  604. }
  605. public virtual bool removeAll(Collection<E> collection)
  606. {
  607. throw new java.lang.UnsupportedOperationException();
  608. }
  609. public virtual bool retainAll(Collection<E> collection)
  610. {
  611. throw new java.lang.UnsupportedOperationException();
  612. }
  613. public virtual int size()
  614. {
  615. return c.size();
  616. }
  617. public virtual Object[] toArray()
  618. {
  619. return c.toArray();
  620. }
  621. public virtual T[] toArray<T>(T[] array)
  622. {
  623. return c.toArray(array);
  624. }
  625. public override String ToString()
  626. {
  627. return c.ToString();
  628. }
  629. }
  630. [Serializable]
  631. internal class UnmodifiableMap<K, V> : Map<K, V>, java.io.Serializable
  632. {
  633. private static readonly long serialVersionUID = -1034234728574286014L;
  634. private readonly Map<K, V> m;
  635. protected internal UnmodifiableMap(Map<K, V> map)
  636. {
  637. m = map;
  638. }
  639. public void clear()
  640. {
  641. throw new java.lang.UnsupportedOperationException();
  642. }
  643. public bool containsKey(Object key)
  644. {
  645. return m.containsKey(key);
  646. }
  647. public bool containsValue(Object value)
  648. {
  649. return m.containsValue(value);
  650. }
  651. public Set<MapNS.Entry<K, V>> entrySet()
  652. {
  653. return new UnmodifiableEntrySet<K, V>(m.entrySet());
  654. }
  655. public override bool Equals(Object obj)
  656. {
  657. return m.equals(obj);
  658. }
  659. public V get(Object key)
  660. {
  661. return m.get(key);
  662. }
  663. public override int GetHashCode()
  664. {
  665. return m.GetHashCode();
  666. }
  667. public bool isEmpty()
  668. {
  669. return m.isEmpty();
  670. }
  671. public Set<K> keySet()
  672. {
  673. return new UnmodifiableSet<K>(m.keySet());
  674. }
  675. public V put(K key, V value)
  676. {
  677. throw new java.lang.UnsupportedOperationException();
  678. }
  679. public void putAll(Map<K, V> map)
  680. {
  681. throw new java.lang.UnsupportedOperationException();
  682. }
  683. public V remove(Object key)
  684. {
  685. throw new java.lang.UnsupportedOperationException();
  686. }
  687. public int size()
  688. {
  689. return m.size();
  690. }
  691. public Collection<V> values()
  692. {
  693. return new UnmodifiableCollection<V>(m.values());
  694. }
  695. public override String ToString()
  696. {
  697. return m.toString();
  698. }
  699. }
  700. internal class UnmodifiableSet<E> : UnmodifiableCollection<E>, Set<E>
  701. {
  702. private static readonly long serialVersionUID = -9215047833775013803L;
  703. protected internal UnmodifiableSet(Set<E> set)
  704. : base(set)
  705. {
  706. }
  707. public override bool Equals(Object obj)
  708. {
  709. return c.Equals(obj);
  710. }
  711. public override int GetHashCode()
  712. {
  713. return c.GetHashCode();
  714. }
  715. }
  716. [Serializable]
  717. internal class SynchronizedRandomAccessList<E> : SynchronizedList<E>, RandomAccess
  718. {
  719. private static long serialVersionUID = 1530674583602358482L;
  720. protected internal SynchronizedRandomAccessList(java.util.List<E> l)
  721. : base(l)
  722. {
  723. }
  724. protected internal SynchronizedRandomAccessList(List<E> l, Object mutex)
  725. : base(l, mutex)
  726. {
  727. }
  728. public override List<E> subList(int start, int end)
  729. {
  730. lock (mutex)
  731. {
  732. return new SynchronizedRandomAccessList<E>(list.subList(start,
  733. end), mutex);
  734. }
  735. }
  736. /**
  737. * Replaces this SynchronizedRandomAccessList with a SynchronizedList so
  738. * that JREs before 1.4 can deserialize this obj without any
  739. * problems. This is necessary since RandomAccess API was introduced
  740. * only in 1.4.
  741. * <p>
  742. *
  743. * @return SynchronizedList
  744. *
  745. * @see SynchronizedList#readResolve()
  746. */
  747. private Object writeReplace()
  748. {
  749. return new SynchronizedList<E>(list);
  750. }
  751. }
  752. [Serializable]
  753. internal class SynchronizedList<E> : SynchronizedCollection<E>, List<E>
  754. {
  755. private static long serialVersionUID = -7754090372962971524L;
  756. internal readonly List<E> list;
  757. internal SynchronizedList(List<E> l)
  758. : base(l)
  759. {
  760. list = l;
  761. }
  762. internal SynchronizedList(List<E> l, Object mutex)
  763. : base(l, mutex)
  764. {
  765. list = l;
  766. }
  767. public virtual void add(int location, E obj)
  768. {
  769. lock (mutex)
  770. {
  771. list.add(location, obj);
  772. }
  773. }
  774. public bool addAll(int location, Collection<E> collection)
  775. {
  776. lock (mutex)
  777. {
  778. return list.addAll(location, collection);
  779. }
  780. }
  781. public override bool Equals(Object obj)
  782. {
  783. lock (mutex)
  784. {
  785. return list.equals(obj);
  786. }
  787. }
  788. public E get(int location)
  789. {
  790. lock (mutex)
  791. {
  792. return list.get(location);
  793. }
  794. }
  795. public override int GetHashCode()
  796. {
  797. lock (mutex)
  798. {
  799. return list.GetHashCode();
  800. }
  801. }
  802. public int indexOf(Object obj)
  803. {
  804. int size;
  805. Object[] array;
  806. lock (mutex)
  807. {
  808. size = list.size();
  809. array = new Object[size];
  810. list.toArray(array);
  811. }
  812. if (null != obj)
  813. for (int i = 0; i < size; i++)
  814. {
  815. if (obj.equals(array[i]))
  816. {
  817. return i;
  818. }
  819. }
  820. else
  821. {
  822. for (int i = 0; i < size; i++)
  823. {
  824. if (null == array[i])
  825. {
  826. return i;
  827. }
  828. }
  829. }
  830. return -1;
  831. }
  832. public int lastIndexOf(Object obj)
  833. {
  834. int size;
  835. Object[] array;
  836. lock (mutex)
  837. {
  838. size = list.size();
  839. array = new Object[size];
  840. list.toArray(array);
  841. }
  842. if (null != obj)
  843. for (int i = size - 1; i >= 0; i--)
  844. {
  845. if (obj.equals(array[i]))
  846. {
  847. return i;
  848. }
  849. }
  850. else
  851. {
  852. for (int i = size - 1; i >= 0; i--)
  853. {
  854. if (null == array[i])
  855. {
  856. return i;
  857. }
  858. }
  859. }
  860. return -1;
  861. }
  862. public ListIterator<E> listIterator()
  863. {
  864. lock (mutex)
  865. {
  866. return list.listIterator();
  867. }
  868. }
  869. public ListIterator<E> listIterator(int location)
  870. {
  871. lock (mutex)
  872. {
  873. return list.listIterator(location);
  874. }
  875. }
  876. public E remove(int location)
  877. {
  878. lock (mutex)
  879. {
  880. return list.remove(location);
  881. }
  882. }
  883. public E set(int location, E obj)
  884. {
  885. lock (mutex)
  886. {
  887. return list.set(location, obj);
  888. }
  889. }
  890. public virtual List<E> subList(int start, int end)
  891. {
  892. lock (mutex)
  893. {
  894. return new SynchronizedList<E>(list.subList(start, end), mutex);
  895. }
  896. }
  897. /*
  898. private void writeObject(ObjectOutputStream stream) throws IOException {
  899. lock (mutex) {
  900. stream.defaultWriteObject();
  901. }
  902. }*/
  903. /**
  904. * Resolves SynchronizedList instances to SynchronizedRandomAccessList
  905. * instances if the underlying list is a Random Access list.
  906. * <p>
  907. * This is necessary since SynchronizedRandomAccessList instances are
  908. * replaced with SynchronizedList instances during serialization for
  909. * compliance with JREs before 1.4.
  910. * <p>
  911. *
  912. * @return a SynchronizedList instance if the underlying list implements
  913. * RandomAccess interface, or this same obj if not.
  914. *
  915. * @see SynchronizedRandomAccessList#writeReplace()
  916. */
  917. private Object readResolve()
  918. {
  919. if (list is RandomAccess)
  920. {
  921. return new SynchronizedRandomAccessList<E>(list, mutex);
  922. }
  923. return this;
  924. }
  925. }
  926. [Serializable]
  927. internal class SynchronizedCollection<E> : Collection<E>, java.io.Serializable
  928. {
  929. private static readonly long serialVersionUID = 3053995032091335093L;
  930. protected internal readonly Collection<E> c;
  931. protected internal readonly Object mutex;
  932. protected internal SynchronizedCollection(Collection<E> collection)
  933. {
  934. c = collection;
  935. mutex = this;
  936. }
  937. protected internal SynchronizedCollection(Collection<E> collection, Object mutex)
  938. {
  939. c = collection;
  940. this.mutex = mutex;
  941. }
  942. public bool add(E obj)
  943. {
  944. lock (mutex)
  945. {
  946. return c.add(obj);
  947. }
  948. }
  949. public bool addAll(Collection<E> collection)
  950. {
  951. lock (mutex)
  952. {
  953. return c.addAll(collection);
  954. }
  955. }
  956. public void clear()
  957. {
  958. lock (mutex)
  959. {
  960. c.clear();
  961. }
  962. }
  963. public bool contains(Object obj)
  964. {
  965. lock (mutex)
  966. {
  967. return c.contains(obj);
  968. }
  969. }
  970. public bool containsAll(Collection<E> collection)
  971. {
  972. lock (mutex)
  973. {
  974. return c.containsAll(collection);
  975. }
  976. }
  977. public bool isEmpty()
  978. {
  979. lock (mutex)
  980. {
  981. return c.isEmpty();
  982. }
  983. }
  984. public Iterator<E> iterator()
  985. {
  986. lock (mutex)
  987. {
  988. return c.iterator();
  989. }
  990. }
  991. public bool remove(Object obj)
  992. {
  993. lock (mutex)
  994. {
  995. return c.remove(obj);
  996. }
  997. }
  998. public bool removeAll(Collection<E> collection)
  999. {
  1000. lock (mutex)
  1001. {
  1002. return c.removeAll(collection);
  1003. }
  1004. }
  1005. public bool retainAll(Collection<E> collection)
  1006. {
  1007. lock (mutex)
  1008. {
  1009. return c.retainAll(collection);
  1010. }
  1011. }
  1012. public int size()
  1013. {
  1014. lock (mutex)
  1015. {
  1016. return c.size();
  1017. }
  1018. }
  1019. public Object[] toArray()
  1020. {
  1021. lock (mutex)
  1022. {
  1023. return c.toArray();
  1024. }
  1025. }
  1026. public override String ToString()
  1027. {
  1028. lock (mutex)
  1029. {
  1030. return c.toString();
  1031. }
  1032. }
  1033. public T[] toArray<T>(T[] array)
  1034. {
  1035. lock (mutex)
  1036. {
  1037. return c.toArray(array);
  1038. }
  1039. }
  1040. private void writeObject(java.io.ObjectOutputStream stream)
  1041. {//throws IOException {
  1042. lock (mutex)
  1043. {
  1044. stream.defaultWriteObject();
  1045. }
  1046. }
  1047. }
  1048. [Serializable]
  1049. internal class UnmodifiableSortedMap<K, V> : UnmodifiableMap<K, V>, SortedMap<K, V>
  1050. {
  1051. private static readonly long serialVersionUID = -8806743815996713206L;
  1052. private readonly SortedMap<K, V> sm;
  1053. protected internal UnmodifiableSortedMap(SortedMap<K, V> map)
  1054. : base(map)
  1055. {
  1056. sm = map;
  1057. }
  1058. public Comparator<K> comparator()
  1059. {
  1060. return sm.comparator();
  1061. }
  1062. public K firstKey()
  1063. {
  1064. return sm.firstKey();
  1065. }
  1066. public SortedMap<K, V> headMap(K before)
  1067. {
  1068. return new UnmodifiableSortedMap<K, V>(sm.headMap(before));
  1069. }
  1070. public K lastKey()
  1071. {
  1072. return sm.lastKey();
  1073. }
  1074. public SortedMap<K, V> subMap(K start, K end)
  1075. {
  1076. return new UnmodifiableSortedMap<K, V>(sm.subMap(start, end));
  1077. }
  1078. public SortedMap<K, V> tailMap(K after)
  1079. {
  1080. return new UnmodifiableSortedMap<K, V>(sm.tailMap(after));
  1081. }
  1082. }
  1083. [Serializable]
  1084. internal class UnmodifiableSortedSet<E> : UnmodifiableSet<E>, SortedSet<E>
  1085. {
  1086. private static readonly long serialVersionUID = -4929149591599911165L;
  1087. private readonly SortedSet<E> ss;
  1088. protected internal UnmodifiableSortedSet(SortedSet<E> set)
  1089. : base(set)
  1090. {
  1091. ss = set;
  1092. }
  1093. public Comparator<E> comparator()
  1094. {
  1095. return ss.comparator();
  1096. }
  1097. public E first()
  1098. {
  1099. return ss.first();
  1100. }
  1101. public SortedSet<E> headSet(E before)
  1102. {
  1103. return new UnmodifiableSortedSet<E>(ss.headSet(before));
  1104. }
  1105. public E last()
  1106. {
  1107. return ss.last();
  1108. }
  1109. public SortedSet<E> subSet(E start, E end)
  1110. {
  1111. return new UnmodifiableSortedSet<E>(ss.subSet(start, end));
  1112. }
  1113. public SortedSet<E> tailSet(E after)
  1114. {
  1115. return new UnmodifiableSortedSet<E>(ss.tailSet(after));
  1116. }
  1117. }
  1118. internal class UnmodifiableEntrySet<K, V> : UnmodifiableSet<MapNS.Entry<K, V>>
  1119. {
  1120. private static readonly long serialVersionUID = 7854390611657943733L;
  1121. protected internal UnmodifiableEntrySet(Set<MapNS.Entry<K, V>> set)
  1122. : base(set)
  1123. {
  1124. }
  1125. public override Iterator<MapNS.Entry<K, V>> iterator()
  1126. {
  1127. UnmodifiableMapWrapperIterator<Object, K, V> w;
  1128. w = new UnmodifiableMapWrapperIterator<Object, K, V>(this.c);
  1129. return (Iterator<MapNS.Entry<K, V>>)w;
  1130. }
  1131. public override Object[] toArray()
  1132. {
  1133. int length = c.size();
  1134. Object[] result = new Object[length];
  1135. Iterator<MapNS.Entry<K, V>> it = iterator();
  1136. for (int i = length; --i >= 0; )
  1137. {
  1138. result[i] = it.next();
  1139. }
  1140. return result;
  1141. }
  1142. public override T[] toArray<T>(T[] contents)
  1143. {
  1144. int size = c.size(), index = 0;
  1145. Iterator<MapNS.Entry<K, V>> it = iterator();
  1146. if (size > contents.Length)
  1147. {
  1148. contents = new T[size];
  1149. }
  1150. while (index < size)
  1151. {
  1152. contents[index++] = (T)it.next();
  1153. }
  1154. if (index < contents.Length)
  1155. {
  1156. contents[index] = default(T);
  1157. }
  1158. return contents;
  1159. }
  1160. }
  1161. #region UnmodifiableIterator implementations
  1162. internal class UnmodifiableCollectionWrapperIterator<E> : SimpleListIterator<E>
  1163. {
  1164. protected internal UnmodifiableCollectionWrapperIterator(E c)
  1165. : base(new ArrayList<E>((Collection<E>)c))
  1166. {
  1167. }
  1168. public override void remove()
  1169. {
  1170. throw new java.lang.UnsupportedOperationException();
  1171. }
  1172. }
  1173. internal class UnmodifiableMapWrapperIterator<E, K, V> : SimpleListIterator<E>
  1174. {
  1175. public UnmodifiableMapWrapperIterator(E c)
  1176. : base(new ArrayList<E>((Collection<E>)(Set<MapNS.Entry<K, V>>)c))
  1177. {
  1178. }
  1179. public override E next()
  1180. {
  1181. Object o = (Object)new UnmodifiableMapEntry<K, V>((MapNS.Entry<K, V>)base.next());
  1182. return (E)o;
  1183. }
  1184. public override void remove()
  1185. {
  1186. throw new java.lang.UnsupportedOperationException();
  1187. }
  1188. }
  1189. internal class UnmodifiableMapEntry<K, V> : MapNS.Entry<K, V>
  1190. {
  1191. private readonly MapNS.Entry<K, V> mapEntry;
  1192. protected internal UnmodifiableMapEntry(MapNS.Entry<K, V> entry)
  1193. {
  1194. mapEntry = entry;
  1195. }
  1196. public override bool Equals(Object obj)
  1197. {
  1198. return mapEntry.equals(obj);
  1199. }
  1200. public K getKey()
  1201. {
  1202. return mapEntry.getKey();
  1203. }
  1204. public V getValue()
  1205. {
  1206. return mapEntry.getValue();
  1207. }
  1208. public override int GetHashCode()
  1209. {
  1210. return mapEntry.GetHashCode();
  1211. }
  1212. public V setValue(V obj)
  1213. {
  1214. throw new java.lang.UnsupportedOperationException();
  1215. }
  1216. public override String ToString()
  1217. {
  1218. return mapEntry.toString();
  1219. }
  1220. }
  1221. #endregion
  1222. #region UnmodifiableList implementations
  1223. [Serializable]
  1224. internal class UnmodifiableRandomAccessList<E> : UnmodifiableList<E>, RandomAccess
  1225. {
  1226. private static readonly long serialVersionUID = -2542308836966382001L;
  1227. internal protected UnmodifiableRandomAccessList(List<E> l)
  1228. : base(l)
  1229. {
  1230. }
  1231. public override List<E> subList(int start, int end)
  1232. {
  1233. return new UnmodifiableRandomAccessList<E>(list.subList(start, end));
  1234. }
  1235. /**
  1236. * Replaces this UnmodifiableRandomAccessList with an UnmodifiableList
  1237. * so that JREs before 1.4 can deserialize this object without any
  1238. * problems. This is necessary since RandomAccess API was introduced
  1239. * only in 1.4.
  1240. * <p>
  1241. *
  1242. * @return UnmodifiableList
  1243. *
  1244. * @see UnmodifiableList#readResolve()
  1245. */
  1246. private Object writeReplace()
  1247. {
  1248. return new UnmodifiableList<E>(list);
  1249. }
  1250. }
  1251. [Serializable]
  1252. internal class UnmodifiableList<E> : UnmodifiableCollection<E>, List<E>
  1253. {
  1254. private static readonly long serialVersionUID = -283967356065247728L;
  1255. protected internal List<E> list;
  1256. internal UnmodifiableList(List<E> l)
  1257. : base(l)
  1258. {
  1259. list = l;
  1260. }
  1261. public void add(int location, E o)
  1262. {
  1263. throw new java.lang.UnsupportedOperationException();
  1264. }
  1265. public bool addAll(int location, Collection<E> collection)
  1266. {
  1267. throw new java.lang.UnsupportedOperationException();
  1268. }
  1269. public override bool Equals(Object o)
  1270. {
  1271. return list.Equals(o);
  1272. }
  1273. public E get(int location)
  1274. {
  1275. return list.get(location);
  1276. }
  1277. public override int GetHashCode()
  1278. {
  1279. return list.GetHashCode();
  1280. }
  1281. public int indexOf(Object o)
  1282. {
  1283. return list.indexOf(o);
  1284. }
  1285. public int lastIndexOf(Object o)
  1286. {
  1287. return list.lastIndexOf(o);
  1288. }
  1289. public ListIterator<E> listIterator()
  1290. {
  1291. return listIterator(0);
  1292. }
  1293. public ListIterator<E> listIterator(int location)
  1294. {
  1295. return new IAC_ListIterator<E>(this, location);
  1296. }
  1297. public E remove(int location)
  1298. {
  1299. throw new java.lang.UnsupportedOperationException();
  1300. }
  1301. public E set(int location, E o)
  1302. {
  1303. throw new java.lang.UnsupportedOperationException();
  1304. }
  1305. public virtual java.util.List<E> subList(int start, int end)
  1306. {
  1307. return new UnmodifiableList<E>(list.subList(start, end));
  1308. }
  1309. /**
  1310. * Resolves UnmodifiableList instances to UnmodifiableRandomAccessList
  1311. * instances if the underlying list is a Random Access list.
  1312. * <p>
  1313. * This is necessary since UnmodifiableRandomAccessList instances are
  1314. * replaced with UnmodifiableList instances during serialization for
  1315. * compliance with JREs before 1.4.
  1316. * <p>
  1317. *
  1318. * @return an UnmodifiableList instance if the underlying list
  1319. * implements RandomAccess interface, or this same object if
  1320. * not.
  1321. *
  1322. * @see UnmodifiableRandomAccessList#writeReplace()
  1323. */
  1324. private Object readResolve()
  1325. {
  1326. if (list is RandomAccess)
  1327. {
  1328. return new UnmodifiableRandomAccessList<E>(list);
  1329. }
  1330. return this;
  1331. }
  1332. }
  1333. internal class IAC_ListIterator<E> : ListIterator<E>
  1334. {
  1335. readonly ListIterator<E> iterator;
  1336. public IAC_ListIterator(List<E> l, int location)
  1337. {
  1338. iterator = l.listIterator(location);
  1339. }
  1340. public void add(E o)
  1341. {
  1342. throw new java.lang.UnsupportedOperationException();
  1343. }
  1344. public bool hasNext()
  1345. {
  1346. return iterator.hasNext();
  1347. }
  1348. public bool hasPrevious()
  1349. {
  1350. return iterator.hasPrevious();
  1351. }
  1352. public E next()
  1353. {
  1354. return iterator.next();
  1355. }
  1356. public int nextIndex()
  1357. {
  1358. return iterator.nextIndex();
  1359. }
  1360. public E previous()
  1361. {
  1362. return iterator.previous();
  1363. }
  1364. public int previousIndex()
  1365. {
  1366. return iterator.previousIndex();
  1367. }
  1368. public void remove()
  1369. {
  1370. throw new java.lang.UnsupportedOperationException();
  1371. }
  1372. public void set(E o)
  1373. {
  1374. throw new java.lang.UnsupportedOperationException();
  1375. }
  1376. }
  1377. [Serializable]
  1378. internal sealed class SingletonSet<E> : AbstractSet<E>, java.io.Serializable
  1379. {
  1380. private static readonly long serialVersionUID = 3193687207550431679L;
  1381. internal readonly E element;
  1382. internal SingletonSet(E obj)
  1383. {
  1384. element = obj;
  1385. }
  1386. public override bool contains(Object obj)
  1387. {
  1388. return element == null ? obj == null : element.equals(obj);
  1389. }
  1390. public override int size()
  1391. {
  1392. return 1;
  1393. }
  1394. public override Iterator<E> iterator()
  1395. {
  1396. return new IAC_SingletonSetIterator(this);
  1397. }
  1398. internal sealed class IAC_SingletonSetIterator : Iterator<E>
  1399. {
  1400. private SingletonSet<E> parent;
  1401. public IAC_SingletonSetIterator(SingletonSet<E> parent)
  1402. {
  1403. this.parent = parent;
  1404. }
  1405. bool hasNextJ = true;
  1406. public bool hasNext()
  1407. {
  1408. return hasNextJ;
  1409. }
  1410. public E next()
  1411. {
  1412. if (hasNextJ)
  1413. {
  1414. hasNextJ = false;
  1415. return parent.element;
  1416. }
  1417. throw new NoSuchElementException();
  1418. }
  1419. public void remove()
  1420. {
  1421. throw new java.lang.UnsupportedOperationException();
  1422. }
  1423. }
  1424. }
  1425. [Serializable]
  1426. internal sealed class CopiesList<E> : AbstractList<E>, java.io.Serializable
  1427. {
  1428. private static readonly long serialVersionUID = 2739099268398711800L;
  1429. private readonly int n;
  1430. private readonly E element;
  1431. internal CopiesList(int length, E obj)
  1432. {
  1433. if (length < 0)
  1434. {
  1435. throw new java.lang.IllegalArgumentException();
  1436. }
  1437. n = length;
  1438. element = obj;
  1439. }
  1440. public override bool contains(Object obj)
  1441. {
  1442. return element == null ? obj == null : element.equals(obj);
  1443. }
  1444. public override int size()
  1445. {
  1446. return n;
  1447. }
  1448. public override E get(int location)
  1449. {
  1450. if (0 <= location && location < n)
  1451. {
  1452. return element;
  1453. }
  1454. throw new java.lang.IndexOutOfBoundsException();
  1455. }
  1456. }
  1457. #endregion
  1458. }