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

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

https://github.com/gadfly/nofs
C# | 1094 lines | 505 code | 72 blank | 517 comment | 101 complexity | 41c54b35916489d6152628e7fcde10d3 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 System.Text;
  17. using java = biz.ritter.javapi;
  18. namespace biz.ritter.javapi.util
  19. {
  20. /**
  21. * Vector is a variable size contiguous indexable array of objects. The size of
  22. * the vector is the number of objects it contains. The capacity of the vector
  23. * is the number of objects it can hold.
  24. * <p>
  25. * Objects may be inserted at any position up to the size of the vector, thus
  26. * increasing the size of the vector. Objects at any position in the vector may
  27. * be removed, thus shrinking the size of the Vector. Objects at any position in
  28. * the Vector may be replaced, which does not affect the vector's size.
  29. * <p>
  30. * The capacity of a vector may be specified when the vector is created. If the
  31. * capacity of the vector is exceeded, the capacity is increased (doubled by
  32. * default).
  33. *
  34. * @see java.lang.StringBuffer
  35. */
  36. [Serializable]
  37. public class Vector<E> : AbstractList<E>, List<E>, RandomAccess, java.lang.Cloneable, java.io.Serializable {
  38. private static readonly long serialVersionUID = -2767605614048989439L;
  39. /**
  40. * The number of elements or the size of the vector.
  41. */
  42. protected internal int elementCount;
  43. /**
  44. * The elements of the vector.
  45. */
  46. protected internal E[] elementData;
  47. /**
  48. * How many elements should be added to the vector when it is detected that
  49. * it needs to grow to accommodate extra entries. If this value is zero or
  50. * negative the size will be doubled if an increase is needed.
  51. */
  52. protected int capacityIncrement;
  53. private static readonly int DEFAULT_SIZE = 10;
  54. /**
  55. * Constructs a new vector using the default capacity.
  56. */
  57. public Vector() :this(DEFAULT_SIZE, 0){
  58. }
  59. /**
  60. * Constructs a new vector using the specified capacity.
  61. *
  62. * @param capacity
  63. * the initial capacity of the new vector.
  64. * @throws IllegalArgumentException
  65. * if {@code capacity} is negative.
  66. */
  67. public Vector(int capacity) :this(capacity, 0){
  68. }
  69. /**
  70. * Constructs a new vector using the specified capacity and capacity
  71. * increment.
  72. *
  73. * @param capacity
  74. * the initial capacity of the new vector.
  75. * @param capacityIncrement
  76. * the amount to increase the capacity when this vector is full.
  77. * @throws IllegalArgumentException
  78. * if {@code capacity} is negative.
  79. */
  80. public Vector(int capacity, int capacityIncrement) {
  81. if (capacity < 0) {
  82. throw new java.lang.IllegalArgumentException();
  83. }
  84. elementData = newElementArray(capacity);
  85. elementCount = 0;
  86. this.capacityIncrement = capacityIncrement;
  87. }
  88. /**
  89. * Constructs a new instance of {@code Vector} containing the elements in
  90. * {@code collection}. The order of the elements in the new {@code Vector}
  91. * is dependent on the iteration order of the seed collection.
  92. *
  93. * @param collection
  94. * the collection of elements to add.
  95. */
  96. public Vector(Collection<E> collection) :this(collection.size(), 0){
  97. Iterator<E> it = collection.iterator();
  98. while (it.hasNext()) {
  99. elementData[elementCount++] = it.next();
  100. }
  101. }
  102. private E[] newElementArray(int size) {
  103. return new E[size];
  104. }
  105. /**
  106. * Adds the specified object into this vector at the specified location. The
  107. * object is inserted before any element with the same or a higher index
  108. * increasing their index by 1. If the location is equal to the size of this
  109. * vector, the object is added at the end.
  110. *
  111. * @param location
  112. * the index at which to insert the element.
  113. * @param object
  114. * the object to insert in this vector.
  115. * @throws ArrayIndexOutOfBoundsException
  116. * if {@code location < 0 || location > size()}.
  117. * @see #addElement
  118. * @see #size
  119. */
  120. public override void add(int location, E obj) {
  121. insertElementAt(obj, location);
  122. }
  123. /**
  124. * Adds the specified object at the end of this vector.
  125. *
  126. * @param object
  127. * the object to add to the vector.
  128. * @return {@code true}
  129. */
  130. public override bool add(E obj) {
  131. lock (this) {
  132. if (elementCount == elementData.Length) {
  133. growByOne();
  134. }
  135. elementData[elementCount++] = obj;
  136. modCount++;
  137. return true;
  138. }
  139. }
  140. /**
  141. * Inserts the objects in the specified collection at the specified location
  142. * in this vector. The objects are inserted in the order in which they are
  143. * returned from the Collection iterator. The elements with an index equal
  144. * or higher than {@code location} have their index increased by the size of
  145. * the added collection.
  146. *
  147. * @param location
  148. * the location to insert the objects.
  149. * @param collection
  150. * the collection of objects.
  151. * @return {@code true} if this vector is modified, {@code false} otherwise.
  152. * @throws ArrayIndexOutOfBoundsException
  153. * if {@code location < 0} or {@code location > size()}.
  154. */
  155. public override bool addAll(int location, Collection<E> collection) {
  156. lock (this){
  157. if (0 <= location && location <= elementCount) {
  158. int size = collection.size();
  159. if (size == 0) {
  160. return false;
  161. }
  162. int required = size - (elementData.Length - elementCount);
  163. if (required > 0) {
  164. growBy(required);
  165. }
  166. int count = elementCount - location;
  167. if (count > 0) {
  168. java.lang.SystemJ.arraycopy(elementData, location, elementData, location
  169. + size, count);
  170. }
  171. Iterator<E> it = collection.iterator();
  172. while (it.hasNext()) {
  173. elementData[location++] = it.next();
  174. }
  175. elementCount += size;
  176. modCount++;
  177. return true;
  178. }
  179. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  180. }
  181. }
  182. /**
  183. * Adds the objects in the specified collection to the end of this vector.
  184. *
  185. * @param collection
  186. * the collection of objects.
  187. * @return {@code true} if this vector is modified, {@code false} otherwise.
  188. */
  189. public override bool addAll(Collection<E> collection) {
  190. lock (this) {
  191. return addAll(elementCount, collection);
  192. }
  193. }
  194. /**
  195. * Adds the specified object at the end of this vector.
  196. *
  197. * @param object
  198. * the object to add to the vector.
  199. */
  200. public virtual void addElement(E obj) {
  201. lock (this) {
  202. if (elementCount == elementData.Length) {
  203. growByOne();
  204. }
  205. elementData[elementCount++] = obj;
  206. modCount++;
  207. }
  208. }
  209. /**
  210. * Returns the number of elements this vector can hold without growing.
  211. *
  212. * @return the capacity of this vector.
  213. * @see #ensureCapacity
  214. * @see #size
  215. */
  216. public virtual int capacity() {
  217. lock (this) {
  218. return elementData.Length;
  219. }
  220. }
  221. /**
  222. * Removes all elements from this vector, leaving it empty.
  223. *
  224. * @see #isEmpty
  225. * @see #size
  226. */
  227. public override void clear() {
  228. removeAllElements();
  229. }
  230. /**
  231. * Returns a new vector with the same elements, size, capacity and capacity
  232. * increment as this vector.
  233. *
  234. * @return a shallow copy of this vector.
  235. * @see java.lang.Cloneable
  236. */
  237. public Object clone() {
  238. lock (this) {
  239. try {
  240. Vector<E> vector = (Vector<E>) base.MemberwiseClone();
  241. vector.elementData = (E[])elementData.Clone();
  242. return vector;
  243. } catch (java.lang.CloneNotSupportedException e) {
  244. return null;
  245. }
  246. }
  247. }
  248. /**
  249. * Searches this vector for the specified object.
  250. *
  251. * @param object
  252. * the object to look for in this vector.
  253. * @return {@code true} if object is an element of this vector,
  254. * {@code false} otherwise.
  255. * @see #indexOf(Object)
  256. * @see #indexOf(Object, int)
  257. * @see java.lang.Object#equals
  258. */
  259. public override bool contains(Object obj) {
  260. lock (this) {
  261. return indexOf(obj, 0) != -1;
  262. }
  263. }
  264. /**
  265. * Searches this vector for all objects in the specified collection.
  266. *
  267. * @param collection
  268. * the collection of objects.
  269. * @return {@code true} if all objects in the specified collection are
  270. * elements of this vector, {@code false} otherwise.
  271. */
  272. public override bool containsAll(Collection<E> collection) {
  273. lock (this) {
  274. return base.containsAll(collection);
  275. }
  276. }
  277. /**
  278. * Attempts to copy elements contained by this {@code Vector} into the
  279. * corresponding elements of the supplied {@code Object} array.
  280. *
  281. * @param elements
  282. * the {@code Object} array into which the elements of this
  283. * vector are copied.
  284. * @throws IndexOutOfBoundsException
  285. * if {@code elements} is not big enough.
  286. * @see #clone
  287. */
  288. public virtual void copyInto(Object[] elements) {
  289. lock (this) {
  290. java.lang.SystemJ.arraycopy(elementData, 0, elements, 0, elementCount);
  291. }
  292. }
  293. /**
  294. * Returns the element at the specified location in this vector.
  295. *
  296. * @param location
  297. * the index of the element to return in this vector.
  298. * @return the element at the specified location.
  299. * @throws ArrayIndexOutOfBoundsException
  300. * if {@code location < 0 || location >= size()}.
  301. * @see #size
  302. */
  303. public virtual E elementAt(int location) {
  304. lock (this) {
  305. if (location < elementCount) {
  306. return (E) elementData[location];
  307. }
  308. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  309. }
  310. }
  311. /**
  312. * Returns an enumeration on the elements of this vector. The results of the
  313. * enumeration may be affected if the contents of this vector is modified.
  314. *
  315. * @return an enumeration of the elements of this vector.
  316. * @see #elementAt
  317. * @see Enumeration
  318. */
  319. public virtual Enumeration<E> elements() {
  320. return new IAC_VECTOR_ENUMERATION<E> (this);
  321. }
  322. /**
  323. * Ensures that this vector can hold the specified number of elements
  324. * without growing.
  325. *
  326. * @param minimumCapacity
  327. * the minimum number of elements that this vector will hold
  328. * before growing.
  329. * @see #capacity
  330. */
  331. public virtual void ensureCapacity(int minimumCapacity) {
  332. lock (this) {
  333. if (elementData.Length < minimumCapacity) {
  334. int next = (capacityIncrement <= 0 ? elementData.Length
  335. : capacityIncrement)
  336. + elementData.Length;
  337. grow(minimumCapacity > next ? minimumCapacity : next);
  338. }
  339. }
  340. }
  341. /**
  342. * Compares the specified object to this vector and returns if they are
  343. * equal. The object must be a List which contains the same objects in the
  344. * same order.
  345. *
  346. * @param object
  347. * the object to compare with this object
  348. * @return {@code true} if the specified object is equal to this vector,
  349. * {@code false} otherwise.
  350. * @see #hashCode
  351. */
  352. public override bool Equals(Object obj) {
  353. lock (this) {
  354. if (this == obj) {
  355. return true;
  356. }
  357. if (obj is java.util.List<Object>) {
  358. java.util.List<Object> list = (java.util.List<Object>) obj;
  359. if (list.size() != elementCount) {
  360. return false;
  361. }
  362. int index = 0;
  363. Iterator<Object> it = list.iterator();
  364. while (it.hasNext()) {
  365. Object e1 = elementData[index++], e2 = it.next();
  366. if (!(e1 == null ? e2 == null : e1.equals(e2))) {
  367. return false;
  368. }
  369. }
  370. return true;
  371. }
  372. return false;
  373. }
  374. }
  375. /**
  376. * Returns the first element in this vector.
  377. *
  378. * @return the element at the first position.
  379. * @throws NoSuchElementException
  380. * if this vector is empty.
  381. * @see #elementAt
  382. * @see #lastElement
  383. * @see #size
  384. */
  385. public virtual E firstElement() {
  386. lock (this) {
  387. if (elementCount > 0) {
  388. return (E) elementData[0];
  389. }
  390. throw new NoSuchElementException();
  391. }
  392. }
  393. /**
  394. * Returns the element at the specified location in this vector.
  395. *
  396. * @param location
  397. * the index of the element to return in this vector.
  398. * @return the element at the specified location.
  399. * @throws ArrayIndexOutOfBoundsException
  400. * if {@code location < 0 || location >= size()}.
  401. * @see #size
  402. */
  403. public override E get(int location) {
  404. return elementAt(location);
  405. }
  406. private void grow(int newCapacity) {
  407. E[] newData = newElementArray(newCapacity);
  408. // Assumes elementCount is <= newCapacity
  409. if (elementCount > newCapacity) throw new java.lang.IllegalArgumentException ("Assumes elementCount is <= newCapacity");//assert elementCount <= newCapacity;
  410. java.lang.SystemJ.arraycopy(elementData, 0, newData, 0, elementCount);
  411. elementData = newData;
  412. }
  413. /**
  414. * JIT optimization
  415. */
  416. private void growByOne() {
  417. int adding = 0;
  418. if (capacityIncrement <= 0) {
  419. if ((adding = elementData.Length) == 0) {
  420. adding = 1;
  421. }
  422. } else {
  423. adding = capacityIncrement;
  424. }
  425. E[] newData = newElementArray(elementData.Length + adding);
  426. java.lang.SystemJ.arraycopy(elementData, 0, newData, 0, elementCount);
  427. elementData = newData;
  428. }
  429. private void growBy(int required) {
  430. int adding = 0;
  431. if (capacityIncrement <= 0) {
  432. if ((adding = elementData.Length) == 0) {
  433. adding = required;
  434. }
  435. while (adding < required) {
  436. adding += adding;
  437. }
  438. } else {
  439. adding = (required / capacityIncrement) * capacityIncrement;
  440. if (adding < required) {
  441. adding += capacityIncrement;
  442. }
  443. }
  444. E[] newData = newElementArray(elementData.Length + adding);
  445. java.lang.SystemJ.arraycopy(elementData, 0, newData, 0, elementCount);
  446. elementData = newData;
  447. }
  448. /**
  449. * Returns an integer hash code for the receiver. Objects which are equal
  450. * return the same value for this method.
  451. *
  452. * @return the receiver's hash.
  453. * @see #equals
  454. */
  455. public override int GetHashCode() {
  456. lock (this) {
  457. int result = 1;
  458. for (int i = 0; i < elementCount; i++) {
  459. result = (31 * result)
  460. + (elementData[i] == null ? 0 : elementData[i].GetHashCode());
  461. }
  462. return result;
  463. }
  464. }
  465. /**
  466. * Searches in this vector for the index of the specified object. The search
  467. * for the object starts at the beginning and moves towards the end of this
  468. * vector.
  469. *
  470. * @param object
  471. * the object to find in this vector.
  472. * @return the index in this vector of the specified element, -1 if the
  473. * element isn't found.
  474. * @see #contains
  475. * @see #lastIndexOf(Object)
  476. * @see #lastIndexOf(Object, int)
  477. */
  478. public override int indexOf(Object obj) {
  479. return indexOf(obj, 0);
  480. }
  481. /**
  482. * Searches in this vector for the index of the specified object. The search
  483. * for the object starts at the specified location and moves towards the end
  484. * of this vector.
  485. *
  486. * @param object
  487. * the object to find in this vector.
  488. * @param location
  489. * the index at which to start searching.
  490. * @return the index in this vector of the specified element, -1 if the
  491. * element isn't found.
  492. * @throws ArrayIndexOutOfBoundsException
  493. * if {@code location < 0}.
  494. * @see #contains
  495. * @see #lastIndexOf(Object)
  496. * @see #lastIndexOf(Object, int)
  497. */
  498. public virtual int indexOf(Object obj, int location) {
  499. lock (this) {
  500. if (obj != null) {
  501. for (int i = location; i < elementCount; i++) {
  502. if (obj.equals(elementData[i])) {
  503. return i;
  504. }
  505. }
  506. } else {
  507. for (int i = location; i < elementCount; i++) {
  508. if (elementData[i] == null) {
  509. return i;
  510. }
  511. }
  512. }
  513. return -1;
  514. }
  515. }
  516. /**
  517. * Inserts the specified object into this vector at the specified location.
  518. * This object is inserted before any previous element at the specified
  519. * location. All elements with an index equal or greater than
  520. * {@code location} have their index increased by 1. If the location is
  521. * equal to the size of this vector, the object is added at the end.
  522. *
  523. * @param object
  524. * the object to insert in this vector.
  525. * @param location
  526. * the index at which to insert the element.
  527. * @throws ArrayIndexOutOfBoundsException
  528. * if {@code location < 0 || location > size()}.
  529. * @see #addElement
  530. * @see #size
  531. */
  532. public virtual void insertElementAt(E obj, int location) {
  533. lock (this) {
  534. if (0 <= location && location <= elementCount) {
  535. if (elementCount == elementData.Length) {
  536. growByOne();
  537. }
  538. int count = elementCount - location;
  539. if (count > 0) {
  540. java.lang.SystemJ.arraycopy(elementData, location, elementData,
  541. location + 1, count);
  542. }
  543. elementData[location] = obj;
  544. elementCount++;
  545. modCount++;
  546. } else {
  547. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  548. }
  549. }
  550. }
  551. /**
  552. * Returns if this vector has no elements, a size of zero.
  553. *
  554. * @return {@code true} if this vector has no elements, {@code false}
  555. * otherwise.
  556. * @see #size
  557. */
  558. public override bool isEmpty() {
  559. lock (this) {
  560. return elementCount == 0;
  561. }
  562. }
  563. /**
  564. * Returns the last element in this vector.
  565. *
  566. * @return the element at the last position.
  567. * @throws NoSuchElementException
  568. * if this vector is empty.
  569. * @see #elementAt
  570. * @see #firstElement
  571. * @see #size
  572. */
  573. public virtual E lastElement() {
  574. lock (this) {
  575. try {
  576. return (E) elementData[elementCount - 1];
  577. } catch (java.lang.IndexOutOfBoundsException e) {
  578. throw new NoSuchElementException();
  579. }
  580. }
  581. }
  582. /**
  583. * Searches in this vector for the index of the specified object. The search
  584. * for the object starts at the end and moves towards the start of this
  585. * vector.
  586. *
  587. * @param object
  588. * the object to find in this vector.
  589. * @return the index in this vector of the specified element, -1 if the
  590. * element isn't found.
  591. * @see #contains
  592. * @see #indexOf(Object)
  593. * @see #indexOf(Object, int)
  594. */
  595. public override int lastIndexOf(Object obj) {
  596. lock (this) {
  597. return lastIndexOf(obj, elementCount - 1);
  598. }
  599. }
  600. /**
  601. * Searches in this vector for the index of the specified object. The search
  602. * for the object starts at the specified location and moves towards the
  603. * start of this vector.
  604. *
  605. * @param object
  606. * the object to find in this vector.
  607. * @param location
  608. * the index at which to start searching.
  609. * @return the index in this vector of the specified element, -1 if the
  610. * element isn't found.
  611. * @throws ArrayIndexOutOfBoundsException
  612. * if {@code location >= size()}.
  613. * @see #contains
  614. * @see #indexOf(Object)
  615. * @see #indexOf(Object, int)
  616. */
  617. public virtual int lastIndexOf(Object obj, int location) {
  618. lock (this) {
  619. if (location < elementCount) {
  620. if (obj != null) {
  621. for (int i = location; i >= 0; i--) {
  622. if (obj.equals(elementData[i])) {
  623. return i;
  624. }
  625. }
  626. } else {
  627. for (int i = location; i >= 0; i--) {
  628. if (elementData[i] == null) {
  629. return i;
  630. }
  631. }
  632. }
  633. return -1;
  634. }
  635. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  636. }
  637. }
  638. /**
  639. * Removes the object at the specified location from this vector. All
  640. * elements with an index bigger than {@code location} have their index
  641. * decreased by 1.
  642. *
  643. * @param location
  644. * the index of the object to remove.
  645. * @return the removed object.
  646. * @throws IndexOutOfBoundsException
  647. * if {@code location < 0 || location >= size()}.
  648. */
  649. public override E remove(int location) {
  650. lock (this) {
  651. if (location < elementCount) {
  652. E result = (E) elementData[location];
  653. elementCount--;
  654. int size = elementCount - location;
  655. if (size > 0) {
  656. java.lang.SystemJ.arraycopy(elementData, location + 1, elementData,
  657. location, size);
  658. }
  659. elementData[elementCount] = default(E);
  660. modCount++;
  661. return result;
  662. }
  663. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  664. }
  665. }
  666. /**
  667. * Removes the first occurrence, starting at the beginning and moving
  668. * towards the end, of the specified object from this vector. All elements
  669. * with an index bigger than the element that gets removed have their index
  670. * decreased by 1.
  671. *
  672. * @param object
  673. * the object to remove from this vector.
  674. * @return {@code true} if the specified object was found, {@code false}
  675. * otherwise.
  676. * @see #removeAllElements
  677. * @see #removeElementAt
  678. * @see #size
  679. */
  680. public override bool remove(Object obj) {
  681. return removeElement(obj);
  682. }
  683. /**
  684. * Removes all occurrences in this vector of each object in the specified
  685. * Collection.
  686. *
  687. * @param collection
  688. * the collection of objects to remove.
  689. * @return {@code true} if this vector is modified, {@code false} otherwise.
  690. * @see #remove(Object)
  691. * @see #contains(Object)
  692. */
  693. public override bool removeAll(Collection<E> collection) {
  694. lock (this) {
  695. return base.removeAll(collection);
  696. }
  697. }
  698. /**
  699. * Removes all elements from this vector, leaving the size zero and the
  700. * capacity unchanged.
  701. *
  702. * @see #isEmpty
  703. * @see #size
  704. */
  705. public virtual void removeAllElements() {
  706. lock (this) {
  707. for (int i = 0; i < elementCount; i++) {
  708. elementData[i] = default(E);
  709. }
  710. modCount++;
  711. elementCount = 0;
  712. }
  713. }
  714. /**
  715. * Removes the first occurrence, starting at the beginning and moving
  716. * towards the end, of the specified object from this vector. All elements
  717. * with an index bigger than the element that gets removed have their index
  718. * decreased by 1.
  719. *
  720. * @param object
  721. * the object to remove from this vector.
  722. * @return {@code true} if the specified object was found, {@code false}
  723. * otherwise.
  724. * @see #removeAllElements
  725. * @see #removeElementAt
  726. * @see #size
  727. */
  728. public virtual bool removeElement(Object obj) {
  729. lock (this) {
  730. int index;
  731. if ((index = indexOf(obj, 0)) == -1) {
  732. return false;
  733. }
  734. removeElementAt(index);
  735. return true;
  736. }
  737. }
  738. /**
  739. * Removes the element found at index position {@code location} from
  740. * this {@code Vector}. All elements with an index bigger than
  741. * {@code location} have their index decreased by 1.
  742. *
  743. * @param location
  744. * the index of the element to remove.
  745. * @throws ArrayIndexOutOfBoundsException
  746. * if {@code location < 0 || location >= size()}.
  747. * @see #removeElement
  748. * @see #removeAllElements
  749. * @see #size
  750. */
  751. public virtual void removeElementAt(int location) {
  752. lock (this) {
  753. if (0 <= location && location < elementCount) {
  754. elementCount--;
  755. int size = elementCount - location;
  756. if (size > 0) {
  757. java.lang.SystemJ.arraycopy(elementData, location + 1, elementData,
  758. location, size);
  759. }
  760. elementData[elementCount] = default(E);
  761. modCount++;
  762. } else {
  763. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  764. }
  765. }
  766. }
  767. /**
  768. * Removes the objects in the specified range from the start to the, but not
  769. * including, end index. All elements with an index bigger than or equal to
  770. * {@code end} have their index decreased by {@code end - start}.
  771. *
  772. * @param start
  773. * the index at which to start removing.
  774. * @param end
  775. * the index one past the end of the range to remove.
  776. * @throws IndexOutOfBoundsException
  777. * if {@code start < 0, start > end} or
  778. * {@code end > size()}.
  779. */
  780. protected internal override void removeRange(int start, int end) {
  781. if (start >= 0 && start <= end && end <= elementCount) {
  782. if (start == end) {
  783. return;
  784. }
  785. if (end != elementCount) {
  786. java.lang.SystemJ.arraycopy(elementData, end, elementData, start,
  787. elementCount - end);
  788. int newCount = elementCount - (end - start);
  789. Arrays<E>.fill(elementData, newCount, elementCount, default(E));
  790. elementCount = newCount;
  791. } else {
  792. Arrays<E>.fill(elementData, start, elementCount, default(E));
  793. elementCount = start;
  794. }
  795. modCount++;
  796. } else {
  797. throw new java.lang.IndexOutOfBoundsException();
  798. }
  799. }
  800. /**
  801. * Removes all objects from this vector that are not contained in the
  802. * specified collection.
  803. *
  804. * @param collection
  805. * the collection of objects to retain.
  806. * @return {@code true} if this vector is modified, {@code false} otherwise.
  807. * @see #remove(Object)
  808. */
  809. public override bool retainAll(Collection<E> collection) {
  810. lock (this) {
  811. return base.retainAll(collection);
  812. }
  813. }
  814. /**
  815. * Replaces the element at the specified location in this vector with the
  816. * specified object.
  817. *
  818. * @param location
  819. * the index at which to put the specified object.
  820. * @param object
  821. * the object to add to this vector.
  822. * @return the previous element at the location.
  823. * @throws ArrayIndexOutOfBoundsException
  824. * if {@code location < 0 || location >= size()}.
  825. * @see #size
  826. */
  827. public override E set(int location, E obj) {
  828. lock (this) {
  829. if (location < elementCount) {
  830. E result = (E) elementData[location];
  831. elementData[location] = obj;
  832. return result;
  833. }
  834. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  835. }
  836. }
  837. /**
  838. * Replaces the element at the specified location in this vector with the
  839. * specified object.
  840. *
  841. * @param object
  842. * the object to add to this vector.
  843. * @param location
  844. * the index at which to put the specified object.
  845. * @throws ArrayIndexOutOfBoundsException
  846. * if {@code location < 0 || location >= size()}.
  847. * @see #size
  848. */
  849. public virtual void setElementAt(E obj, int location) {
  850. lock (this) {
  851. if (location < elementCount) {
  852. elementData[location] = obj;
  853. } else {
  854. throw new java.lang.ArrayIndexOutOfBoundsException(location);
  855. }
  856. }
  857. }
  858. /**
  859. * Sets the size of this vector to the specified size. If there are more
  860. * than length elements in this vector, the elements at end are lost. If
  861. * there are less than length elements in the vector, the additional
  862. * elements contain null.
  863. *
  864. * @param length
  865. * the new size of this vector.
  866. * @see #size
  867. */
  868. public virtual void setSize(int length) {
  869. lock (this) {
  870. if (length == elementCount) {
  871. return;
  872. }
  873. ensureCapacity(length);
  874. if (elementCount > length) {
  875. Arrays<E>.fill(elementData, length, elementCount, default(E));
  876. }
  877. elementCount = length;
  878. modCount++;
  879. }
  880. }
  881. /**
  882. * Returns the number of elements in this vector.
  883. *
  884. * @return the number of elements in this vector.
  885. * @see #elementCount
  886. * @see #lastElement
  887. */
  888. public override int size() {
  889. lock (this) {
  890. return elementCount;
  891. }
  892. }
  893. /**
  894. * Returns a List of the specified portion of this vector from the start
  895. * index to one less than the end index. The returned List is backed by this
  896. * vector so changes to one are reflected by the other.
  897. *
  898. * @param start
  899. * the index at which to start the sublist.
  900. * @param end
  901. * the index one past the end of the sublist.
  902. * @return a List of a portion of this vector.
  903. * @throws IndexOutOfBoundsException
  904. * if {@code start < 0} or {@code end > size()}.
  905. * @throws IllegalArgumentException
  906. * if {@code start > end}.
  907. */
  908. public override List<E> subList(int start, int end) {
  909. lock (this) {
  910. return new SynchronizedRandomAccessList<E>(base.subList(
  911. start, end), this);
  912. }
  913. }
  914. /**
  915. * Returns a new array containing all elements contained in this vector.
  916. *
  917. * @return an array of the elements from this vector.
  918. */
  919. public override Object[] toArray() {
  920. lock (this) {
  921. Object[] result = new Object[elementCount];
  922. java.lang.SystemJ.arraycopy(elementData, 0, result, 0, elementCount);
  923. return result;
  924. }
  925. }
  926. /**
  927. * Returns an array containing all elements contained in this vector. If the
  928. * specified array is large enough to hold the elements, the specified array
  929. * is used, otherwise an array of the same type is created. If the specified
  930. * array is used and is larger than this vector, the array element following
  931. * the collection elements is set to null.
  932. *
  933. * @param contents
  934. * the array to fill.
  935. * @return an array of the elements from this vector.
  936. * @throws ArrayStoreException
  937. * if the type of an element in this vector cannot be
  938. * stored in the type of the specified array.
  939. */
  940. public override T[] toArray<T>(T[] contents) {
  941. lock (this) {
  942. if (elementCount > contents.Length) {
  943. contents = new T[elementCount];
  944. }
  945. java.lang.SystemJ.arraycopy(elementData, 0, contents, 0, elementCount);
  946. if (elementCount < contents.Length) {
  947. contents[elementCount] = default(T);
  948. }
  949. return contents;
  950. }
  951. }
  952. /**
  953. * Returns the string representation of this vector.
  954. *
  955. * @return the string representation of this vector.
  956. * @see #elements
  957. */
  958. public override String ToString() {
  959. lock (this) {
  960. if (elementCount == 0) {
  961. return "[]"; //$NON-NLS-1$
  962. }
  963. int length = elementCount - 1;
  964. StringBuilder buffer = new StringBuilder(elementCount * 16);
  965. buffer.append('[');
  966. for (int i = 0; i < length; i++) {
  967. if (((Object)this) == ((Object)elementData[i])) {
  968. buffer.append("(this Collection)"); //$NON-NLS-1$
  969. } else {
  970. buffer.append(elementData[i]);
  971. }
  972. buffer.append(", "); //$NON-NLS-1$
  973. }
  974. if (((Object)elementData[length]) == ((Object)this)) {
  975. buffer.append("(this Collection)"); //$NON-NLS-1$
  976. } else {
  977. buffer.append(elementData[length]);
  978. }
  979. buffer.append(']');
  980. return buffer.toString();
  981. }
  982. }
  983. /**
  984. * Sets the capacity of this vector to be the same as the size.
  985. *
  986. * @see #capacity
  987. * @see #ensureCapacity
  988. * @see #size
  989. */
  990. public virtual void trimToSize() {
  991. lock (this) {
  992. if (elementData.Length != elementCount) {
  993. grow(elementCount);
  994. }
  995. }
  996. }
  997. /* private void writeObject(ObjectOutputStream stream)
  998. throws IOException {
  999. stream.defaultWriteObject();
  1000. }*/
  1001. }
  1002. #region IAC_VECTOR_ENUMERATION
  1003. internal class IAC_VECTOR_ENUMERATION<E> : Enumeration<E>{
  1004. internal int pos = 0;
  1005. private readonly Vector<E> root;
  1006. public IAC_VECTOR_ENUMERATION (Vector<E> v) {
  1007. this.root = v;
  1008. }
  1009. public bool hasMoreElements() {
  1010. return pos < root.elementCount;
  1011. }
  1012. public E nextElement() {
  1013. lock (this.root) {
  1014. if (pos < root.elementCount) {
  1015. return (E) root.elementData[pos++];
  1016. }
  1017. }
  1018. throw new java.util.NoSuchElementException();
  1019. }
  1020. }
  1021. #endregion
  1022. }