Changeset 15982 in josm


Ignore:
Timestamp:
2020-03-01T23:35:53+01:00 (4 years ago)
Author:
simon04
Message:

see #18802 - MapCSSTagChecker.TagCheck: use unmodifiable collections

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r15981 r15982  
    283283        protected final GroupedMapCSSRule rule;
    284284        /** Commands to apply in order to fix a matching primitive */
    285         protected final List<FixCommand> fixCommands = new ArrayList<>();
     285        protected final List<FixCommand> fixCommands;
    286286        /** Tags (or arbitrary strings) of alternatives to be presented to the user */
    287         protected final List<String> alternatives = new ArrayList<>();
     287        protected final List<String> alternatives;
    288288        /** An {@link org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.AssignmentInstruction}-{@link Severity} pair.
    289289         * Is evaluated on the matching primitive to give the error message. Map is checked to contain exactly one element. */
    290         protected final Map<Instruction.AssignmentInstruction, Severity> errors = new HashMap<>();
     290        protected final Map<Instruction.AssignmentInstruction, Severity> errors;
    291291        /** MapCSS Classes to set on matching primitives */
    292         protected final Set<String> setClassExpressions = new HashSet<>();
     292        protected final Collection<String> setClassExpressions;
    293293        /** Denotes whether the object should be deleted for fixing it */
    294294        protected boolean deletion;
     
    298298        TagCheck(GroupedMapCSSRule rule) {
    299299            this.rule = rule;
     300            this.fixCommands = new ArrayList<>();
     301            this.alternatives = new ArrayList<>();
     302            this.errors = new HashMap<>();
     303            this.setClassExpressions = new HashSet<>();
     304        }
     305
     306        TagCheck(TagCheck check) {
     307            this.rule = check.rule;
     308            this.fixCommands = Utils.toUnmodifiableList(check.fixCommands);
     309            this.alternatives = Utils.toUnmodifiableList(check.alternatives);
     310            this.errors = Utils.toUnmodifiableMap(check.errors);
     311            this.setClassExpressions = Utils.toUnmodifiableList(check.setClassExpressions);
     312            this.deletion = check.deletion;
     313            this.group = check.group;
     314        }
     315
     316        TagCheck toImmutable() {
     317            return new TagCheck(this);
    300318        }
    301319
     
    372390                MapCSSTagCheckerAsserts.checkAsserts(check, assertions, assertionConsumer);
    373391            }
    374             return check;
     392            return check.toImmutable();
    375393        }
    376394
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r15909 r15982  
    4848import java.util.List;
    4949import java.util.Locale;
     50import java.util.Map;
    5051import java.util.Optional;
    5152import java.util.concurrent.ExecutionException;
     
    6970import javax.script.ScriptEngineManager;
    7071
     72import com.kitfox.svg.xml.XMLParseUtil;
    7173import org.openstreetmap.josm.spi.preferences.Config;
    7274
     
    766768            return (List<T>) Arrays.asList(collection.toArray());
    767769        }
     770    }
     771
     772    /**
     773     * Returns an unmodifiable map for the given map.
     774     * Makes use of {@link Collections#emptyMap()} and {@link Collections#singletonMap} and {@link Map#ofEntries(Map.Entry[])} to save memory.
     775     *
     776     * @param map the map for which an unmodifiable map is to be returned
     777     * @param <K> the type of keys maintained by this map
     778     * @param <V> the type of mapped values
     779     * @return an unmodifiable map
     780     * @see <a href="https://dzone.com/articles/preventing-your-java-collections-from-wasting-memo">
     781     *     How to Prevent Your Java Collections From Wasting Memory</a>
     782     */
     783    public static <K, V> Map<K, V> toUnmodifiableMap(Map<K, V> map) {
     784        return XMLParseUtil.toUnmodifiableMap(map);
    768785    }
    769786
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r15909 r15982  
    1414import java.util.Arrays;
    1515import java.util.Collections;
     16import java.util.HashMap;
    1617import java.util.LinkedList;
    1718import java.util.List;
    1819import java.util.Locale;
     20import java.util.Map;
     21import java.util.TreeMap;
    1922import java.util.regex.Pattern;
    2023
     
    574577        assertEquals(Arrays.asList("foo", "bar", "baz"), Utils.toUnmodifiableList(new LinkedList<>(Arrays.asList("foo", "bar", "baz"))));
    575578    }
     579
     580    /**
     581     * Test of {@link Utils#toUnmodifiableMap}
     582     */
     583    @Test
     584    public void testToUnmodifiableMap() {
     585        assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(null));
     586        assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(Collections.emptyMap()));
     587        assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(new HashMap<>()));
     588        assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(new TreeMap<>()));
     589        assertEquals(Collections.singletonMap("foo", "bar"), Utils.toUnmodifiableMap(new HashMap<>(Collections.singletonMap("foo", "bar"))));
     590        assertEquals(Collections.singletonMap("foo", "bar"), Utils.toUnmodifiableMap(new TreeMap<>(Collections.singletonMap("foo", "bar"))));
     591        final Map<String, String> map4 = new HashMap<>();
     592        map4.put("jjj", "foo");
     593        map4.put("ooo", "bar");
     594        map4.put("sss", "baz");
     595        map4.put("mmm", ":-)");
     596        assertEquals(map4, Utils.toUnmodifiableMap(map4));
     597    }
    576598}
Note: See TracChangeset for help on using the changeset viewer.