source: josm/trunk/src/org/openstreetmap/josm/data/preferences/AbstractToStringProperty.java@ 14273

Last change on this file since 14273 was 14114, checked in by Don-vip, 6 years ago

fix NPE seen in integration test

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences;
3
4import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
5import org.openstreetmap.josm.tools.CheckParameterUtil;
6import org.openstreetmap.josm.tools.Logging;
7import org.openstreetmap.josm.tools.bugreport.BugReport;
8
9/**
10 * This class represents a property that can be represented as String.
11 *
12 * @author Michael Zangl
13 *
14 * @param <T> The property content type.
15 * @since 10824
16 */
17public abstract class AbstractToStringProperty<T> extends AbstractProperty<T> {
18
19 /**
20 * This is a version of this property that attempts to get the property with a more specialized key and - if that fails - uses the property
21 * value as default.
22 *
23 * @author Michael Zangl
24 * @param <T> The content type
25 */
26 public static class ChildProperty<T> extends AbstractToStringProperty<T> {
27 private final AbstractToStringProperty<T> parent;
28
29 ChildProperty(AbstractToStringProperty<T> parent, String key) {
30 super(key, null);
31 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
32 this.parent = parent;
33 }
34
35 @Override
36 protected void storeDefaultValue() {
37 // Default value hidden in preferences.
38 }
39
40 @Override
41 public T getDefaultValue() {
42 return parent.get();
43 }
44
45 @Override
46 protected T fromString(String string) {
47 return parent.fromString(string);
48 }
49
50 @Override
51 protected String toString(T t) {
52 return parent.toString(t);
53 }
54
55 @Override
56 protected void addListenerImpl(PreferenceChangedListener adapter) {
57 super.addListenerImpl(adapter);
58 parent.addListenerImpl(adapter);
59 }
60
61 @Override
62 protected void removeListenerImpl(PreferenceChangedListener adapter) {
63 super.removeListenerImpl(adapter);
64 parent.removeListenerImpl(adapter);
65 }
66
67 @Override
68 public CachingProperty<T> cached() {
69 throw new UnsupportedOperationException("Not implemented yet.");
70 }
71 }
72
73 /**
74 * Create a new property and store the default value.
75 * @param key The key
76 * @param defaultValue The default value.
77 * @see AbstractProperty#AbstractProperty(String, Object)
78 */
79 public AbstractToStringProperty(String key, T defaultValue) {
80 super(key, defaultValue);
81 storeDefaultValue();
82 }
83
84 @Override
85 public T get() {
86 String string = getAsString();
87 if (!string.isEmpty()) {
88 try {
89 return fromString(string);
90 } catch (InvalidPreferenceValueException e) {
91 Logging.warn(BugReport.intercept(e).put("key", key).put("value", string));
92 }
93 }
94 return getDefaultValue();
95 }
96
97 /**
98 * Converts the string to an object of the given type.
99 * @param string The string
100 * @return The object.
101 * @throws InvalidPreferenceValueException If the value could not be converted.
102 */
103 protected abstract T fromString(String string);
104
105 @Override
106 public boolean put(T value) {
107 String string = value == null ? null : toString(value);
108 return getPreferences().put(getKey(), string);
109 }
110
111 /**
112 * Converts the string to an object of the given type.
113 * @param t The object.
114 * @return The string representing the object
115 * @throws InvalidPreferenceValueException If the value could not be converted.
116 */
117 protected abstract String toString(T t);
118
119 /**
120 * Gets the preference value as String.
121 * @return The string preference value.
122 */
123 protected String getAsString() {
124 T def = getDefaultValue();
125 String sdef = def == null ? "" : toString(def);
126 return getPreferences() != null ? getPreferences().get(key, sdef) : sdef;
127 }
128
129 /**
130 * Gets a specialized setting value that has the current value as default
131 * <p>
132 * The key will be getKey().spec
133 * @param spec The key specialization
134 * @return The property
135 */
136 public AbstractToStringProperty<T> getSpecialized(String spec) {
137 return getChildProperty(getKey() + "." + spec);
138 }
139
140 /**
141 * Gets a setting that defaults to this setting if the key is not set.
142 * @param key The more specialized key.
143 * @return The new setting.
144 */
145 protected AbstractToStringProperty<T> getChildProperty(String key) {
146 return new ChildProperty<>(this, key);
147 }
148
149}
Note: See TracBrowser for help on using the repository browser.