Changeset 15909 in josm for trunk/src/org


Ignore:
Timestamp:
2020-02-23T00:29:53+01:00 (5 years ago)
Author:
simon04
Message:

see #18749 - Use efficient/unmodifiable collections

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSRule.java

    r15717 r15909  
    88import org.openstreetmap.josm.gui.mappaint.Environment;
    99import org.openstreetmap.josm.gui.mappaint.StyleSource;
     10import org.openstreetmap.josm.tools.Utils;
    1011
    1112/**
     
    4849         */
    4950        public Declaration(List<Instruction> instructions, int idx) {
    50             this.instructions = instructions;
     51            this.instructions = Utils.toUnmodifiableList(instructions);
    5152            this.idx = idx;
    5253        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r15730 r15909  
    537537
    538538        protected AbstractSelector(List<Condition> conditions) {
    539             if (conditions == null || conditions.isEmpty()) {
    540                 this.conds = null;
    541             } else {
    542                 this.conds = conditions;
    543             }
     539            this.conds = Utils.toUnmodifiableList(conditions);
    544540        }
    545541
     
    552548        public boolean matches(Environment env) {
    553549            CheckParameterUtil.ensureParameterNotNull(env, "env");
    554             if (conds == null) return true;
    555550            for (Condition c : conds) {
    556551                try {
     
    569564         */
    570565        public List<Condition> getConditions() {
    571             if (conds == null) {
    572                 return Collections.emptyList();
    573             }
    574             return Collections.unmodifiableList(conds);
     566            return conds;
    575567        }
    576568    }
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetSelector.java

    r15583 r15909  
    8989        public int classification;
    9090        public int favoriteIndex;
    91         private final Collection<String> groups = new HashSet<>();
    92         private final Collection<String> names = new HashSet<>();
    93         private final Collection<String> tags = new HashSet<>();
     91        private final Collection<String> groups;
     92        private final Collection<String> names;
     93        private final Collection<String> tags;
    9494
    9595        PresetClassification(TaggingPreset preset) {
    9696            this.preset = preset;
     97            Set<String> groups = new HashSet<>();
     98            Set<String> names = new HashSet<>();
     99            Set<String> tags = new HashSet<>();
    97100            TaggingPreset group = preset.group;
    98101            while (group != null) {
     
    119122                }
    120123            }
     124            this.groups = Utils.toUnmodifiableList(groups);
     125            this.names = Utils.toUnmodifiableList(names);
     126            this.tags = Utils.toUnmodifiableList(tags);
    121127        }
    122128
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r15866 r15909  
    748748
    749749    /**
     750     * Returns an unmodifiable list for the given collection.
     751     * Makes use of {@link Collections#emptySet()} and {@link Collections#singleton} and {@link Arrays#asList} to save memory.
     752     * @param  collection the collection for which an unmodifiable collection is to be returned
     753     * @param <T> the class of the objects in the array
     754     * @return an unmodifiable list
     755     * @see <a href="https://dzone.com/articles/preventing-your-java-collections-from-wasting-memo">
     756     *     How to Prevent Your Java Collections From Wasting Memory</a>
     757     */
     758    @SuppressWarnings("unchecked")
     759    public static <T> List<T> toUnmodifiableList(Collection<T> collection) {
     760        // Java 9: use List.of(...)
     761        if (collection == null || collection.isEmpty()) {
     762            return Collections.emptyList();
     763        } else if (collection.size() == 1) {
     764            return Collections.singletonList(collection.iterator().next());
     765        } else {
     766            return (List<T>) Arrays.asList(collection.toArray());
     767        }
     768    }
     769
     770    /**
    750771     * Returns the first not empty string in the given candidates, otherwise the default string.
    751772     * @param defaultString default string returned if all candidates would be empty if stripped
Note: See TracChangeset for help on using the changeset viewer.