PageEventList.java 5.45 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.PageModification
 *  com.day.cq.wcm.api.PageModification$ModificationType
 *  com.day.text.Text
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.wcm.api.PageModification;
import com.day.cq.wcm.msm.impl.BlueprintEvent;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

class PageEventList {
    private final ArrayList<List<PageModificationList>> entries = new ArrayList();

    PageEventList() {
    }

    public void addEvent(BlueprintEvent blueprintEvent) {
        List targetScope = null;
        PageModificationList map = new PageModificationList(blueprintEvent.getModifications(null));
        Iterator<List<PageModificationList>> scopes = this.entries.iterator();
        while (targetScope == null && scopes.hasNext()) {
            targetScope = scopes.next();
            Iterator itr = targetScope.iterator();
            while (itr.hasNext() && targetScope != null) {
                PageModificationList other = (PageModificationList)itr.next();
                if (!map.overlaps(other)) continue;
                targetScope = null;
            }
        }
        if (targetScope == null) {
            targetScope = new ArrayList<PageModificationList>();
            this.entries.add(targetScope);
        }
        targetScope.add((PageModificationList)new PageModificationList(blueprintEvent.getModifications(null)));
    }

    public Iterator<List<PageModificationList>> getNonOverlapping() {
        return this.entries.iterator();
    }

    static class PageModificationList
    implements Iterable<PageModification> {
        private final TreeMap<String, List<PageModification>> branchToEntry;
        private final TreeSet<String> rootIndex;

        PageModificationList(Iterator<PageModification> modifications) {
            this.branchToEntry = new TreeMap(new Comparator<String>(){

                @Override
                public int compare(String o1, String o2) {
                    return Text.compareHandles((String)o1, (String)o2);
                }
            });
            this.rootIndex = new TreeSet();
            while (modifications.hasNext()) {
                this.add(modifications.next());
            }
        }

        public void add(PageModification pageModification) {
            String path = pageModification.getPath();
            List<PageModification> values = this.branchToEntry.get(path);
            if (values == null) {
                values = new ArrayList<PageModification>();
                this.branchToEntry.put(path, values);
                if (this.rootIndex.isEmpty()) {
                    this.rootIndex.add(path);
                } else {
                    String lower;
                    String higher = this.rootIndex.higher(path);
                    if (higher != null) {
                        if (!Text.isDescendant((String)path, (String)higher)) {
                            this.rootIndex.add(path);
                        } else {
                            this.rootIndex.remove(higher);
                        }
                    }
                    if ((lower = this.rootIndex.lower(path)) != null) {
                        if (Text.isDescendant((String)lower, (String)path)) {
                            this.rootIndex.remove(path);
                        } else {
                            this.rootIndex.add(path);
                        }
                    }
                }
            }
            values.add(pageModification);
        }

        @Override
        public Iterator<PageModification> iterator() {
            final Iterator<List<PageModification>> values = this.branchToEntry.values().iterator();
            return new Iterator<PageModification>(){
                private PageModification next;
                private Iterator<PageModification> list;

                @Override
                public boolean hasNext() {
                    if (this.next == null) {
                        if ((this.list == null || !this.list.hasNext()) && values.hasNext()) {
                            this.list = ((List)values.next()).iterator();
                        }
                        if (this.list != null && this.list.hasNext()) {
                            this.next = this.list.next();
                        }
                    }
                    return this.next != null;
                }

                @Override
                public PageModification next() {
                    PageModification ret = this.next;
                    this.next = null;
                    return ret;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove not implemented");
                }
            };
        }

        boolean overlaps(PageModificationList other) {
            for (String root : other.getRoots()) {
                for (String myRoots : this.getRoots()) {
                    if (!Text.isDescendantOrEqual((String)root, (String)myRoots) && !Text.isDescendantOrEqual((String)myRoots, (String)root)) continue;
                    return true;
                }
            }
            return false;
        }

        SortedSet<String> getRoots() {
            return this.rootIndex;
        }

    }

}