Changeset 10299 in josm


Ignore:
Timestamp:
2016-05-29T16:33:08+02:00 (4 years ago)
Author:
Don-vip
Message:

sonar - Performance - Method passes constant String of length 1 to character overridden method + add unit tests/javadoc

Location:
trunk
Files:
1 added
5 edited

Legend:

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

    r10208 r10299  
    319319            defaultExtension,
    320320            description + (!extensionsForDescription.isEmpty()
    321                 ? (" (" + Utils.join(", ", extensionsForDescription) + ")")
     321                ? (" (" + Utils.join(", ", extensionsForDescription) + ')')
    322322                : "")
    323323            );
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r10220 r10299  
    244244
    245245    public interface BinaryMatchFactory extends MatchFactory {
    246         BinaryMatch get(String keyword, Match lhs, Match rhs, PushbackTokenizer tokenizer) throws ParseError;
     246        AbstractBinaryMatch get(String keyword, Match lhs, Match rhs, PushbackTokenizer tokenizer) throws ParseError;
    247247    }
    248248
     
    337337     * A binary search operator which may take data parameters.
    338338     */
    339     public abstract static class BinaryMatch extends Match {
     339    public abstract static class AbstractBinaryMatch extends Match {
    340340
    341341        protected final Match lhs;
    342342        protected final Match rhs;
    343343
    344         public BinaryMatch(Match lhs, Match rhs) {
     344        /**
     345         * Constructs a new {@code BinaryMatch}.
     346         * @param lhs Left hand side
     347         * @param rhs Right hand side
     348         */
     349        public AbstractBinaryMatch(Match lhs, Match rhs) {
    345350            this.lhs = lhs;
    346351            this.rhs = rhs;
    347352        }
    348353
    349         public Match getLhs() {
     354        /**
     355         * Returns left hand side.
     356         * @return left hand side
     357         */
     358        public final Match getLhs() {
    350359            return lhs;
    351360        }
    352361
    353         public Match getRhs() {
     362        /**
     363         * Returns right hand side.
     364         * @return right hand side
     365         */
     366        public final Match getRhs() {
    354367            return rhs;
    355368        }
     
    438451     * Matches if both left and right expressions match.
    439452     */
    440     public static class And extends BinaryMatch {
     453    public static class And extends AbstractBinaryMatch {
     454        /**
     455         * Constructs a new {@code And} match.
     456         * @param lhs left hand side
     457         * @param rhs right hand side
     458         */
    441459        public And(Match lhs, Match rhs) {
    442460            super(lhs, rhs);
     
    455473        @Override
    456474        public String toString() {
    457             return (lhs instanceof BinaryMatch && !(lhs instanceof And) ? "(" + lhs + ")" : lhs) + " && "
    458                     + (rhs instanceof BinaryMatch && !(rhs instanceof And) ? "(" + rhs + ")" : rhs);
     475            return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof And) ? ("(" + lhs + ')') : lhs) + " && "
     476                    + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof And) ? ("(" + rhs + ')') : rhs);
    459477        }
    460478    }
     
    463481     * Matches if the left OR the right expression match.
    464482     */
    465     public static class Or extends BinaryMatch {
     483    public static class Or extends AbstractBinaryMatch {
     484        /**
     485         * Constructs a new {@code Or} match.
     486         * @param lhs left hand side
     487         * @param rhs right hand side
     488         */
    466489        public Or(Match lhs, Match rhs) {
    467490            super(lhs, rhs);
     
    480503        @Override
    481504        public String toString() {
    482             return (lhs instanceof BinaryMatch && !(lhs instanceof Or) ? "(" + lhs + ")" : lhs) + " || "
    483                     + (rhs instanceof BinaryMatch && !(rhs instanceof Or) ? "(" + rhs + ")" : rhs);
     505            return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof Or) ? ("(" + lhs + ')') : lhs) + " || "
     506                    + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof Or) ? ("(" + rhs + ')') : rhs);
    484507        }
    485508    }
     
    488511     * Matches if the left OR the right expression match, but not both.
    489512     */
    490     public static class Xor extends BinaryMatch {
     513    public static class Xor extends AbstractBinaryMatch {
     514        /**
     515         * Constructs a new {@code Xor} match.
     516         * @param lhs left hand side
     517         * @param rhs right hand side
     518         */
    491519        public Xor(Match lhs, Match rhs) {
    492520            super(lhs, rhs);
     
    505533        @Override
    506534        public String toString() {
    507             return (lhs instanceof BinaryMatch && !(lhs instanceof Xor) ? "(" + lhs + ")" : lhs) + " ^ "
    508                     + (rhs instanceof BinaryMatch && !(rhs instanceof Xor) ? "(" + rhs + ")" : rhs);
     535            return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof Xor) ? ("(" + lhs + ')') : lhs) + " ^ "
     536                    + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof Xor) ? ("(" + rhs + ')') : rhs);
    509537        }
    510538    }
     
    17551783        final String forKey = '"' + escapeStringForSearch(key) + '"' + '=';
    17561784        if (value == null || value.isEmpty()) {
    1757             return forKey + "*";
     1785            return forKey + '*';
    17581786        } else {
    17591787            return forKey + '"' + escapeStringForSearch(value) + '"';
  • trunk/src/org/openstreetmap/josm/data/AutosaveTask.java

    r10212 r10299  
    1313import java.nio.charset.StandardCharsets;
    1414import java.nio.file.Files;
     15import java.nio.file.Path;
    1516import java.util.ArrayList;
    1617import java.util.Date;
     
    7374    public static final BooleanProperty PROP_NOTIFICATION = new BooleanProperty("autosave.notification", false);
    7475
    75     private static class AutosaveLayerInfo {
    76         private OsmDataLayer layer;
     76    protected static final class AutosaveLayerInfo {
     77        private final OsmDataLayer layer;
    7778        private String layerName;
    7879        private String layerFileName;
    7980        private final Deque<File> backupFiles = new LinkedList<>();
     81
     82        AutosaveLayerInfo(OsmDataLayer layer) {
     83            this.layer = layer;
     84        }
    8085    }
    8186
     
    8994    private final File autosaveDir = new File(Main.pref.getUserDataDirectory(), AUTOSAVE_DIR);
    9095    private final File deletedLayersDir = new File(Main.pref.getUserDataDirectory(), DELETED_LAYERS_DIR);
     96
     97    /**
     98     * Replies the autosave directory.
     99     * @return the autosave directory
     100     * @since 10299
     101     */
     102    public final Path getAutosaveDir() {
     103        return autosaveDir.toPath();
     104    }
    91105
    92106    public void schedule() {
     
    153167    }
    154168
    155     private File getNewLayerFile(AutosaveLayerInfo layer) {
    156         int index = 0;
    157         Date now = new Date();
     169    protected File getNewLayerFile(AutosaveLayerInfo layer, Date now, int startIndex) {
     170        int index = startIndex;
    158171        while (true) {
    159172            String filename = String.format("%1$s_%2$tY%2$tm%2$td_%2$tH%2$tM%2$tS%2$tL%3$s",
    160                     layer.layerFileName, now, index == 0 ? "" : '_' + index);
    161             File result = new File(autosaveDir, filename + "." + Main.pref.get("autosave.extension", "osm"));
     173                    layer.layerFileName, now, index == 0 ? "" : ("_" + index));
     174            File result = new File(autosaveDir, filename + '.' + Main.pref.get("autosave.extension", "osm"));
    162175            try {
     176                if (index > PROP_INDEX_LIMIT.get())
     177                    throw new IOException("index limit exceeded");
    163178                if (result.createNewFile()) {
    164                     File pidFile = new File(autosaveDir, filename+".pid");
    165                     try (PrintStream ps = new PrintStream(pidFile, "UTF-8")) {
    166                         ps.println(ManagementFactory.getRuntimeMXBean().getName());
    167                     } catch (IOException | SecurityException t) {
    168                         Main.error(t);
    169                     }
     179                    createNewPidFile(autosaveDir, filename);
    170180                    return result;
    171181                } else {
    172182                    Main.warn(tr("Unable to create file {0}, other filename will be used", result.getAbsolutePath()));
    173                     if (index > PROP_INDEX_LIMIT.get())
    174                         throw new IOException("index limit exceeded");
    175183                }
    176184            } catch (IOException e) {
     
    179187            }
    180188            index++;
     189        }
     190    }
     191
     192    private static void createNewPidFile(File autosaveDir, String filename) {
     193        File pidFile = new File(autosaveDir, filename+".pid");
     194        try (PrintStream ps = new PrintStream(pidFile, "UTF-8")) {
     195            ps.println(ManagementFactory.getRuntimeMXBean().getName());
     196        } catch (IOException | SecurityException t) {
     197            Main.error(t);
    181198        }
    182199    }
     
    188205        }
    189206        if (changedDatasets.remove(info.layer.data)) {
    190             File file = getNewLayerFile(info);
     207            File file = getNewLayerFile(info, new Date(), 0);
    191208            if (file != null) {
    192209                info.backupFiles.add(file);
     
    240257        synchronized (layersLock) {
    241258            layer.data.addDataSetListener(datasetAdapter);
    242             AutosaveLayerInfo info = new AutosaveLayerInfo();
    243             info.layer = layer;
    244             layersInfo.add(info);
     259            layersInfo.add(new AutosaveLayerInfo(layer));
    245260        }
    246261    }
     
    287302    }
    288303
    289     private File getPidFile(File osmFile) {
     304    protected File getPidFile(File osmFile) {
    290305        return new File(autosaveDir, osmFile.getName().replaceFirst("[.][^.]+$", ".pid"));
    291306    }
  • trunk/test/unit/org/openstreetmap/josm/actions/ExtensionFileFilterTest.java

    r9669 r10299  
    22package org.openstreetmap.josm.actions;
    33
    4 import nl.jqno.equalsverifier.EqualsVerifier;
     4import static org.junit.Assert.assertEquals;
    55
    66import org.junit.Test;
     7
     8import nl.jqno.equalsverifier.EqualsVerifier;
    79
    810/**
     
    1012 */
    1113public class ExtensionFileFilterTest {
     14
     15    private static void test(String extensions, String defaultExtension, String description, boolean addArchiveExtensionsToDescription,
     16            String expectedExtensions, String expectedDescription) {
     17        ExtensionFileFilter ext = ExtensionFileFilter.newFilterWithArchiveExtensions(
     18                extensions, defaultExtension, description, addArchiveExtensionsToDescription);
     19        assertEquals(expectedExtensions, ext.getExtensions());
     20        assertEquals(defaultExtension, ext.getDefaultExtension());
     21        assertEquals(expectedDescription, ext.getDescription());
     22    }
     23
     24    /**
     25     * Unit test of method {@link ExtensionFileFilter#newFilterWithArchiveExtensions}.
     26     */
     27    @Test
     28    public void testNewFilterWithArchiveExtensions() {
     29        test("ext1", "ext1", "description", true,
     30                "ext1,ext1.gz,ext1.bz2", "description (*.ext1, *.ext1.gz, *.ext1.bz2)");
     31        test("ext1", "ext1", "description", false,
     32                "ext1,ext1.gz,ext1.bz2", "description (*.ext1)");
     33        test("ext1,ext2", "ext1", "description", true,
     34                "ext1,ext1.gz,ext1.bz2,ext2,ext2.gz,ext2.bz2", "description (*.ext1, *.ext1.gz, *.ext1.bz2, *.ext2, *.ext2.gz, *.ext2.bz2)");
     35        test("ext1,ext2", "ext1", "description", false,
     36                "ext1,ext1.gz,ext1.bz2,ext2,ext2.gz,ext2.bz2", "description (*.ext1, *.ext2)");
     37    }
    1238
    1339    /**
  • trunk/test/unit/org/openstreetmap/josm/actions/search/SearchCompilerTest.java

    r9940 r10299  
    427427        final SearchCompiler.Match c4 = SearchCompiler.compile("foo1 OR (bar1 bar2 baz1 XOR baz2) OR foo2");
    428428        assertEquals("foo1 || (bar1 && bar2 && (baz1 ^ baz2)) || foo2", c4.toString());
     429        final SearchCompiler.Match c5 = SearchCompiler.compile("foo1 XOR (baz1 XOR (bar baz))");
     430        assertEquals("foo1 ^ baz1 ^ (bar && baz)", c5.toString());
     431        final SearchCompiler.Match c6 = SearchCompiler.compile("foo1 XOR ((baz1 baz2) XOR (bar OR baz))");
     432        assertEquals("foo1 ^ (baz1 && baz2) ^ (bar || baz)", c6.toString());
    429433    }
    430434
Note: See TracChangeset for help on using the changeset viewer.