source: josm/trunk/src/org/openstreetmap/josm/data/preferences/sources/SourcePrefHelper.java@ 12846

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences.sources;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashMap;
7import java.util.LinkedHashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import org.openstreetmap.josm.spi.preferences.Config;
13
14/**
15 * Helper class for specialized extensions preferences.
16 * @since 12649 (extracted from gui.preferences package)
17 */
18public abstract class SourcePrefHelper {
19
20 private final String pref;
21 protected final SourceType type;
22
23 /**
24 * Constructs a new {@code SourcePrefHelper} for the given preference key.
25 * @param pref The preference key
26 * @param type The source type
27 * @since 12825
28 */
29 public SourcePrefHelper(String pref, SourceType type) {
30 this.pref = pref;
31 this.type = type;
32 }
33
34 /**
35 * Returns the default sources provided by JOSM core.
36 * @return the default sources provided by JOSM core
37 */
38 public abstract Collection<ExtendedSourceEntry> getDefault();
39
40 /**
41 * Serializes the given source entry as a map.
42 * @param entry source entry to serialize
43 * @return map (key=value)
44 */
45 public abstract Map<String, String> serialize(SourceEntry entry);
46
47 /**
48 * Deserializes the given map as a source entry.
49 * @param entryStr map (key=value)
50 * @return source entry
51 */
52 public abstract SourceEntry deserialize(Map<String, String> entryStr);
53
54 /**
55 * Returns the list of sources.
56 * @return The list of sources
57 */
58 public List<SourceEntry> get() {
59
60 List<Map<String, String>> src = Config.getPref().getListOfMaps(pref, null);
61 if (src == null)
62 return new ArrayList<>(getDefault());
63
64 List<SourceEntry> entries = new ArrayList<>();
65 for (Map<String, String> sourcePref : src) {
66 SourceEntry e = deserialize(new HashMap<>(sourcePref));
67 if (e != null) {
68 entries.add(e);
69 }
70 }
71 return entries;
72 }
73
74 /**
75 * Saves a list of sources to JOSM preferences.
76 * @param entries list of sources
77 * @return {@code true}, if something has changed (i.e. value is different than before)
78 */
79 public boolean put(Collection<? extends SourceEntry> entries) {
80 List<Map<String, String>> setting = serializeList(entries);
81 boolean unset = Config.getPref().getListOfMaps(pref, null) == null;
82 if (unset) {
83 Collection<Map<String, String>> def = serializeList(getDefault());
84 if (setting.equals(def))
85 return false;
86 }
87 return Config.getPref().putListOfMaps(pref, setting);
88 }
89
90 private List<Map<String, String>> serializeList(Collection<? extends SourceEntry> entries) {
91 List<Map<String, String>> setting = new ArrayList<>(entries.size());
92 for (SourceEntry e : entries) {
93 setting.add(serialize(e));
94 }
95 return setting;
96 }
97
98 /**
99 * Returns the set of active source URLs.
100 * @return The set of active source URLs.
101 */
102 public final Set<String> getActiveUrls() {
103 Set<String> urls = new LinkedHashSet<>(); // retain order
104 for (SourceEntry e : get()) {
105 if (e.active) {
106 urls.add(e.url);
107 }
108 }
109 return urls;
110 }
111}
Note: See TracBrowser for help on using the repository browser.