Ignore:
Timestamp:
2015-05-06T00:23:51+02:00 (5 years ago)
Author:
Don-vip
Message:

fix various issues:

  • performance - inefficient use of keySet iterator instead of entrySet iterator
  • squid:S2674: The value returned from a stream read should be checked
  • remove unused imports
  • improve javadoc
Location:
trunk/src/org/openstreetmap/josm/io
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/AbstractReader.java

    r8291 r8332  
    99import java.util.List;
    1010import java.util.Map;
     11import java.util.Map.Entry;
    1112
    1213import org.openstreetmap.josm.Main;
     
    8283     */
    8384    protected void processWaysAfterParsing() throws IllegalDataException{
    84         for (Long externalWayId: ways.keySet()) {
     85        for (Entry<Long, Collection<Long>> entry : ways.entrySet()) {
     86            Long externalWayId = entry.getKey();
    8587            Way w = (Way)externalIdMap.get(new SimplePrimitiveId(externalWayId, OsmPrimitiveType.WAY));
    8688            List<Node> wayNodes = new ArrayList<>();
    87             for (long id : ways.get(externalWayId)) {
     89            for (long id : entry.getValue()) {
    8890                Node n = (Node)externalIdMap.get(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
    8991                if (n == null) {
     
    132134        }
    133135
    134         for (Long externalRelationId : relations.keySet()) {
     136        for (Entry<Long, Collection<RelationMemberData>> entry : relations.entrySet()) {
     137            Long externalRelationId = entry.getKey();
    135138            Relation relation = (Relation) externalIdMap.get(
    136139                    new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
    137140            );
    138141            List<RelationMember> relationMembers = new ArrayList<>();
    139             for (RelationMemberData rm : relations.get(externalRelationId)) {
     142            for (RelationMemberData rm : entry.getValue()) {
    140143                OsmPrimitive primitive = null;
    141144
  • trunk/src/org/openstreetmap/josm/io/CacheCustomContent.java

    r7434 r8332  
    1919 * use {@link RuntimeException} if no exception must be handled.
    2020 * @author xeen
    21  *
     21 * @since 1450
    2222 */
    2323public abstract class CacheCustomContent<T extends Throwable> {
    24     /**
    25      * Common intervals
    26      */
     24
     25    /** Update interval meaning an update is always needed */
    2726    public static final int INTERVAL_ALWAYS = -1;
     27    /** Update interval meaning an update is needed each hour */
    2828    public static final int INTERVAL_HOURLY = 60*60;
     29    /** Update interval meaning an update is needed each day */
    2930    public static final int INTERVAL_DAILY = INTERVAL_HOURLY * 24;
     31    /** Update interval meaning an update is needed each week */
    3032    public static final int INTERVAL_WEEKLY = INTERVAL_DAILY * 7;
     33    /** Update interval meaning an update is needed each month */
    3134    public static final int INTERVAL_MONTHLY = INTERVAL_WEEKLY * 4;
     35    /** Update interval meaning an update is never needed */
    3236    public static final int INTERVAL_NEVER = Integer.MAX_VALUE;
    3337
     
    6165
    6266    /**
    63      * This function serves as a comfort hook to perform additional checks if the cache is valid
    64      * @return True if the cached copy is still valid
    65      */
    66     protected boolean isCacheValid() {
    67         return true;
    68     }
    69 
    70     /**
    7167     * Initializes the class. Note that all read data will be stored in memory until it is flushed
    7268     * by flushData().
    73      * @param ident
    74      * @param updateInterval
     69     * @param ident ident that identifies the stored file. Includes file-ending.
     70     * @param updateInterval update interval in seconds. -1 means always
    7571     */
    7672    public CacheCustomContent(String ident, int updateInterval) {
     
    7874        this.updateInterval = updateInterval;
    7975        this.path = new File(Main.pref.getCacheDirectory(), ident);
     76    }
     77
     78    /**
     79     * This function serves as a comfort hook to perform additional checks if the cache is valid
     80     * @return True if the cached copy is still valid
     81     */
     82    protected boolean isCacheValid() {
     83        return true;
    8084    }
    8185
     
    104108     * Updates data if required
    105109     * @return Returns the data
     110     * @throws T if an error occurs
    106111     */
    107112    public byte[] updateIfRequired() throws T {
     
    114119     * Updates data if required
    115120     * @return Returns the data as string
     121     * @throws T if an error occurs
    116122     */
    117123    public String updateIfRequiredString() throws T {
     
    124130     * Executes an update regardless of updateInterval
    125131     * @return Returns the data
     132     * @throws T if an error occurs
    126133     */
    127134    public byte[] updateForce() throws T {
     
    135142     * Executes an update regardless of updateInterval
    136143     * @return Returns the data as String
     144     * @throws T if an error occurs
    137145     */
    138146    public String updateForceString() throws T {
     
    144152     * Returns the data without performing any updates
    145153     * @return the data
     154     * @throws T if an error occurs
    146155     */
    147156    public byte[] getData() throws T {
     
    155164     * Returns the data without performing any updates
    156165     * @return the data as String
     166     * @throws T if an error occurs
    157167     */
    158168    public String getDataString() throws T {
     
    170180        try (BufferedInputStream input = new BufferedInputStream(new FileInputStream(path))) {
    171181            this.data = new byte[input.available()];
    172             input.read(this.data);
     182            if (input.read(this.data) < this.data.length) {
     183                Main.error("Failed to read expected contents from "+path);
     184            }
    173185        } catch (IOException e) {
    174186            if (!isOffline()) {
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/AddTagsDialog.java

    r8308 r8332  
    1515import java.util.HashSet;
    1616import java.util.Map;
     17import java.util.Map.Entry;
    1718import java.util.Set;
    1819
     
    9495
    9596        private String getToolTip() {
    96             StringBuilder sb=new StringBuilder();
     97            StringBuilder sb = new StringBuilder();
    9798            sb.append("<html>");
    9899            sb.append(tr("Old values of"));
     
    100101            sb.append(tag);
    101102            sb.append("</b><br/>");
    102             for (String k: valueCount.keySet()) {
     103            for (Entry<String, Integer> e : valueCount.entrySet()) {
    103104                sb.append("<b>");
    104                 sb.append(valueCount.get(k));
     105                sb.append(e.getValue());
    105106                sb.append(" x </b>");
    106                 sb.append(k);
     107                sb.append(e.getKey());
    107108                sb.append("<br/>");
    108109            }
    109110            sb.append("</html>");
    110111            return sb.toString();
    111 
    112112        }
    113113    }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java

    r8285 r8332  
    1212import java.util.List;
    1313import java.util.Map;
     14import java.util.Map.Entry;
    1415
    1516import org.openstreetmap.josm.Main;
     
    119120     * Find the node with almost the same ccords in dataset or in already added nodes
    120121     * @since 5845
    121      **/
     122     */
    122123    Node findOrCreateNode(LatLon ll,  List<Command> commands) {
    123124        Node nd = null;
     
    132133
    133134        Node prev = null;
    134         for (LatLon lOld: addedNodes.keySet()) {
     135        for (Entry<LatLon, Node> entry : addedNodes.entrySet()) {
     136            LatLon lOld = entry.getKey();
    135137            if (lOld.greatCircleDistance(ll) < Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
    136                 prev = addedNodes.get(lOld);
     138                prev = entry.getValue();
    137139                break;
    138140            }
  • trunk/src/org/openstreetmap/josm/io/session/SessionReader.java

    r7326 r8332  
    484484
    485485        layers = new ArrayList<>();
    486         for (int idx : layersMap.keySet()) {
    487             Layer layer = layersMap.get(idx);
     486        for (Entry<Integer, Layer> entry : layersMap.entrySet()) {
     487            Layer layer = entry.getValue();
    488488            if (layer == null) {
    489489                continue;
    490490            }
    491             Element el = elems.get(idx);
     491            Element el = elems.get(entry.getKey());
    492492            if (el.hasAttribute("visible")) {
    493493                layer.setVisible(Boolean.parseBoolean(el.getAttribute("visible")));
Note: See TracChangeset for help on using the changeset viewer.