Changeset 7748 in josm


Ignore:
Timestamp:
2014-11-25T23:51:45+01:00 (9 years ago)
Author:
Don-vip
Message:

fix some Sonar issues recently introduced

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/upload/UploadNotesTask.java

    r7699 r7748  
    4545        Map<Note, Exception> failedNotes = new HashMap<>();
    4646
     47        /**
     48         * Constructs a new {@code UploadTask}.
     49         * @param title message for the user
     50         * @param monitor progress monitor
     51         */
    4752        public UploadTask(String title, ProgressMonitor monitor) {
    4853            super(title, monitor, false);
     
    7176                            Main.debug("found note change to upload");
    7277                        }
    73                         try {
    74                             Note newNote;
    75                             switch (comment.getNoteAction()) {
    76                             case opened:
    77                                 if (Main.isDebugEnabled()) {
    78                                     Main.debug("opening new note");
    79                                 }
    80                                 newNote = api.createNote(note.getLatLon(), comment.getText(), monitor);
    81                                 note.setId(newNote.getId());
    82                                 break;
    83                             case closed:
    84                                 if (Main.isDebugEnabled()) {
    85                                     Main.debug("closing note " + note.getId());
    86                                 }
    87                                 newNote = api.closeNote(note, comment.getText(), monitor);
    88                                 break;
    89                             case commented:
    90                                 if (Main.isDebugEnabled()) {
    91                                     Main.debug("adding comment to note " + note.getId());
    92                                 }
    93                                 newNote = api.addCommentToNote(note, comment.getText(), monitor);
    94                                 break;
    95                             case reopened:
    96                                 if (Main.isDebugEnabled()) {
    97                                     Main.debug("reopening note " + note.getId());
    98                                 }
    99                                 newNote = api.reopenNote(note, comment.getText(), monitor);
    100                                 break;
    101                             default:
    102                                 newNote = null;
    103                             }
    104                             updatedNotes.put(note, newNote);
    105                         } catch (Exception e) {
    106                             Main.error("Failed to upload note to server: " + note.getId());
    107                             failedNotes.put(note, e);
    108                         }
     78                        processNoteComment(monitor, api, note, comment);
    10979                    }
    11080                }
     81            }
     82        }
     83
     84        private void processNoteComment(ProgressMonitor monitor, OsmApi api, Note note, NoteComment comment) {
     85            try {
     86                Note newNote;
     87                switch (comment.getNoteAction()) {
     88                case opened:
     89                    if (Main.isDebugEnabled()) {
     90                        Main.debug("opening new note");
     91                    }
     92                    newNote = api.createNote(note.getLatLon(), comment.getText(), monitor);
     93                    note.setId(newNote.getId());
     94                    break;
     95                case closed:
     96                    if (Main.isDebugEnabled()) {
     97                        Main.debug("closing note " + note.getId());
     98                    }
     99                    newNote = api.closeNote(note, comment.getText(), monitor);
     100                    break;
     101                case commented:
     102                    if (Main.isDebugEnabled()) {
     103                        Main.debug("adding comment to note " + note.getId());
     104                    }
     105                    newNote = api.addCommentToNote(note, comment.getText(), monitor);
     106                    break;
     107                case reopened:
     108                    if (Main.isDebugEnabled()) {
     109                        Main.debug("reopening note " + note.getId());
     110                    }
     111                    newNote = api.reopenNote(note, comment.getText(), monitor);
     112                    break;
     113                default:
     114                    newNote = null;
     115                }
     116                updatedNotes.put(note, newNote);
     117            } catch (Exception e) {
     118                Main.error("Failed to upload note to server: " + note.getId());
     119                failedNotes.put(note, e);
    111120            }
    112121        }
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java

    r7725 r7748  
    6060     */
    6161    public static class UserInputTag {
    62         public String key;
    63         public String value;
    64         public boolean defaultKey;
     62        private final String key;
     63        private final String value;
     64        private final boolean defaultKey;
    6565
    6666        /**
    6767         * Constructor.
    68          * 
     68         *
    6969         * @param key the tag key
    7070         * @param value the tag value
     
    9090        @Override
    9191        public boolean equals(Object obj) {
    92             if (obj == null) {
     92            if (obj == null || getClass() != obj.getClass()) {
    9393                return false;
    9494            }
    95             if (getClass() != obj.getClass()) {
    96                 return false;
    97             }
    9895            final UserInputTag other = (UserInputTag) obj;
    99             if (!Objects.equals(this.key, other.key)) {
    100                 return false;
    101             }
    102             if (!Objects.equals(this.value, other.value)) {
    103                 return false;
    104             }
    105             if (this.defaultKey != other.defaultKey) {
    106                 return false;
    107             }
    108             return true;
     96            return Objects.equals(this.key, other.key)
     97                && Objects.equals(this.value, other.value)
     98                && this.defaultKey == other.defaultKey;
    10999        }
    110100    }
     
    121111     */
    122112    protected MultiMap<String, String> tagCache;
    123     /**
    124      * the same as tagCache but for the preset keys and values
    125      * can be accessed directly
    126      */
    127     protected static final MultiMap<String, String> presetTagCache = new MultiMap<>();
     113
     114    /**
     115     * the same as tagCache but for the preset keys and values can be accessed directly
     116     */
     117    protected static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>();
     118
    128119    /**
    129120     * Cache for tags that have been entered by the user.
    130121     */
    131     protected static final Set<UserInputTag> userInputTagCache = new LinkedHashSet<>();
    132    
     122    protected static final Set<UserInputTag> USER_INPUT_TAG_CACHE = new LinkedHashSet<>();
     123
    133124    /**
    134125     * the cached list of member roles
     
    137128     */
    138129    protected Set<String> roleCache;
    139     /**
    140      * the same as roleCache but for the preset roles
    141      * can be accessed directly
    142      */
    143     protected static final Set<String> presetRoleCache = new HashSet<>();
    144 
     130
     131    /**
     132     * the same as roleCache but for the preset roles can be accessed directly
     133     */
     134    protected static final Set<String> PRESET_ROLE_CACHE = new HashSet<>();
     135
     136    /**
     137     * Constructs a new {@code AutoCompletionManager}.
     138     * @param ds data set
     139     */
    145140    public AutoCompletionManager(DataSet ds) {
    146141        this.ds = ds;
    147         dirty = true;
     142        this.dirty = true;
    148143    }
    149144
     
    166161    /**
    167162     * initializes the cache from the primitives in the dataset
    168      *
    169163     */
    170164    protected void rebuild() {
     
    211205    /**
    212206     * Initialize the cache for presets. This is done only once.
     207     * @param presets Tagging presets to cache
    213208     */
    214209    public static void cachePresets(Collection<TaggingPreset> presets) {
     
    219214                    if (ki.key != null && ki.getValues() != null) {
    220215                        try {
    221                             presetTagCache.putAll(ki.key, ki.getValues());
     216                            PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());
    222217                        } catch (NullPointerException e) {
    223218                            Main.error(p+": Unable to cache "+ki);
     
    228223                    for (TaggingPresetItems.Role i : r.roles) {
    229224                        if (i.key != null) {
    230                             presetRoleCache.add(i.key);
     225                            PRESET_ROLE_CACHE.add(i.key);
    231226                        }
    232227                    }
     
    235230        }
    236231    }
    237    
    238    
     232
     233    /**
     234     * Remembers user input for the given key/value.
     235     * @param key Tag key
     236     * @param value Tag value
     237     * @param defaultKey true, if the key was not really entered by the user, e.g. for preset text fields
     238     */
    239239    public static void rememberUserInput(String key, String value, boolean defaultKey) {
    240240        UserInputTag tag = new UserInputTag(key, value, defaultKey);
    241         userInputTagCache.remove(tag); // re-add, so it gets to the last position of the LinkedHashSet
    242         userInputTagCache.add(tag);
     241        USER_INPUT_TAG_CACHE.remove(tag); // re-add, so it gets to the last position of the LinkedHashSet
     242        USER_INPUT_TAG_CACHE.add(tag);
    243243    }
    244244
     
    253253
    254254    protected List<String> getPresetKeys() {
    255         return new ArrayList<>(presetTagCache.keySet());
    256     }
    257    
     255        return new ArrayList<>(PRESET_TAG_CACHE.keySet());
     256    }
     257
    258258    protected Collection<String> getUserInputKeys() {
    259259        List<String> keys = new ArrayList<>();
    260         for (UserInputTag tag : userInputTagCache) {
     260        for (UserInputTag tag : USER_INPUT_TAG_CACHE) {
    261261            if (!tag.defaultKey) {
    262262                keys.add(tag.key);
     
    279279
    280280    protected static List<String> getPresetValues(String key) {
    281         return new ArrayList<>(presetTagCache.getValues(key));
     281        return new ArrayList<>(PRESET_TAG_CACHE.getValues(key));
    282282    }
    283283
    284284    protected static Collection<String> getUserInputValues(String key) {
    285         ArrayList<String> values = new ArrayList<>();
    286         for (UserInputTag tag : userInputTagCache) {
     285        List<String> values = new ArrayList<>();
     286        for (UserInputTag tag : USER_INPUT_TAG_CACHE) {
    287287            if (key.equals(tag.key)) {
    288288                values.add(tag.value);
     
    309309     */
    310310    public void populateWithMemberRoles(AutoCompletionList list) {
    311         list.add(presetRoleCache, AutoCompletionItemPriority.IS_IN_STANDARD);
     311        list.add(PRESET_ROLE_CACHE, AutoCompletionItemPriority.IS_IN_STANDARD);
    312312        list.add(getRoleCache(), AutoCompletionItemPriority.IS_IN_DATASET);
    313313    }
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r7663 r7748  
    332332        // this works around a ruby (or lighttpd) bug where two consecutive slashes in
    333333        // an URL will cause a "404 not found" response.
    334         int p; while ((p = rv.indexOf("//", rv.indexOf("://")+2)) > -1) { rv.delete(p, p + 1); }
     334        int p;
     335        while ((p = rv.indexOf("//", rv.indexOf("://")+2)) > -1) {
     336            rv.delete(p, p + 1);
     337        }
    335338        return rv.toString();
    336339    }
     
    383386            osm.setVisible(true);
    384387        } catch(NumberFormatException e) {
    385             throw new OsmTransferException(tr("Unexpected format of new version of modified primitive ''{0}''. Got ''{1}''.", osm.getId(), ret));
     388            throw new OsmTransferException(tr("Unexpected format of new version of modified primitive ''{0}''. Got ''{1}''.",
     389                    osm.getId(), ret));
    386390        }
    387391    }
     
    467471            throw e;
    468472        } catch(OsmApiException e) {
    469             if (e.getResponseCode() == HttpURLConnection.HTTP_CONFLICT && ChangesetClosedException.errorHeaderMatchesPattern(e.getErrorHeader()))
    470                 throw new ChangesetClosedException(e.getErrorHeader(), ChangesetClosedException.Source.UPDATE_CHANGESET);
     473            String errorHeader = e.getErrorHeader();
     474            if (e.getResponseCode() == HttpURLConnection.HTTP_CONFLICT && ChangesetClosedException.errorHeaderMatchesPattern(errorHeader))
     475                throw new ChangesetClosedException(errorHeader, ChangesetClosedException.Source.UPDATE_CHANGESET);
    471476            throw e;
    472477        } finally {
     
    512517     * @throws OsmTransferException if something is wrong
    513518     */
    514     public Collection<OsmPrimitive> uploadDiff(Collection<? extends OsmPrimitive> list, ProgressMonitor monitor) throws OsmTransferException {
     519    public Collection<OsmPrimitive> uploadDiff(Collection<? extends OsmPrimitive> list, ProgressMonitor monitor)
     520            throws OsmTransferException {
    515521        try {
    516522            monitor.beginTask("", list.size() * 2);
     
    588594    }
    589595
    590     protected final String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor) throws OsmTransferException {
     596    protected final String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor)
     597            throws OsmTransferException {
    591598        return sendRequest(requestMethod, urlSuffix, requestBody, monitor, true, false);
    592599    }
     
    611618     *    been exhausted), or rewrapping a Java exception.
    612619     */
    613     protected final String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor, boolean doAuthenticate, boolean fastFail) throws OsmTransferException {
     620    protected final String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor,
     621            boolean doAuthenticate, boolean fastFail) throws OsmTransferException {
    614622        StringBuilder responseBody = new StringBuilder();
    615623        int retries = fastFail ? 0 : getMaxRetries();
     
    788796    }
    789797
    790     /**
    791      * Create a new note on the server
     798    private static StringBuilder noteStringBuilder(Note note) {
     799        return new StringBuilder().append("notes/").append(note.getId());
     800    }
     801
     802    /**
     803     * Create a new note on the server.
    792804     * @param latlon Location of note
    793805     * @param text Comment entered by user to open the note
     
    798810    public Note createNote(LatLon latlon, String text, ProgressMonitor monitor) throws OsmTransferException {
    799811        initialize(monitor);
    800         String url = new StringBuilder()
     812        String noteUrl = new StringBuilder()
    801813            .append("notes?lat=")
    802814            .append(latlon.lat())
     
    806818            .append(urlEncode(text)).toString();
    807819
    808         String response = sendRequest("POST", url, null, monitor, true, false);
     820        String response = sendRequest("POST", noteUrl, null, monitor, true, false);
    809821        return parseSingleNote(response);
    810822    }
     
    820832    public Note addCommentToNote(Note note, String comment, ProgressMonitor monitor) throws OsmTransferException {
    821833        initialize(monitor);
    822         String url = new StringBuilder()
    823             .append("notes/")
    824             .append(note.getId())
     834        String noteUrl = noteStringBuilder(note)
    825835            .append("/comment?text=")
    826836            .append(urlEncode(comment)).toString();
    827837
    828         String response = sendRequest("POST", url, null, monitor, true, false);
     838        String response = sendRequest("POST", noteUrl, null, monitor, true, false);
    829839        return parseSingleNote(response);
    830840    }
    831841
    832842    /**
    833      * Close a note
     843     * Close a note.
    834844     * @param note Note to close. Must currently be open
    835845     * @param closeMessage Optional message supplied by the user when closing the note
     
    841851        initialize(monitor);
    842852        String encodedMessage = urlEncode(closeMessage);
    843         StringBuilder urlBuilder = new StringBuilder()
    844             .append("notes/")
    845             .append(note.getId())
     853        StringBuilder urlBuilder = noteStringBuilder(note)
    846854            .append("/close");
    847855        if (encodedMessage != null && !encodedMessage.trim().isEmpty()) {
     
    865873        initialize(monitor);
    866874        String encodedMessage = urlEncode(reactivateMessage);
    867         StringBuilder urlBuilder = new StringBuilder()
    868             .append("notes/")
    869             .append(note.getId())
     875        StringBuilder urlBuilder = noteStringBuilder(note)
    870876            .append("/reopen");
    871877        if (encodedMessage != null && !encodedMessage.trim().isEmpty()) {
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r7731 r7748  
    9191public class ImageProvider {
    9292
     93    private static final String HTTP_PROTOCOL  = "http://";
     94    private static final String HTTPS_PROTOCOL = "https://";
     95    private static final String WIKI_PROTOCOL  = "wiki://";
     96
    9397    /**
    9498     * Position of an overlay icon
     
    266270     * @since 7687
    267271     */
    268     static public Dimension getImageSizes(ImageSizes size) {
     272    public static Dimension getImageSizes(ImageSizes size) {
    269273        int sizeval;
    270274        switch(size) {
     
    458462     */
    459463    public void getInBackground(final ImageCallback callback) {
    460         if (name.startsWith("http://") || name.startsWith("wiki://")) {
     464        if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(WIKI_PROTOCOL)) {
    461465            Runnable fetch = new Runnable() {
    462466                @Override
     
    486490     */
    487491    public void getInBackground(final ImageResourceCallback callback) {
    488         if (name.startsWith("http://") || name.startsWith("wiki://")) {
     492        if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(WIKI_PROTOCOL)) {
    489493            Runnable fetch = new Runnable() {
    490494                @Override
     
    572576            ImageType type = name.toLowerCase().endsWith(".svg") ? ImageType.SVG : ImageType.OTHER;
    573577
    574             if (name.startsWith("http://") || name.startsWith("https://")) {
     578            if (name.startsWith(HTTP_PROTOCOL) || name.startsWith(HTTPS_PROTOCOL)) {
    575579                String url = name;
    576580                ImageResource ir = cache.get(url);
     
    581585                }
    582586                return ir;
    583             } else if (name.startsWith("wiki://")) {
     587            } else if (name.startsWith(WIKI_PROTOCOL)) {
    584588                ImageResource ir = cache.get(name);
    585589                if (ir != null) return ir;
     
    984988     * overlay must be transparent in the background.
    985989     * Also scaling is not cared about with current implementation.
     990     * @deprecated this method will be refactored
    986991     */
    987992    @Deprecated
Note: See TracChangeset for help on using the changeset viewer.