Changeset 15986 in josm


Ignore:
Timestamp:
2020-03-01T23:36:02+01:00 (5 years ago)
Author:
simon04
Message:

see #18802 - Add Selector.getBase

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerIndex.java

    r15104 r15986  
    8484
    8585            for (Selector s : c.rule.selectors) {
    86                 // find the rightmost selector, this must be a GeneralSelector
    87                 boolean hasLeftRightSel = false;
    88                 Selector selRightmost = s;
    89                 while (selRightmost instanceof Selector.ChildOrParentSelector) {
    90                     hasLeftRightSel = true;
    91                     selRightmost = ((Selector.ChildOrParentSelector) selRightmost).right;
    92                 }
     86                boolean hasLeftRightSel = s instanceof Selector.ChildOrParentSelector;
    9387                if (!allTests && !hasLeftRightSel) {
    9488                    continue;
     
    9892
    9993                ruleToCheckMap.put(optRule, c);
    100                 final String base = ((GeneralSelector) selRightmost).getBase();
     94                final String base = s.getBase();
    10195                switch (base) {
    10296                case Selector.BASE_NODE:
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r15949 r15986  
    1717import java.util.ArrayList;
    1818import java.util.BitSet;
     19import java.util.Collection;
    1920import java.util.Collections;
    2021import java.util.HashMap;
     
    2627import java.util.Map.Entry;
    2728import java.util.NoSuchElementException;
     29import java.util.Optional;
    2830import java.util.Set;
    2931import java.util.concurrent.locks.ReadWriteLock;
     
    5658import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyValueCondition;
    5759import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.SimpleKeyValueCondition;
    58 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
    5960import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
    60 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.OptimizedGeneralSelector;
    6161import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
    6262import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
     
    292292            for (int ruleIndex = 0; ruleIndex < rules.size(); ruleIndex++) {
    293293                MapCSSRule r = rules.get(ruleIndex);
    294                 // find the rightmost selector, this must be a GeneralSelector
    295                 Selector selRightmost = r.selector;
    296                 while (selRightmost instanceof ChildOrParentSelector) {
    297                     selRightmost = ((ChildOrParentSelector) selRightmost).right;
    298                 }
    299                 OptimizedGeneralSelector s = (OptimizedGeneralSelector) selRightmost;
    300                 if (s.conds == null) {
     294                final List<Condition> conditions = r.selector.getConditions();
     295                if (conditions == null || conditions.isEmpty()) {
    301296                    remaining.set(ruleIndex);
    302297                    continue;
    303298                }
    304                 List<SimpleKeyValueCondition> sk = new ArrayList<>(Utils.filteredCollection(s.conds,
    305                         SimpleKeyValueCondition.class));
    306                 if (!sk.isEmpty()) {
    307                     SimpleKeyValueCondition c = sk.get(sk.size() - 1);
    308                     getEntryInIndex(c.k).addForKeyAndValue(c.v, ruleIndex);
     299                Optional<SimpleKeyValueCondition> lastCondition = Utils.filteredCollection(conditions, SimpleKeyValueCondition.class).stream()
     300                        .reduce((first, last) -> last);
     301                if (lastCondition.isPresent()) {
     302                    getEntryInIndex(lastCondition.get().k).addForKeyAndValue(lastCondition.get().v, ruleIndex);
    309303                } else {
    310                     String key = findAnyRequiredKey(s.conds);
     304                    String key = findAnyRequiredKey(conditions);
    311305                    if (key != null) {
    312306                        getEntryInIndex(key).addForKey(ruleIndex);
     
    468462            // optimization: filter rules for different primitive types
    469463            for (MapCSSRule r: rules) {
    470                 // find the rightmost selector, this must be a GeneralSelector
    471                 Selector selRightmost = r.selector;
    472                 while (selRightmost instanceof ChildOrParentSelector) {
    473                     selRightmost = ((ChildOrParentSelector) selRightmost).right;
    474                 }
    475464                MapCSSRule optRule = new MapCSSRule(r.selector.optimizedBaseCheck(), r.declaration);
    476                 final String base = ((GeneralSelector) selRightmost).getBase();
     465                final String base = r.selector.getBase();
    477466                switch (base) {
    478467                    case Selector.BASE_NODE:
     
    758747     */
    759748    public void removeMetaRules() {
    760         for (Iterator<MapCSSRule> it = rules.iterator(); it.hasNext();) {
    761             MapCSSRule x = it.next();
    762             if (x.selector instanceof GeneralSelector) {
    763                 GeneralSelector gs = (GeneralSelector) x.selector;
    764                 if (Selector.BASE_META.equals(gs.base)) {
    765                     it.remove();
    766                 }
    767             }
    768         }
     749        rules.removeIf(x -> x.selector instanceof GeneralSelector && Selector.BASE_META.equals(x.selector.getBase()));
    769750    }
    770751
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r15985 r15986  
    112112    Range getRange();
    113113
     114    String getBase();
     115
    114116    /**
    115117     * Create an "optimized" copy of this selector that omits the base check.
     
    177179            this.right = b;
    178180            this.type = type;
     181        }
     182
     183        @Override
     184        public String getBase() {
     185            // take the base from the rightmost selector
     186            return right.getBase();
    179187        }
    180188
     
    678686
    679687        @Override
     688        public String getBase() {
     689            throw new UnsupportedOperationException();
     690        }
     691
     692        @Override
    680693        public Subpart getSubpart() {
    681             throw new UnsupportedOperationException("Not supported yet.");
     694            throw new UnsupportedOperationException();
    682695        }
    683696
    684697        @Override
    685698        public Range getRange() {
    686             throw new UnsupportedOperationException("Not supported yet.");
     699            throw new UnsupportedOperationException();
    687700        }
    688701
     
    774787        }
    775788
     789        @Override
    776790        public String getBase() {
    777791            return base;
Note: See TracChangeset for help on using the changeset viewer.