Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContentList.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*--
  2. $Id: ContentList.java,v 1.42 2007/11/10 05:28:58 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org).
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom;
  46. import java.util.*;
  47. import org.jdom.filter.*;
  48. /**
  49. * A non-public list implementation holding only legal JDOM content, including
  50. * content for Document or Element nodes. Users see this class as a simple List
  51. * implementation.
  52. *
  53. * @see CDATA
  54. * @see Comment
  55. * @see Element
  56. * @see EntityRef
  57. * @see ProcessingInstruction
  58. * @see Text
  59. *
  60. * @version $Revision: 1.42 $, $Date: 2007/11/10 05:28:58 $
  61. * @author Alex Rosen
  62. * @author Philippe Riand
  63. * @author Bradley S. Huffman
  64. */
  65. final class ContentList extends AbstractList implements java.io.Serializable {
  66. private static final String CVS_ID =
  67. "@(#) $RCSfile: ContentList.java,v $ $Revision: 1.42 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1 $";
  68. private static final long serialVersionUID = 1L;
  69. private static final int INITIAL_ARRAY_SIZE = 5;
  70. /** Our backing list */
  71. private Content elementData[];
  72. private int size;
  73. /** Document or Element this list belongs to */
  74. private Parent parent;
  75. /** Force either a Document or Element parent */
  76. ContentList(Parent parent) {
  77. this.parent = parent;
  78. }
  79. /**
  80. * Package internal method to support building from sources that are
  81. * 100% trusted.
  82. *
  83. * @param c content to add without any checks
  84. */
  85. final void uncheckedAddContent(Content c) {
  86. c.parent = parent;
  87. ensureCapacity(size + 1);
  88. elementData[size++] = c;
  89. modCount++;
  90. }
  91. /**
  92. * Inserts the specified object at the specified position in this list.
  93. * Shifts the object currently at that position (if any) and any
  94. * subsequent objects to the right (adds one to their indices).
  95. *
  96. * @param index The location to set the value to.
  97. * @param obj The object to insert into the list.
  98. * throws IndexOutOfBoundsException if index < 0 || index > size()
  99. */
  100. public void add(int index, Object obj) {
  101. if (obj == null) {
  102. throw new IllegalAddException("Cannot add null object");
  103. }
  104. if (obj instanceof String) { // String is OK to add as special case
  105. obj = new Text(obj.toString()); // wrap it as a Content
  106. }
  107. if ((obj instanceof Content)) {
  108. add(index, (Content) obj);
  109. } else {
  110. throw new IllegalAddException("Class " +
  111. obj.getClass().getName() +
  112. " is of unrecognized type and cannot be added");
  113. }
  114. }
  115. /**
  116. * @see org.jdom.ContentList#add(int, org.jdom.Content)
  117. */
  118. private void documentCanContain(int index, Content child) throws IllegalAddException {
  119. if (child instanceof Element) {
  120. if (indexOfFirstElement() >= 0) {
  121. throw new IllegalAddException(
  122. "Cannot add a second root element, only one is allowed");
  123. }
  124. if (indexOfDocType() > index) {
  125. throw new IllegalAddException(
  126. "A root element cannot be added before the DocType");
  127. }
  128. }
  129. if (child instanceof DocType) {
  130. if (indexOfDocType() >= 0) {
  131. throw new IllegalAddException(
  132. "Cannot add a second doctype, only one is allowed");
  133. }
  134. int firstElt = indexOfFirstElement();
  135. if (firstElt != -1 && firstElt < index) {
  136. throw new IllegalAddException(
  137. "A DocType cannot be added after the root element");
  138. }
  139. }
  140. if (child instanceof CDATA) {
  141. throw new IllegalAddException("A CDATA is not allowed at the document root");
  142. }
  143. if (child instanceof Text) {
  144. throw new IllegalAddException("A Text is not allowed at the document root");
  145. }
  146. if (child instanceof EntityRef) {
  147. throw new IllegalAddException("An EntityRef is not allowed at the document root");
  148. }
  149. }
  150. private static void elementCanContain(int index, Content child) throws IllegalAddException {
  151. if (child instanceof DocType) {
  152. throw new IllegalAddException(
  153. "A DocType is not allowed except at the document level");
  154. }
  155. }
  156. /**
  157. * Check and add the <code>Element</code> to this list at
  158. * the given index.
  159. *
  160. * @param index index where to add <code>Element</code>
  161. * @param child <code>Element</code> to add
  162. */
  163. void add(int index, Content child) {
  164. if (child == null) {
  165. throw new IllegalAddException("Cannot add null object");
  166. }
  167. if (parent instanceof Document) {
  168. documentCanContain(index, child);
  169. }
  170. else {
  171. elementCanContain(index, child);
  172. }
  173. if (child.getParent() != null) {
  174. Parent p = child.getParent();
  175. if (p instanceof Document) {
  176. throw new IllegalAddException((Element)child,
  177. "The Content already has an existing parent document");
  178. }
  179. else {
  180. throw new IllegalAddException(
  181. "The Content already has an existing parent \"" +
  182. ((Element)p).getQualifiedName() + "\"");
  183. }
  184. }
  185. if (child == parent) {
  186. throw new IllegalAddException(
  187. "The Element cannot be added to itself");
  188. }
  189. // Detect if we have <a><b><c/></b></a> and c.add(a)
  190. if ((parent instanceof Element && child instanceof Element) &&
  191. ((Element) child).isAncestor((Element)parent)) {
  192. throw new IllegalAddException(
  193. "The Element cannot be added as a descendent of itself");
  194. }
  195. if (index<0 || index>size) {
  196. throw new IndexOutOfBoundsException("Index: " + index +
  197. " Size: " + size());
  198. }
  199. child.setParent(parent);
  200. ensureCapacity(size+1);
  201. if( index==size ) {
  202. elementData[size++] = child;
  203. } else {
  204. System.arraycopy(elementData, index, elementData, index + 1, size - index);
  205. elementData[index] = child;
  206. size++;
  207. }
  208. modCount++;
  209. }
  210. /**
  211. * Add the specified collecton to the end of this list.
  212. *
  213. * @param collection The collection to add to the list.
  214. * @return <code>true</code> if the list was modified as a result of
  215. * the add.
  216. */
  217. public boolean addAll(Collection collection) {
  218. return addAll(size(), collection);
  219. }
  220. /**
  221. * Inserts the specified collecton at the specified position in this list.
  222. * Shifts the object currently at that position (if any) and any
  223. * subsequent objects to the right (adds one to their indices).
  224. *
  225. * @param index The offset to start adding the data in the collection
  226. * @param collection The collection to insert into the list.
  227. * @return <code>true</code> if the list was modified as a result of
  228. * the add.
  229. * throws IndexOutOfBoundsException if index < 0 || index > size()
  230. */
  231. public boolean addAll(int index, Collection collection) {
  232. if (index<0 || index>size) {
  233. throw new IndexOutOfBoundsException("Index: " + index +
  234. " Size: " + size());
  235. }
  236. if ((collection == null) || (collection.size() == 0)) {
  237. return false;
  238. }
  239. ensureCapacity(size() + collection.size());
  240. int count = 0;
  241. try {
  242. Iterator i = collection.iterator();
  243. while (i.hasNext()) {
  244. Object obj = i.next();
  245. add(index + count, obj);
  246. count++;
  247. }
  248. }
  249. catch (RuntimeException exception) {
  250. for (int i = 0; i < count; i++) {
  251. remove(index);
  252. }
  253. throw exception;
  254. }
  255. return true;
  256. }
  257. /**
  258. * Clear the current list.
  259. */
  260. public void clear() {
  261. if (elementData != null) {
  262. for (int i = 0; i < size; i++) {
  263. Content obj = elementData[i];
  264. removeParent(obj);
  265. }
  266. elementData = null;
  267. size = 0;
  268. }
  269. modCount++;
  270. }
  271. /**
  272. * Clear the current list and set it to the contents
  273. * of the <code>Collection</code>.
  274. * object.
  275. *
  276. * @param collection The collection to use.
  277. */
  278. void clearAndSet(Collection collection) {
  279. Content[] old = elementData;
  280. int oldSize = size;
  281. elementData = null;
  282. size = 0;
  283. if ((collection != null) && (collection.size() != 0)) {
  284. ensureCapacity(collection.size());
  285. try {
  286. addAll(0, collection);
  287. }
  288. catch (RuntimeException exception) {
  289. elementData = old;
  290. size = oldSize;
  291. throw exception;
  292. }
  293. }
  294. if (old != null) {
  295. for (int i = 0; i < oldSize; i++) {
  296. removeParent(old[i]);
  297. }
  298. }
  299. modCount++;
  300. }
  301. /**
  302. * Increases the capacity of this <code>ContentList</code> instance,
  303. * if necessary, to ensure that it can hold at least the number of
  304. * items specified by the minimum capacity argument.
  305. *
  306. * @param minCapacity the desired minimum capacity.
  307. */
  308. void ensureCapacity(int minCapacity) {
  309. if( elementData==null ) {
  310. elementData = new Content[Math.max(minCapacity, INITIAL_ARRAY_SIZE)];
  311. } else {
  312. int oldCapacity = elementData.length;
  313. if (minCapacity > oldCapacity) {
  314. Object oldData[] = elementData;
  315. int newCapacity = (oldCapacity * 3)/2 + 1;
  316. if (newCapacity < minCapacity)
  317. newCapacity = minCapacity;
  318. elementData = new Content[newCapacity];
  319. System.arraycopy(oldData, 0, elementData, 0, size);
  320. }
  321. }
  322. }
  323. /**
  324. * Return the object at the specified offset.
  325. *
  326. * @param index The offset of the object.
  327. * @return The Object which was returned.
  328. */
  329. public Object get(int index) {
  330. if (index<0 || index>=size) {
  331. throw new IndexOutOfBoundsException("Index: " + index +
  332. " Size: " + size());
  333. }
  334. return elementData[index];
  335. }
  336. /**
  337. * Return a view of this list based on the given filter.
  338. *
  339. * @param filter <code>Filter</code> for this view.
  340. * @return a list representing the rules of the <code>Filter</code>.
  341. */
  342. List getView(Filter filter) {
  343. return new FilterList(filter);
  344. }
  345. /**
  346. * Return the index of the first Element in the list. If the parent
  347. * is a <code>Document</code> then the element is the root element.
  348. * If the list contains no Elements, it returns -1.
  349. *
  350. * @return index of first element, or -1 if one doesn't exist
  351. */
  352. int indexOfFirstElement() {
  353. if( elementData!=null ) {
  354. for (int i = 0; i < size; i++) {
  355. if (elementData[i] instanceof Element) {
  356. return i;
  357. }
  358. }
  359. }
  360. return -1;
  361. }
  362. /**
  363. * Return the index of the DocType element in the list. If the list contains
  364. * no DocType, it returns -1.
  365. *
  366. * @return index of the DocType, or -1 if it doesn't
  367. * exist
  368. */
  369. int indexOfDocType() {
  370. if (elementData != null) {
  371. for (int i = 0; i < size; i++) {
  372. if (elementData[i] instanceof DocType) {
  373. return i;
  374. }
  375. }
  376. }
  377. return -1;
  378. }
  379. /**
  380. * Remove the object at the specified offset.
  381. *
  382. * @param index The offset of the object.
  383. * @return The Object which was removed.
  384. */
  385. public Object remove(int index) {
  386. if (index<0 || index>=size)
  387. throw new IndexOutOfBoundsException("Index: " + index +
  388. " Size: " + size());
  389. Content old = elementData[index];
  390. removeParent(old);
  391. int numMoved = size - index - 1;
  392. if (numMoved > 0)
  393. System.arraycopy(elementData, index+1, elementData, index,numMoved);
  394. elementData[--size] = null; // Let gc do its work
  395. modCount++;
  396. return old;
  397. }
  398. /** Remove the parent of a Object */
  399. private static void removeParent(Content c) {
  400. c.setParent(null);
  401. }
  402. /**
  403. * Set the object at the specified location to the supplied
  404. * object.
  405. *
  406. * @param index The location to set the value to.
  407. * @param obj The location to set the value to.
  408. * @return The object which was replaced.
  409. * throws IndexOutOfBoundsException if index < 0 || index >= size()
  410. */
  411. public Object set(int index, Object obj) {
  412. if (index<0 || index>=size)
  413. throw new IndexOutOfBoundsException("Index: " + index +
  414. " Size: " + size());
  415. if ((obj instanceof Element) && (parent instanceof Document)) {
  416. int root = indexOfFirstElement();
  417. if ((root >= 0) && (root != index)) {
  418. throw new IllegalAddException(
  419. "Cannot add a second root element, only one is allowed");
  420. }
  421. }
  422. if ((obj instanceof DocType) && (parent instanceof Document)) {
  423. int docTypeIndex = indexOfDocType();
  424. if ((docTypeIndex >= 0) && (docTypeIndex != index)) {
  425. throw new IllegalAddException(
  426. "Cannot add a second doctype, only one is allowed");
  427. }
  428. }
  429. Object old = remove(index);
  430. try {
  431. add(index, obj);
  432. }
  433. catch (RuntimeException exception) {
  434. add(index, old);
  435. throw exception;
  436. }
  437. return old;
  438. }
  439. /**
  440. * Return the number of items in this list
  441. *
  442. * @return The number of items in this list.
  443. */
  444. public int size() {
  445. return size;
  446. }
  447. /**
  448. * Return this list as a <code>String</code>
  449. *
  450. * @return The number of items in this list.
  451. */
  452. public String toString() {
  453. return super.toString();
  454. }
  455. /** Give access of ContentList.modCount to FilterList */
  456. private int getModCount() {
  457. return modCount;
  458. }
  459. /* * * * * * * * * * * * * FilterList * * * * * * * * * * * * * * * */
  460. /* * * * * * * * * * * * * FilterList * * * * * * * * * * * * * * * */
  461. /**
  462. * <code>FilterList</code> represents legal JDOM content, including content
  463. * for <code>Document</code>s or <code>Element</code>s.
  464. */
  465. class FilterList extends AbstractList implements java.io.Serializable {
  466. /** The Filter */
  467. Filter filter;
  468. /** Current number of items in this view */
  469. int count = 0;
  470. /** Expected modCount in our backing list */
  471. int expected = -1;
  472. // Implementation Note: Directly after size() is called, expected
  473. // is sync'd with ContentList.modCount and count provides
  474. // the true size of this view. Before the first call to
  475. // size() or if the backing list is modified outside this
  476. // FilterList, both might contain bogus values and should
  477. // not be used without first calling size();
  478. /**
  479. * Create a new instance of the FilterList with the specified Filter.
  480. */
  481. FilterList(Filter filter) {
  482. this.filter = filter;
  483. }
  484. /**
  485. * Inserts the specified object at the specified position in this list.
  486. * Shifts the object currently at that position (if any) and any
  487. * subsequent objects to the right (adds one to their indices).
  488. *
  489. * @param index The location to set the value to.
  490. * @param obj The object to insert into the list.
  491. * throws IndexOutOfBoundsException if index < 0 || index > size()
  492. */
  493. public void add(int index, Object obj) {
  494. if (filter.matches(obj)) {
  495. int adjusted = getAdjustedIndex(index);
  496. ContentList.this.add(adjusted, obj);
  497. expected++;
  498. count++;
  499. }
  500. else throw new IllegalAddException("Filter won't allow the " +
  501. obj.getClass().getName() +
  502. " '" + obj + "' to be added to the list");
  503. }
  504. /**
  505. * Return the object at the specified offset.
  506. *
  507. * @param index The offset of the object.
  508. * @return The Object which was returned.
  509. */
  510. public Object get(int index) {
  511. int adjusted = getAdjustedIndex(index);
  512. return ContentList.this.get(adjusted);
  513. }
  514. public Iterator iterator() {
  515. return new FilterListIterator(filter, 0);
  516. }
  517. public ListIterator listIterator() {
  518. return new FilterListIterator(filter, 0);
  519. }
  520. public ListIterator listIterator(int index) {
  521. return new FilterListIterator(filter, index);
  522. }
  523. /**
  524. * Remove the object at the specified offset.
  525. *
  526. * @param index The offset of the object.
  527. * @return The Object which was removed.
  528. */
  529. public Object remove(int index) {
  530. int adjusted = getAdjustedIndex(index);
  531. Object old = ContentList.this.get(adjusted);
  532. if (filter.matches(old)) {
  533. old = ContentList.this.remove(adjusted);
  534. expected++;
  535. count--;
  536. }
  537. else {
  538. throw new IllegalAddException("Filter won't allow the " +
  539. (old.getClass()).getName() +
  540. " '" + old + "' (index " + index +
  541. ") to be removed");
  542. }
  543. return old;
  544. }
  545. /**
  546. * Set the object at the specified location to the supplied
  547. * object.
  548. *
  549. * @param index The location to set the value to.
  550. * @param obj The location to set the value to.
  551. * @return The object which was replaced.
  552. * throws IndexOutOfBoundsException if index < 0 || index >= size()
  553. */
  554. public Object set(int index, Object obj) {
  555. Object old = null;
  556. if (filter.matches(obj)) {
  557. int adjusted = getAdjustedIndex(index);
  558. old = ContentList.this.get(adjusted);
  559. if (!filter.matches(old)) {
  560. throw new IllegalAddException("Filter won't allow the " +
  561. (old.getClass()).getName() +
  562. " '" + old + "' (index " + index +
  563. ") to be removed");
  564. }
  565. old = ContentList.this.set(adjusted, obj);
  566. expected += 2;
  567. }
  568. else {
  569. throw new IllegalAddException("Filter won't allow index " +
  570. index + " to be set to " +
  571. (obj.getClass()).getName());
  572. }
  573. return old;
  574. }
  575. /**
  576. * Return the number of items in this list
  577. *
  578. * @return The number of items in this list.
  579. */
  580. public int size() {
  581. // Implementation Note: Directly after size() is called, expected
  582. // is sync'd with ContentList.modCount and count provides
  583. // the true size of this view. Before the first call to
  584. // size() or if the backing list is modified outside this
  585. // FilterList, both might contain bogus values and should
  586. // not be used without first calling size();
  587. if (expected == ContentList.this.getModCount()) {
  588. return count;
  589. }
  590. count = 0;
  591. for (int i = 0; i < ContentList.this.size(); i++) {
  592. Object obj = ContentList.this.elementData[i];
  593. if (filter.matches(obj)) {
  594. count++;
  595. }
  596. }
  597. expected = ContentList.this.getModCount();
  598. return count;
  599. }
  600. /**
  601. * Return the adjusted index
  602. *
  603. * @param index Index of in this view.
  604. * @return True index in backing list
  605. */
  606. final private int getAdjustedIndex(int index) {
  607. int adjusted = 0;
  608. for (int i = 0; i < ContentList.this.size; i++) {
  609. Object obj = ContentList.this.elementData[i];
  610. if (filter.matches(obj)) {
  611. if (index == adjusted) {
  612. return i;
  613. }
  614. adjusted++;
  615. }
  616. }
  617. if (index == adjusted) {
  618. return ContentList.this.size;
  619. }
  620. return ContentList.this.size + 1;
  621. }
  622. }
  623. /* * * * * * * * * * * * * FilterListIterator * * * * * * * * * * * */
  624. /* * * * * * * * * * * * * FilterListIterator * * * * * * * * * * * */
  625. class FilterListIterator implements ListIterator {
  626. /** The Filter that applies */
  627. Filter filter;
  628. /** Whether this iterator is in forward or reverse. */
  629. private boolean forward = false;
  630. /** Whether a call to remove() is valid */
  631. private boolean canremove = false;
  632. /** Whether a call to set() is valid */
  633. private boolean canset = false;
  634. /** Index in backing list of next object */
  635. private int cursor = -1;
  636. /** the backing index to use if we actually DO move */
  637. private int tmpcursor = -1;
  638. /** Index in ListIterator */
  639. private int index = -1;
  640. /** Expected modCount in our backing list */
  641. private int expected = -1;
  642. /** Number of elements matching the filter. */
  643. private int fsize = 0;
  644. /**
  645. * Default constructor
  646. */
  647. FilterListIterator(Filter filter, int start) {
  648. this.filter = filter;
  649. expected = ContentList.this.getModCount();
  650. // always start list iterators in backward mode ....
  651. // it makes sense... really.
  652. forward = false;
  653. if (start < 0) {
  654. throw new IndexOutOfBoundsException("Index: " + start);
  655. }
  656. // the number of matching elements....
  657. fsize = 0;
  658. // go through the list, count the matching elements...
  659. for (int i = 0; i < ContentList.this.size(); i++) {
  660. if (filter.matches(ContentList.this.get(i))) {
  661. if (start == fsize) {
  662. // set the back-end cursor to the matching element....
  663. cursor = i;
  664. // set the front-end cursor too.
  665. index = fsize;
  666. }
  667. fsize++;
  668. }
  669. }
  670. if (start > fsize) {
  671. throw new IndexOutOfBoundsException("Index: " + start + " Size: " + fsize);
  672. }
  673. if (cursor == -1) {
  674. // implies that start == fsize (i.e. after the last element
  675. // put the insertion point at the end of the Underlying
  676. // content list ....
  677. // i.e. an add() at this point may potentially end up with
  678. // filtered content between previous() and next()
  679. // the alternative is to put the cursor on the Content after
  680. // the last Content that the filter passed
  681. // The implications are ambiguous.
  682. cursor = ContentList.this.size();
  683. index = fsize;
  684. }
  685. }
  686. /**
  687. * Returns <code>true</code> if this list iterator has a next element.
  688. */
  689. public boolean hasNext() {
  690. return nextIndex() < fsize;
  691. }
  692. /**
  693. * Returns the next element in the list.
  694. */
  695. public Object next() {
  696. if (!hasNext())
  697. throw new NoSuchElementException("next() is beyond the end of the Iterator");
  698. index = nextIndex();
  699. cursor = tmpcursor;
  700. forward = true;
  701. canremove = true;
  702. canset = true;
  703. return ContentList.this.get(cursor);
  704. }
  705. /**
  706. * Returns <code>true</code> if this list iterator has more elements
  707. * when traversing the list in the reverse direction.
  708. */
  709. public boolean hasPrevious() {
  710. return previousIndex() >= 0;
  711. }
  712. /**
  713. * Returns the previous element in the list.
  714. */
  715. public Object previous() {
  716. if (!hasPrevious())
  717. throw new NoSuchElementException("previous() is before the start of the Iterator");
  718. index = previousIndex();
  719. cursor = tmpcursor;
  720. forward = false;
  721. canremove = true;
  722. canset = true;
  723. return ContentList.this.get(cursor);
  724. }
  725. /**
  726. * Returns the index of the element that would be returned by a
  727. * subsequent call to <code>next</code>.
  728. */
  729. public int nextIndex() {
  730. checkConcurrentModification();
  731. if (forward) {
  732. // Starting with next possibility ....
  733. for (int i = cursor + 1; i < ContentList.this.size(); i++) {
  734. if (filter.matches(ContentList.this.get(i))) {
  735. tmpcursor = i;
  736. return index + 1;
  737. }
  738. }
  739. // Never found another match.... put the insertion point at
  740. // the end of the list....
  741. tmpcursor = ContentList.this.size();
  742. return index + 1;
  743. }
  744. // We've been going back... so nextIndex() returns the same
  745. // element.
  746. tmpcursor = cursor;
  747. return index;
  748. }
  749. /**
  750. * Returns the index of the element that would be returned by a
  751. * subsequent call to <code>previous</code>. (Returns -1 if the
  752. * list iterator is at the beginning of the list.)
  753. */
  754. public int previousIndex() {
  755. checkConcurrentModification();
  756. if (!forward) {
  757. // starting with next possibility ....
  758. for (int i = cursor - 1; i >= 0; i--) {
  759. if (filter.matches(ContentList.this.get(i))) {
  760. tmpcursor = i;
  761. return index - 1;
  762. }
  763. }
  764. // Never found another match.... put the insertion point at
  765. // the start of the list....
  766. tmpcursor = -1;
  767. return index - 1;
  768. }
  769. // We've been going forwards... so previousIndex() returns same
  770. // element.
  771. tmpcursor = cursor;
  772. return index;
  773. }
  774. /**
  775. * Inserts the specified element into the list.
  776. */
  777. public void add(Object obj) {
  778. // Call to nextIndex() will check concurrent.
  779. nextIndex();
  780. // tmpcursor is the backing cursor of the next element
  781. // Remember that List.add(index,obj) is really an insert....
  782. ContentList.this.add(tmpcursor, obj);
  783. forward = true;
  784. expected = ContentList.this.getModCount();
  785. canremove = canset = false;
  786. index = nextIndex();
  787. cursor = tmpcursor;
  788. fsize++;
  789. }
  790. /**
  791. * Removes from the list the last element that was returned by
  792. * the last call to <code>next</code> or <code>previous</code>.
  793. */
  794. public void remove() {
  795. if (!canremove)
  796. throw new IllegalStateException("Can not remove an "
  797. + "element unless either next() or previous() has been called "
  798. + "since the last remove()");
  799. nextIndex(); // to get out cursor ...
  800. ContentList.this.remove(cursor);
  801. cursor = tmpcursor - 1;
  802. expected = ContentList.this.getModCount();
  803. forward = false;
  804. canremove = false;
  805. canset = false;
  806. fsize--;
  807. }
  808. /**
  809. * Replaces the last element returned by <code>next</code> or
  810. * <code>previous</code> with the specified element.
  811. */
  812. public void set(Object obj) {
  813. if (!canset)
  814. throw new IllegalStateException("Can not set an element "
  815. + "unless either next() or previous() has been called since the "
  816. + "last remove() or set()");
  817. checkConcurrentModification();
  818. if (!filter.matches(obj)) {
  819. throw new IllegalAddException("Filter won't allow index " + index + " to be set to "
  820. + (obj.getClass()).getName());
  821. }
  822. ContentList.this.set(cursor, obj);
  823. expected = ContentList.this.getModCount();
  824. }
  825. /**
  826. * Check if are backing list is being modified by someone else.
  827. */
  828. private void checkConcurrentModification() {
  829. if (expected != ContentList.this.getModCount()) {
  830. throw new ConcurrentModificationException();
  831. }
  832. }
  833. }
  834. }