Changeset 15104 in josm for trunk


Ignore:
Timestamp:
2019-05-21T20:24:59+02:00 (5 years ago)
Author:
GerdP
Message:

fix SonarLint issue: extract class IndexData (Reduce this class from 102 to the maximum allowed 100 or externalize it in a public class)

Location:
trunk/src/org/openstreetmap/josm/data/validation/tests
Files:
1 added
1 edited

Legend:

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

    r15103 r15104  
    3838import org.openstreetmap.josm.data.coor.LatLon;
    3939import org.openstreetmap.josm.data.osm.DataSet;
    40 import org.openstreetmap.josm.data.osm.INode;
    4140import org.openstreetmap.josm.data.osm.IPrimitive;
    42 import org.openstreetmap.josm.data.osm.IRelation;
    43 import org.openstreetmap.josm.data.osm.IWay;
    4441import org.openstreetmap.josm.data.osm.OsmPrimitive;
    4542import org.openstreetmap.josm.data.osm.OsmUtils;
     
    8683import org.openstreetmap.josm.tools.GeoPropertyIndex;
    8784import org.openstreetmap.josm.tools.I18n;
    88 import org.openstreetmap.josm.tools.JosmRuntimeException;
    8985import org.openstreetmap.josm.tools.Logging;
    9086import org.openstreetmap.josm.tools.MultiMap;
     
    9793 */
    9894public class MapCSSTagChecker extends Test.TagTest {
    99     IndexData indexData;
     95    MapCSSTagCheckerIndex indexData;
    10096    final Set<OsmPrimitive> tested = new HashSet<>();
    10197
    102     private static final boolean ALL_TESTS = true;
    103     private static final boolean ONLY_SELECTED_TESTS = false;
    104 
    105     /**
    106      * Helper class to store indexes of rules.
    107      * @author Gerd
    108      *
    109      */
    110     private static final class IndexData {
    111         final Map<MapCSSRule, TagCheck> ruleToCheckMap = new HashMap<>();
    112 
    113         /**
    114          * Rules for nodes
    115          */
    116         final MapCSSRuleIndex nodeRules = new MapCSSRuleIndex();
    117         /**
    118          * Rules for ways without tag area=no
    119          */
    120         final MapCSSRuleIndex wayRules = new MapCSSRuleIndex();
    121         /**
    122          * Rules for ways with tag area=no
    123          */
    124         final MapCSSRuleIndex wayNoAreaRules = new MapCSSRuleIndex();
    125         /**
    126          * Rules for relations that are not multipolygon relations
    127          */
    128         final MapCSSRuleIndex relationRules = new MapCSSRuleIndex();
    129         /**
    130          * Rules for multipolygon relations
    131          */
    132         final MapCSSRuleIndex multipolygonRules = new MapCSSRuleIndex();
    133 
    134         private IndexData(MultiMap<String, TagCheck> checks, boolean includeOtherSeverity, boolean allTests) {
    135             buildIndex(checks, includeOtherSeverity, allTests);
    136         }
    137 
    138         private void buildIndex(MultiMap<String, TagCheck> checks, boolean includeOtherSeverity, boolean allTests) {
    139             List<TagCheck> allChecks = new ArrayList<>();
    140             for (Set<TagCheck> cs : checks.values()) {
    141                 allChecks.addAll(cs);
    142             }
    143 
    144             ruleToCheckMap.clear();
    145             nodeRules.clear();
    146             wayRules.clear();
    147             wayNoAreaRules.clear();
    148             relationRules.clear();
    149             multipolygonRules.clear();
    150 
    151             // optimization: filter rules for different primitive types
    152             for (TagCheck c : allChecks) {
    153                 if (!includeOtherSeverity && Severity.OTHER == c.getSeverity()
    154                         && c.setClassExpressions.isEmpty()) {
    155                     // Ignore "information" level checks if not wanted, unless they also set a MapCSS class
    156                     continue;
    157                 }
    158 
    159                 for (Selector s : c.rule.selectors) {
    160                     // find the rightmost selector, this must be a GeneralSelector
    161                     boolean hasLeftRightSel = false;
    162                     Selector selRightmost = s;
    163                     while (selRightmost instanceof Selector.ChildOrParentSelector) {
    164                         hasLeftRightSel = true;
    165                         selRightmost = ((Selector.ChildOrParentSelector) selRightmost).right;
    166                     }
    167                     if (!allTests && !hasLeftRightSel) {
    168                         continue;
    169                     }
    170 
    171                     MapCSSRule optRule = new MapCSSRule(s.optimizedBaseCheck(), c.rule.declaration);
    172 
    173                     ruleToCheckMap.put(optRule, c);
    174                     final String base = ((GeneralSelector) selRightmost).getBase();
    175                     switch (base) {
    176                     case Selector.BASE_NODE:
    177                         nodeRules.add(optRule);
    178                         break;
    179                     case Selector.BASE_WAY:
    180                         wayNoAreaRules.add(optRule);
    181                         wayRules.add(optRule);
    182                         break;
    183                     case Selector.BASE_AREA:
    184                         wayRules.add(optRule);
    185                         multipolygonRules.add(optRule);
    186                         break;
    187                     case Selector.BASE_RELATION:
    188                         relationRules.add(optRule);
    189                         multipolygonRules.add(optRule);
    190                         break;
    191                     case Selector.BASE_ANY:
    192                         nodeRules.add(optRule);
    193                         wayRules.add(optRule);
    194                         wayNoAreaRules.add(optRule);
    195                         relationRules.add(optRule);
    196                         multipolygonRules.add(optRule);
    197                         break;
    198                     case Selector.BASE_CANVAS:
    199                     case Selector.BASE_META:
    200                     case Selector.BASE_SETTING:
    201                         break;
    202                     default:
    203                         final RuntimeException e = new JosmRuntimeException(MessageFormat.format("Unknown MapCSS base selector {0}", base));
    204                         Logging.warn(tr("Failed to index validator rules. Error was: {0}", e.getMessage()));
    205                         Logging.error(e);
    206                     }
    207                 }
    208             }
    209             nodeRules.initIndex();
    210             wayRules.initIndex();
    211             wayNoAreaRules.initIndex();
    212             relationRules.initIndex();
    213             multipolygonRules.initIndex();
    214         }
    215 
    216         /**
    217          * Get the index of rules for the given primitive.
    218          * @param p the primitve
    219          * @return index of rules for the given primitive
    220          */
    221         public MapCSSRuleIndex get(OsmPrimitive p) {
    222             if (p instanceof INode) {
    223                 return nodeRules;
    224             } else if (p instanceof IWay) {
    225                 if (OsmUtils.isFalse(p.get("area"))) {
    226                     return wayNoAreaRules;
    227                 } else {
    228                     return wayRules;
    229                 }
    230             } else if (p instanceof IRelation) {
    231                 if (((IRelation<?>) p).isMultipolygon()) {
    232                     return multipolygonRules;
    233                 } else {
    234                     return relationRules;
    235                 }
    236             } else {
    237                 throw new IllegalArgumentException("Unsupported type: " + p);
    238             }
    239         }
    240 
    241         /**
    242          * return the TagCheck for which the given indexed rule was created.
    243          * @param rule an indexed rule
    244          * @return the original TagCheck
    245          */
    246         public TagCheck getCheck(MapCSSRule rule) {
    247             return ruleToCheckMap.get(rule);
    248         }
    249     }
    25098
    25199    /**
     
    860708    public synchronized Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity) {
    861709        final List<TestError> res = new ArrayList<>();
    862         if (indexData == null)
    863             indexData = new IndexData(checks, includeOtherSeverity, ALL_TESTS);
     710        if (indexData == null) {
     711            indexData = new MapCSSTagCheckerIndex(checks, includeOtherSeverity, MapCSSTagCheckerIndex.ALL_TESTS);
     712        }
    864713
    865714        MapCSSRuleIndex matchingRuleIndex = indexData.get(p);
     
    1140989        super.setShowElements(true);
    1141990        if (indexData == null) {
    1142             indexData = new IndexData(checks, includeOtherSeverityChecks(), ALL_TESTS);
     991            indexData = new MapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), MapCSSTagCheckerIndex.ALL_TESTS);
    1143992        }
    1144993        tested.clear();
     
    11531002            // rebuild index with a reduced set of rules (those that use ChildOrParentSelector) and thus may have left selectors
    11541003            // matching the previously tested elements
    1155             indexData = new IndexData(checks, includeOtherSeverityChecks(), ONLY_SELECTED_TESTS);
     1004            indexData = new MapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), MapCSSTagCheckerIndex.ONLY_SELECTED_TESTS);
    11561005
    11571006            Set<OsmPrimitive> surrounding = new HashSet<>();
     
    11641013            final boolean includeOtherSeverity = includeOtherSeverityChecks();
    11651014            for (OsmPrimitive p : surrounding) {
    1166                 if (tested.contains(p) )
     1015                if (tested.contains(p))
    11671016                    continue;
    11681017                Collection<TestError> additionalErrors = getErrorsForPrimitive(p, includeOtherSeverity);
Note: See TracChangeset for help on using the changeset viewer.