|
19 | 19 | import java.io.ObjectOutputStream; |
20 | 20 | import java.io.Serializable; |
21 | 21 | import java.io.StringReader; |
| 22 | +import java.util.ArrayDeque; |
22 | 23 | import java.util.ArrayList; |
23 | 24 | import java.util.HashMap; |
24 | 25 | import java.util.Iterator; |
@@ -415,79 +416,53 @@ public Iterator<SelectorEntry> getSelectorEntriesIteratorFor(final String elemen |
415 | 416 | } |
416 | 417 |
|
417 | 418 | static final class SelectorEntriesIterator implements Iterator<SelectorEntry> { |
418 | | - private Iterator<SelectorEntry> anyElementSelectors_; |
419 | | - private Iterator<SelectorEntry> elementSelectors_; |
420 | | - private Iterator<SelectorEntry> otherSelectors_; |
| 419 | + private ArrayDeque<Iterator<SelectorEntry>> iterators_; |
421 | 420 |
|
422 | 421 | SelectorEntriesIterator(final CSSStyleSheetRuleIndex index, final String elementName) { |
| 422 | + iterators_ = new ArrayDeque<Iterator<SelectorEntry>>(); |
423 | 423 | List<SelectorEntry> sel = index.elementSelectors_.get("*"); |
424 | 424 | if (sel != null && !sel.isEmpty()) { |
425 | | - anyElementSelectors_ = sel.iterator(); |
426 | | - } |
427 | | - else { |
428 | | - anyElementSelectors_ = null; |
| 425 | + iterators_.add(sel.iterator()); |
429 | 426 | } |
430 | 427 |
|
431 | 428 | sel = index.elementSelectors_.get(elementName); |
432 | 429 | if (sel != null && !sel.isEmpty()) { |
433 | | - elementSelectors_ = sel.iterator(); |
434 | | - } |
435 | | - else { |
436 | | - elementSelectors_ = null; |
| 430 | + iterators_.add(sel.iterator()); |
437 | 431 | } |
438 | 432 |
|
439 | 433 | if (index.otherSelectors_ != null && !index.otherSelectors_.isEmpty()) { |
440 | | - otherSelectors_ = index.otherSelectors_.iterator(); |
441 | | - } |
442 | | - else { |
443 | | - otherSelectors_ = null; |
| 434 | + iterators_.add(index.otherSelectors_.iterator()); |
444 | 435 | } |
445 | 436 | } |
446 | 437 |
|
447 | 438 | @Override |
448 | 439 | public SelectorEntry next() { |
449 | | - if (anyElementSelectors_ != null) { |
450 | | - if (anyElementSelectors_.hasNext()) { |
451 | | - return anyElementSelectors_.next(); |
452 | | - } |
453 | | - anyElementSelectors_ = null; |
| 440 | + if (iterators_.isEmpty()) { |
| 441 | + return null; |
454 | 442 | } |
455 | | - if (elementSelectors_ != null) { |
456 | | - if (elementSelectors_.hasNext()) { |
457 | | - return elementSelectors_.next(); |
458 | | - } |
459 | | - elementSelectors_ = null; |
460 | | - } |
461 | | - if (otherSelectors_ != null) { |
462 | | - if (otherSelectors_.hasNext()) { |
463 | | - return otherSelectors_.next(); |
464 | | - } |
465 | | - otherSelectors_ = null; |
| 443 | + |
| 444 | + final Iterator<SelectorEntry> iter = iterators_.peek(); |
| 445 | + if (iter.hasNext()) { |
| 446 | + return iter.next(); |
466 | 447 | } |
467 | | - return null; |
| 448 | + |
| 449 | + iterators_.pop(); |
| 450 | + return next(); |
468 | 451 | } |
469 | 452 |
|
470 | 453 | @Override |
471 | 454 | public boolean hasNext() { |
472 | | - if (anyElementSelectors_ != null) { |
473 | | - if (anyElementSelectors_.hasNext()) { |
474 | | - return true; |
475 | | - } |
476 | | - anyElementSelectors_ = null; |
477 | | - } |
478 | | - if (elementSelectors_ != null) { |
479 | | - if (elementSelectors_.hasNext()) { |
480 | | - return true; |
481 | | - } |
482 | | - elementSelectors_ = null; |
| 455 | + if (iterators_.isEmpty()) { |
| 456 | + return false; |
483 | 457 | } |
484 | | - if (otherSelectors_ != null) { |
485 | | - if (otherSelectors_.hasNext()) { |
486 | | - return true; |
487 | | - } |
488 | | - otherSelectors_ = null; |
| 458 | + |
| 459 | + final Iterator<SelectorEntry> iter = iterators_.peek(); |
| 460 | + if (iter.hasNext()) { |
| 461 | + return true; |
489 | 462 | } |
490 | | - return false; |
| 463 | + |
| 464 | + iterators_.pop(); |
| 465 | + return hasNext(); |
491 | 466 | } |
492 | 467 | } |
493 | 468 | } |
0 commit comments