Skip to content

Commit c20a1c5

Browse files
committed
simplify
1 parent f4d0999 commit c20a1c5

File tree

1 file changed

+24
-49
lines changed

1 file changed

+24
-49
lines changed

src/main/java/com/gargoylesoftware/css/dom/CSSStyleSheetImpl.java

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ObjectOutputStream;
2020
import java.io.Serializable;
2121
import java.io.StringReader;
22+
import java.util.ArrayDeque;
2223
import java.util.ArrayList;
2324
import java.util.HashMap;
2425
import java.util.Iterator;
@@ -415,79 +416,53 @@ public Iterator<SelectorEntry> getSelectorEntriesIteratorFor(final String elemen
415416
}
416417

417418
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_;
421420

422421
SelectorEntriesIterator(final CSSStyleSheetRuleIndex index, final String elementName) {
422+
iterators_ = new ArrayDeque<Iterator<SelectorEntry>>();
423423
List<SelectorEntry> sel = index.elementSelectors_.get("*");
424424
if (sel != null && !sel.isEmpty()) {
425-
anyElementSelectors_ = sel.iterator();
426-
}
427-
else {
428-
anyElementSelectors_ = null;
425+
iterators_.add(sel.iterator());
429426
}
430427

431428
sel = index.elementSelectors_.get(elementName);
432429
if (sel != null && !sel.isEmpty()) {
433-
elementSelectors_ = sel.iterator();
434-
}
435-
else {
436-
elementSelectors_ = null;
430+
iterators_.add(sel.iterator());
437431
}
438432

439433
if (index.otherSelectors_ != null && !index.otherSelectors_.isEmpty()) {
440-
otherSelectors_ = index.otherSelectors_.iterator();
441-
}
442-
else {
443-
otherSelectors_ = null;
434+
iterators_.add(index.otherSelectors_.iterator());
444435
}
445436
}
446437

447438
@Override
448439
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;
454442
}
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();
466447
}
467-
return null;
448+
449+
iterators_.pop();
450+
return next();
468451
}
469452

470453
@Override
471454
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;
483457
}
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;
489462
}
490-
return false;
463+
464+
iterators_.pop();
465+
return hasNext();
491466
}
492467
}
493468
}

0 commit comments

Comments
 (0)