Changeset 15950 in josm


Ignore:
Timestamp:
2020-02-28T00:24:17+01:00 (4 years ago)
Author:
simon04
Message:

Simplify OsmUtils.isTrue/isFalse/isReversed

Just use a plain switch statement instead of a HashMap

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/OsmUtils.java

    r15692 r15950  
    22package org.openstreetmap.josm.data.osm;
    33
    4 import java.util.Arrays;
    54import java.util.Collection;
    6 import java.util.HashSet;
    75import java.util.Locale;
    86import java.util.Map;
    9 import java.util.Set;
    107import java.util.regex.Pattern;
    118import java.util.stream.Stream;
     
    1916 */
    2017public final class OsmUtils {
    21 
    22     private static final Set<String> TRUE_VALUES = new HashSet<>(Arrays
    23             .asList("true", "yes", "1", "on"));
    24     private static final Set<String> FALSE_VALUES = new HashSet<>(Arrays
    25             .asList("false", "no", "0", "off"));
    26     private static final Set<String> REVERSE_VALUES = new HashSet<>(Arrays
    27             .asList("reverse", "-1"));
    2818
    2919    /**
     
    7060        if (value == null) return null;
    7161        String lowerValue = value.toLowerCase(Locale.ENGLISH);
    72         if (TRUE_VALUES.contains(lowerValue)) return Boolean.TRUE;
    73         if (FALSE_VALUES.contains(lowerValue)) return Boolean.FALSE;
     62        if (isTrue(lowerValue)) return Boolean.TRUE;
     63        if (isFalse(lowerValue)) return Boolean.FALSE;
    7464        return null;
    7565    }
     
    9383     */
    9484    public static boolean isReversed(String value) {
    95         return REVERSE_VALUES.contains(value);
     85        switch (value) {
     86            case "reverse":
     87            case "-1":
     88                return true;
     89            default:
     90                return false;
     91        }
    9692    }
    9793
     
    10298     */
    10399    public static boolean isTrue(String value) {
    104         return TRUE_VALUES.contains(value);
     100        switch (value) {
     101            case "true":
     102            case "yes":
     103            case "1":
     104            case "on":
     105                return true;
     106            default:
     107                return false;
     108        }
    105109    }
    106110
     
    111115     */
    112116    public static boolean isFalse(String value) {
    113         return FALSE_VALUES.contains(value);
     117        switch (value) {
     118            case "false":
     119            case "no":
     120            case "0":
     121            case "off":
     122                return true;
     123            default:
     124                return false;
     125        }
    114126    }
    115127
  • trunk/test/unit/org/openstreetmap/josm/data/osm/OsmUtilsTest.java

    r15671 r15950  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertNull;
    57import static org.junit.Assert.assertTrue;
    68
     
    1416import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1517
     18/**
     19 * Unit tests for class {@link OsmUtils}.
     20 */
    1621public class OsmUtilsTest {
    1722
     
    2328    public JOSMTestRules test = new JOSMTestRules();
    2429
     30    /**
     31     * Unit test of {@link OsmUtils#createPrimitive}
     32     */
    2533    @Test
    26     public void testCreatePrimitive() throws Exception {
     34    public void testCreatePrimitive() {
    2735        final OsmPrimitive p = OsmUtils.createPrimitive("way name=Foo railway=rail");
    2836        assertTrue(p instanceof Way);
     
    3240    }
    3341
     42    /**
     43     * Unit test of {@link OsmUtils#createPrimitive}
     44     */
    3445    @Test
    35     public void testArea() throws Exception {
     46    public void testArea() {
    3647        final OsmPrimitive p = OsmUtils.createPrimitive("area name=Foo railway=rail");
    3748        assertEquals(OsmPrimitiveType.WAY, p.getType());
    38         assertTrue(p.getKeys().equals(OsmUtils.createPrimitive("way name=Foo railway=rail").getKeys()));
     49        assertEquals(p.getKeys(), OsmUtils.createPrimitive("way name=Foo railway=rail").getKeys());
    3950    }
    4051
     52    /**
     53     * Unit test of {@link OsmUtils#createPrimitive}
     54     */
    4155    @Test(expected = IllegalArgumentException.class)
    42     public void testCreatePrimitiveFail() throws Exception {
     56    public void testCreatePrimitiveFail() {
    4357        OsmUtils.createPrimitive("noway name=Foo");
    4458    }
    4559
     60    /**
     61     * Unit test of {@link OsmUtils#splitMultipleValues}
     62     */
    4663    @Test
    4764    public void testSplitMultipleValues() {
     
    5269                OsmUtils.splitMultipleValues("Tu-Fr 08:00-18:00;Mo 09:00-18:00;Sa 09:00-12:00;closed Aug").collect(Collectors.toList()));
    5370    }
     71
     72    /**
     73     * Unit test of {@link OsmUtils#isTrue}, {@link OsmUtils#isFalse}, {@link OsmUtils#getOsmBoolean}
     74     */
     75    @Test
     76    public void testTrueFalse() {
     77        assertTrue(OsmUtils.isTrue("yes"));
     78        assertFalse(OsmUtils.isFalse("yes"));
     79        assertEquals(Boolean.TRUE, OsmUtils.getOsmBoolean("yes"));
     80        assertTrue(OsmUtils.isFalse("no"));
     81        assertFalse(OsmUtils.isTrue("no"));
     82        assertEquals(Boolean.FALSE, OsmUtils.getOsmBoolean("no"));
     83        assertNull(OsmUtils.getOsmBoolean("foobar"));
     84    }
    5485}
Note: See TracChangeset for help on using the changeset viewer.