source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetNameTemplateList.java@ 12042

Last change on this file since 12042 was 10044, checked in by Don-vip, 8 years ago

sonar - squid:S1641 - Sets with elements that are enum values should be replaced with EnumSet

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets;
3
4import java.util.Collection;
5import java.util.EnumSet;
6import java.util.LinkedList;
7import java.util.List;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11
12/**
13 * List of tagging presets with name templates, allows to find appropriate template based on existing primitive
14 */
15public final class TaggingPresetNameTemplateList implements TaggingPresetListener {
16
17 private static TaggingPresetNameTemplateList instance;
18
19 private final List<TaggingPreset> presetsWithPattern = new LinkedList<>();
20
21 /**
22 * Replies the unique instance.
23 * @return the unique instance
24 */
25 public static synchronized TaggingPresetNameTemplateList getInstance() {
26 if (instance == null) {
27 instance = new TaggingPresetNameTemplateList();
28 TaggingPresets.addListener(instance);
29 }
30 return instance;
31 }
32
33 private TaggingPresetNameTemplateList() {
34 buildPresetsWithPattern();
35 }
36
37 private void buildPresetsWithPattern() {
38 synchronized (this) {
39 Main.debug("Building list of presets with name template");
40 presetsWithPattern.clear();
41 for (TaggingPreset tp : TaggingPresets.getTaggingPresets()) {
42 if (tp.nameTemplate != null) {
43 presetsWithPattern.add(tp);
44 }
45 }
46 }
47 }
48
49 /**
50 * Finds and returns the first occurence of preset with template name matching the given primitive
51 * @param primitive The primitive to match
52 * @return the first occurence of preset with template name matching the primitive
53 */
54 public TaggingPreset findPresetTemplate(OsmPrimitive primitive) {
55 synchronized (this) {
56 for (TaggingPreset t : presetsWithPattern) {
57 Collection<TaggingPresetType> type = EnumSet.of(TaggingPresetType.forPrimitive(primitive));
58 if (t.typeMatches(type)) {
59 if (t.nameTemplateFilter != null) {
60 if (t.nameTemplateFilter.match(primitive))
61 return t;
62 else {
63 continue;
64 }
65 } else if (t.matches(type, primitive.getKeys(), false)) {
66 return t;
67 }
68 }
69 }
70 }
71 return null;
72 }
73
74 @Override
75 public void taggingPresetsModified() {
76 buildPresetsWithPattern();
77 }
78}
Note: See TracBrowser for help on using the repository browser.