Ignore:
Timestamp:
2014-10-19T01:27:04+02:00 (10 years ago)
Author:
donvip
Message:

[josm_plugins] fix java 7 warnings / global usage of try-with-resource

Location:
applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/AssociatedStreetFixer.java

    r30737 r30738  
    1919public class AssociatedStreetFixer extends RelationFixer {
    2020
    21         public AssociatedStreetFixer() {
    22                 super("associatedStreet");
    23         }
    24 
    25         @Override
    26         public boolean isRelationGood(Relation rel) {
    27                 for (RelationMember m : rel.getMembers()) {
    28                 if (m.getType().equals(OsmPrimitiveType.NODE) && !"house".equals(m.getRole())) {
    29                     setWarningMessage(tr("Node without ''house'' role found"));
    30                         return false;
    31                 }
    32                 if (m.getType().equals(OsmPrimitiveType.WAY) && !("house".equals(m.getRole()) || "street".equals(m.getRole()))) {
    33                     setWarningMessage(tr("Way without ''house'' or ''street'' role found"));
    34                     return false;
    35                 }
    36                 if (m.getType().equals(OsmPrimitiveType.RELATION) && !"house".equals(m.getRole())) {
    37                     setWarningMessage(tr("Relation without ''house'' role found"));
    38                         return false;
    39                 }
    40         }
    41                 // relation should have name
    42                 if (!rel.hasKey("name")) {
    43                     setWarningMessage(tr("Relation does not have name"));
    44                         return false;
    45                 }
    46                 // check that all street members have same name as relation (???)
    47                 String streetName = rel.get("name");
    48                 if (streetName == null) streetName = "";
    49                 for (RelationMember m : rel.getMembers()) {
    50                         if ("street".equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) {
    51                             String anotherName = m.getWay().get("name");
    52                             if (anotherName != null && !anotherName.isEmpty()) {
    53                             setWarningMessage(tr("Relation has streets with different names"));
    54                             return false;
    55                             }
    56                         }
    57                 }
    58                 clearWarningMessage();
    59                 return true;
    60         }
     21    public AssociatedStreetFixer() {
     22        super("associatedStreet");
     23    }
    6124
    6225    @Override
    63         public Command fixRelation(Relation source) {
    64                 // any way with highway tag -> street
    65                 // any way/point/relation with addr:housenumber=* or building=* or type=multipolygon -> house
    66                 // name - check which name is most used in street members and add to relation
    67                 // copy this name to the other street members (???)
    68                 Relation rel = new Relation(source);
    69                 boolean fixed = false;
     26    public boolean isRelationGood(Relation rel) {
     27        for (RelationMember m : rel.getMembers()) {
     28            if (m.getType().equals(OsmPrimitiveType.NODE) && !"house".equals(m.getRole())) {
     29                setWarningMessage(tr("Node without ''house'' role found"));
     30                return false;
     31            }
     32            if (m.getType().equals(OsmPrimitiveType.WAY) && !("house".equals(m.getRole()) || "street".equals(m.getRole()))) {
     33                setWarningMessage(tr("Way without ''house'' or ''street'' role found"));
     34                return false;
     35            }
     36            if (m.getType().equals(OsmPrimitiveType.RELATION) && !"house".equals(m.getRole())) {
     37                setWarningMessage(tr("Relation without ''house'' role found"));
     38                return false;
     39            }
     40        }
     41        // relation should have name
     42        if (!rel.hasKey("name")) {
     43            setWarningMessage(tr("Relation does not have name"));
     44            return false;
     45        }
     46        // check that all street members have same name as relation (???)
     47        String streetName = rel.get("name");
     48        if (streetName == null) streetName = "";
     49        for (RelationMember m : rel.getMembers()) {
     50            if ("street".equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) {
     51                String anotherName = m.getWay().get("name");
     52                if (anotherName != null && !anotherName.isEmpty()) {
     53                    setWarningMessage(tr("Relation has streets with different names"));
     54                    return false;
     55                }
     56            }
     57        }
     58        clearWarningMessage();
     59        return true;
     60    }
    7061
    71                 for (int i = 0; i < rel.getMembersCount(); i++) {
    72                         RelationMember m = rel.getMember(i);
     62    @Override
     63    public Command fixRelation(Relation source) {
     64        // any way with highway tag -> street
     65        // any way/point/relation with addr:housenumber=* or building=* or type=multipolygon -> house
     66        // name - check which name is most used in street members and add to relation
     67        // copy this name to the other street members (???)
     68        Relation rel = new Relation(source);
     69        boolean fixed = false;
    7370
    74                         if (m.isNode()) {
    75                                 Node node = m.getNode();
    76                                 if (!"house".equals(m.getRole()) &&
    77                                                 (node.hasKey("building") || node.hasKey("addr:housenumber"))) {
    78                                         fixed = true;
    79                                         rel.setMember(i, new RelationMember("house", node));
    80                                 }
    81                         } else if (m.isWay()) {
    82                                 Way way = m.getWay();
    83                                 if (!"street".equals(m.getRole()) && way.hasKey("highway")) {
    84                                         fixed = true;
    85                                         rel.setMember(i, new RelationMember("street", way));
    86                                 } else if (!"house".equals(m.getRole()) &&
    87                                                 (way.hasKey("building") || way.hasKey("addr:housenumber"))) {
    88                                         fixed = true;
    89                                         rel.setMember(i,  new RelationMember("house", way));
    90                                 }
    91                         } else if (m.isRelation()) {
    92                                 Relation relation = m.getRelation();
    93                                 if (!"house".equals(m.getRole()) &&
    94                                                 (relation.hasKey("building") || relation.hasKey("addr:housenumber") || "multipolygon".equals(relation.get("type")))) {
    95                                         fixed = true;
    96                                         rel.setMember(i, new RelationMember("house", relation));
    97                                 }
    98                         }
    99                 }
     71        for (int i = 0; i < rel.getMembersCount(); i++) {
     72            RelationMember m = rel.getMember(i);
    10073
    101                 // fill relation name
    102                 Map<String, Integer> streetNames = new HashMap<>();
    103                 for (RelationMember m : rel.getMembers())
    104                         if ("street".equals(m.getRole()) && m.isWay()) {
    105                                 String name = m.getWay().get("name");
    106                                 if (name == null || name.isEmpty()) continue;
     74            if (m.isNode()) {
     75                Node node = m.getNode();
     76                if (!"house".equals(m.getRole()) &&
     77                        (node.hasKey("building") || node.hasKey("addr:housenumber"))) {
     78                    fixed = true;
     79                    rel.setMember(i, new RelationMember("house", node));
     80                }
     81            } else if (m.isWay()) {
     82                Way way = m.getWay();
     83                if (!"street".equals(m.getRole()) && way.hasKey("highway")) {
     84                    fixed = true;
     85                    rel.setMember(i, new RelationMember("street", way));
     86                } else if (!"house".equals(m.getRole()) &&
     87                        (way.hasKey("building") || way.hasKey("addr:housenumber"))) {
     88                    fixed = true;
     89                    rel.setMember(i,  new RelationMember("house", way));
     90                }
     91            } else if (m.isRelation()) {
     92                Relation relation = m.getRelation();
     93                if (!"house".equals(m.getRole()) &&
     94                        (relation.hasKey("building") || relation.hasKey("addr:housenumber") || "multipolygon".equals(relation.get("type")))) {
     95                    fixed = true;
     96                    rel.setMember(i, new RelationMember("house", relation));
     97                }
     98            }
     99        }
    107100
    108                                 Integer count = streetNames.get(name);
     101        // fill relation name
     102        Map<String, Integer> streetNames = new HashMap<>();
     103        for (RelationMember m : rel.getMembers())
     104            if ("street".equals(m.getRole()) && m.isWay()) {
     105                String name = m.getWay().get("name");
     106                if (name == null || name.isEmpty()) continue;
    109107
    110                                 streetNames.put(name, count != null? count + 1 : 1);
    111                         }
    112                 String commonName = "";
    113                 Integer commonCount = 0;
    114                 for (Map.Entry<String, Integer> entry : streetNames.entrySet()) {
    115                         if (entry.getValue() > commonCount) {
    116                                 commonCount = entry.getValue();
    117                                 commonName = entry.getKey();
    118                         }
    119                 }
     108                Integer count = streetNames.get(name);
    120109
    121                 if (!rel.hasKey("name") && !commonName.isEmpty()) {
    122                         fixed = true;
    123                         rel.put("name", commonName);
    124                 } else {
    125                         commonName = ""; // set empty common name - if we already have name on relation, do not overwrite it
    126                 }
     110                streetNames.put(name, count != null? count + 1 : 1);
     111            }
     112        String commonName = "";
     113        Integer commonCount = 0;
     114        for (Map.Entry<String, Integer> entry : streetNames.entrySet()) {
     115            if (entry.getValue() > commonCount) {
     116                commonCount = entry.getValue();
     117                commonName = entry.getKey();
     118            }
     119        }
    127120
    128                 List<Command> commandList = new ArrayList<>();
    129                 if (fixed) {
    130                         commandList.add(new ChangeCommand(source, rel));
    131                 }
     121        if (!rel.hasKey("name") && !commonName.isEmpty()) {
     122            fixed = true;
     123            rel.put("name", commonName);
     124        } else {
     125            commonName = ""; // set empty common name - if we already have name on relation, do not overwrite it
     126        }
    132127
    133                 /*if (!commonName.isEmpty())
    134                 // fill common name to streets
    135                 for (RelationMember m : rel.getMembers())
    136                         if ("street".equals(m.getRole()) && m.isWay()) {
    137                                 String name = m.getWay().get("name");
    138                                 if (commonName.equals(name)) continue;
     128        List<Command> commandList = new ArrayList<>();
     129        if (fixed) {
     130            commandList.add(new ChangeCommand(source, rel));
     131        }
    139132
    140                                 // TODO: ask user if he really wants to overwrite street name??
     133        /*if (!commonName.isEmpty())
     134        // fill common name to streets
     135        for (RelationMember m : rel.getMembers())
     136            if ("street".equals(m.getRole()) && m.isWay()) {
     137                String name = m.getWay().get("name");
     138                if (commonName.equals(name)) continue;
    141139
    142                                 Way oldWay = m.getWay();
    143                                 Way newWay = new Way(oldWay);
    144                                 newWay.put("name", commonName);
     140                // TODO: ask user if he really wants to overwrite street name??
    145141
    146                                 commandList.add(new ChangeCommand(oldWay, newWay));
    147                         }
    148                 */
    149                 // return results
    150                 if (commandList.size() == 0)
    151                         return null;
    152                 if (commandList.size() == 1)
    153                         return commandList.get(0);
    154                 return new SequenceCommand(tr("fix associatedStreet relation"), commandList);
    155         }
     142                Way oldWay = m.getWay();
     143                Way newWay = new Way(oldWay);
     144                newWay.put("name", commonName);
     145
     146                commandList.add(new ChangeCommand(oldWay, newWay));
     147            }
     148        */
     149        // return results
     150        if (commandList.size() == 0)
     151            return null;
     152        if (commandList.size() == 1)
     153            return commandList.get(0);
     154        return new SequenceCommand(tr("fix associatedStreet relation"), commandList);
     155    }
    156156}
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/BoundaryFixer.java

    r28762 r30738  
    1616public class BoundaryFixer extends MultipolygonFixer {
    1717
    18         public BoundaryFixer() {
    19                 super("boundary", "multipolygon");
    20         }
     18    public BoundaryFixer() {
     19        super("boundary", "multipolygon");
     20    }
    2121
    22         /**
    23         * For boundary relations both "boundary" and "multipolygon" types are applicable, but
    24         * it should also have key boundary=administrative to be fully boundary.
    25         * @see http://wiki.openstreetmap.org/wiki/Relation:boundary
    26         */
    27         @Override
    28         public boolean isFixerApplicable(Relation rel) {
    29                 return super.isFixerApplicable(rel) && "administrative".equals(rel.get("boundary"));
    30         }
     22    /**
     23    * For boundary relations both "boundary" and "multipolygon" types are applicable, but
     24    * it should also have key boundary=administrative to be fully boundary.
     25    * @see http://wiki.openstreetmap.org/wiki/Relation:boundary
     26    */
     27    @Override
     28    public boolean isFixerApplicable(Relation rel) {
     29        return super.isFixerApplicable(rel) && "administrative".equals(rel.get("boundary"));
     30    }
    3131
    32         @Override
    33         public boolean isRelationGood(Relation rel) {
    34                 for( RelationMember m : rel.getMembers() ) {
     32    @Override
     33    public boolean isRelationGood(Relation rel) {
     34        for( RelationMember m : rel.getMembers() ) {
    3535            if (m.getType().equals(OsmPrimitiveType.RELATION) && !"subarea".equals(m.getRole())) {
    3636                setWarningMessage(tr("Relation without ''subarea'' role found"));
     
    4343            if (m.getType().equals(OsmPrimitiveType.WAY) && !("outer".equals(m.getRole()) || "inner".equals(m.getRole()))) {
    4444                setWarningMessage(tr("Way without ''inner'' or ''outer'' role found"));
    45                                 return false;
     45                return false;
    4646            }
    4747        }
    48                 clearWarningMessage();
    49                 return true;
    50         }
     48        clearWarningMessage();
     49        return true;
     50    }
    5151
    52         @Override
    53         public Command fixRelation(Relation rel) {
    54                 Relation r = rel;
    55                 Relation rr = fixMultipolygonRoles(r);
    56                 boolean fixed = false;
    57                 if (rr != null) {
    58                         fixed = true;
    59                         r = rr;
    60                 }
    61                 rr = fixBoundaryRoles(r);
    62                 if (rr != null) {
    63                         fixed = true;
    64                         r = rr;
    65                 }
    66                 return fixed ? new ChangeCommand(rel, r) : null;
    67         }
     52    @Override
     53    public Command fixRelation(Relation rel) {
     54        Relation r = rel;
     55        Relation rr = fixMultipolygonRoles(r);
     56        boolean fixed = false;
     57        if (rr != null) {
     58            fixed = true;
     59            r = rr;
     60        }
     61        rr = fixBoundaryRoles(r);
     62        if (rr != null) {
     63            fixed = true;
     64            r = rr;
     65        }
     66        return fixed ? new ChangeCommand(rel, r) : null;
     67    }
    6868
    69         private Relation fixBoundaryRoles( Relation source ) {
     69    private Relation fixBoundaryRoles( Relation source ) {
    7070        Relation r = new Relation(source);
    7171        boolean fixed = false;
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/MultipolygonFixer.java

    r30737 r30738  
    2222public class MultipolygonFixer extends RelationFixer {
    2323
    24         public MultipolygonFixer() {
    25                 super("multipolygon");
    26         }
     24    public MultipolygonFixer() {
     25        super("multipolygon");
     26    }
    2727
    28         protected MultipolygonFixer(String...types) {
    29                 super(types);
    30         }
     28    protected MultipolygonFixer(String...types) {
     29        super(types);
     30    }
    3131
    3232
    33         @Override
    34         public boolean isRelationGood(Relation rel) {
    35                 for (RelationMember m : rel.getMembers())
    36                         if (m.getType().equals(OsmPrimitiveType.WAY) && !("outer".equals(m.getRole()) || "inner".equals(m.getRole()))) {
    37                             setWarningMessage(tr("Way without ''inner'' or ''outer'' role found"));
    38                             return false;
    39                         }
    40                 clearWarningMessage();
    41                 return true;
    42         }
     33    @Override
     34    public boolean isRelationGood(Relation rel) {
     35        for (RelationMember m : rel.getMembers())
     36            if (m.getType().equals(OsmPrimitiveType.WAY) && !("outer".equals(m.getRole()) || "inner".equals(m.getRole()))) {
     37                setWarningMessage(tr("Way without ''inner'' or ''outer'' role found"));
     38                return false;
     39            }
     40        clearWarningMessage();
     41        return true;
     42    }
    4343
    44         @Override
    45         public Command fixRelation(Relation rel) {
    46                 Relation rr = fixMultipolygonRoles(rel);
    47                 return rr != null? new ChangeCommand(rel, rr) : null;
    48         }
     44    @Override
     45    public Command fixRelation(Relation rel) {
     46        Relation rr = fixMultipolygonRoles(rel);
     47        return rr != null? new ChangeCommand(rel, rr) : null;
     48    }
    4949
    50         /**
     50    /**
    5151     * Basically, created multipolygon from scratch, and if successful, replace roles with new ones.
    5252     */
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/NothingFixer.java

    r28693 r30738  
    99public class NothingFixer extends RelationFixer {
    1010
    11         public NothingFixer() {
    12                 super("");
    13         }
    14         @Override
    15         public boolean isFixerApplicable(Relation rel) {
    16                 return true;
    17         }
    18         @Override
    19         public boolean isRelationGood(Relation rel) {
    20                 return true;
    21         }
     11    public NothingFixer() {
     12        super("");
     13    }
     14    @Override
     15    public boolean isFixerApplicable(Relation rel) {
     16        return true;
     17    }
     18    @Override
     19    public boolean isRelationGood(Relation rel) {
     20        return true;
     21    }
    2222
    23         @Override
    24         public Command fixRelation(Relation rel) {
    25                 return null;
    26         }
     23    @Override
     24    public Command fixRelation(Relation rel) {
     25        return null;
     26    }
    2727
    2828}
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/RelationFixer.java

    r30737 r30738  
    1515public abstract class RelationFixer {
    1616
    17         private List<String> applicableTypes;
    18         private SortAndFixAction sortAndFixAction;
     17    private List<String> applicableTypes;
     18    private SortAndFixAction sortAndFixAction;
    1919
    20         /**
    21         * Construct new RelationFixer by a list of applicable types
    22         * @param types
    23         */
    24         public RelationFixer(String... types) {
    25             applicableTypes = new ArrayList<>();
    26                 for(String type: types) {
    27                         applicableTypes.add(type);
    28                 }
    29         }
     20    /**
     21    * Construct new RelationFixer by a list of applicable types
     22    * @param types
     23    */
     24    public RelationFixer(String... types) {
     25        applicableTypes = new ArrayList<>();
     26        for(String type: types) {
     27            applicableTypes.add(type);
     28        }
     29    }
    3030
    31         /**
    32         * Check if given relation is of needed type. You may override this method to check first type
    33         * and then check desired relation properties.
    34         * Note that this only verifies if current RelationFixer can be used to check and fix given relation
    35         * Deeper relation checking is at {@link isRelationGood}
    36         *
    37         * @param rel Relation to check
    38         * @return true if relation can be verified by current RelationFixer
    39         */
    40         public boolean isFixerApplicable(Relation rel) {
    41                 if (rel == null)
    42                         return false;
    43                 if (!rel.hasKey("type"))
    44                         return false;
     31    /**
     32    * Check if given relation is of needed type. You may override this method to check first type
     33    * and then check desired relation properties.
     34    * Note that this only verifies if current RelationFixer can be used to check and fix given relation
     35    * Deeper relation checking is at {@link isRelationGood}
     36    *
     37    * @param rel Relation to check
     38    * @return true if relation can be verified by current RelationFixer
     39    */
     40    public boolean isFixerApplicable(Relation rel) {
     41        if (rel == null)
     42            return false;
     43        if (!rel.hasKey("type"))
     44            return false;
    4545
    46                 String type = rel.get("type");
    47                 for(String oktype: applicableTypes)
    48                         if (oktype.equals(type))
    49                                 return true;
     46        String type = rel.get("type");
     47        for(String oktype: applicableTypes)
     48            if (oktype.equals(type))
     49                return true;
    5050
    51                 return false;
    52         }
     51        return false;
     52    }
    5353
    54         /**
    55         * Check if given relation is OK. That means if all roles are given properly, all tags exist as expected etc.
    56         * Should be written in children classes.
    57         *
    58         * @param rel Relation to verify
    59         * @return true if given relation is OK
    60         */
    61         public abstract boolean isRelationGood(Relation rel);
     54    /**
     55    * Check if given relation is OK. That means if all roles are given properly, all tags exist as expected etc.
     56    * Should be written in children classes.
     57    *
     58    * @param rel Relation to verify
     59    * @return true if given relation is OK
     60    */
     61    public abstract boolean isRelationGood(Relation rel);
    6262
    63         /**
    64         * Fix relation and return new relation with fixed tags, roles etc.
    65         * Note that is not obligatory to return true for isRelationGood for new relation
    66         *
    67         * @param rel Relation to fix
    68         * @return command that fixes the relation {@code null} if it cannot be fixed or is already OK
    69         */
    70         public abstract Command fixRelation(Relation rel);
     63    /**
     64    * Fix relation and return new relation with fixed tags, roles etc.
     65    * Note that is not obligatory to return true for isRelationGood for new relation
     66    *
     67    * @param rel Relation to fix
     68    * @return command that fixes the relation {@code null} if it cannot be fixed or is already OK
     69    */
     70    public abstract Command fixRelation(Relation rel);
    7171
    7272    public void setFixAction(SortAndFixAction sortAndFixAction) {
Note: See TracChangeset for help on using the changeset viewer.