source: josm/trunk/src/org/openstreetmap/josm/gui/io/UploadStrategy.java@ 12012

Last change on this file since 12012 was 11489, checked in by Don-vip, 7 years ago

error-prone - enums should be immutable, and cannot have non-final fields

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Locale;
7
8import org.openstreetmap.josm.Main;
9
10public enum UploadStrategy {
11 /**
12 * Uploads the objects individually, one request per object
13 */
14 INDIVIDUAL_OBJECTS_STRATEGY("individualobjects"),
15 /**
16 * Upload the objects in junks of n objects using m diff uploads
17 */
18 CHUNKED_DATASET_STRATEGY("chunked"),
19 /**
20 * Upload the objects in one request using 1 diff upload
21 */
22 SINGLE_REQUEST_STRATEGY("singlerequest");
23
24 private final String preferenceValue;
25
26 UploadStrategy(String preferenceValue) {
27 this.preferenceValue = preferenceValue;
28 }
29
30 public static UploadStrategy fromPreference(String preferenceValue) {
31 if (preferenceValue == null) return null;
32 preferenceValue = preferenceValue.trim().toLowerCase(Locale.ENGLISH);
33 for (UploadStrategy strategy: values()) {
34 if (strategy.getPreferenceValue().equals(preferenceValue))
35 return strategy;
36 }
37 return null;
38 }
39
40 /**
41 * Replies the value which is written to the preferences for a specific
42 * upload strategy
43 *
44 * @return the value which is written to the preferences for a specific
45 * upload strategy
46 */
47 public String getPreferenceValue() {
48 return preferenceValue;
49 }
50
51 /**
52 * the default upload strategy
53 */
54 public static final UploadStrategy DEFAULT_UPLOAD_STRATEGY = SINGLE_REQUEST_STRATEGY;
55
56 /**
57 * Replies the upload strategy currently configured in the preferences.
58 *
59 * First checks for the preference key <pre>osm-server.upload-strategy</pre>. If not
60 * present, checks for the legacy preference key <pre>osm-server.atomic-upload</pre>.
61 *
62 * If both are missing or if the preference value is illegal, {@link #DEFAULT_UPLOAD_STRATEGY}
63 * is replied.
64 *
65 * @return the upload strategy currently configured in the preferences.
66 */
67 public static UploadStrategy getFromPreferences() {
68 String v = Main.pref.get("osm-server.upload-strategy", null);
69 if (v == null) {
70 // legacy support. Until 12/2009 we had osm-server.atomic-upload only.
71 // If we still find "osm-server.atomic-upload" we use it and remove it.
72 // When the preferences are saved the next time, "osm-server.upload-strategy"
73 // will be inserted.
74 v = Main.pref.get("osm-server.atomic-upload", null);
75 if (v != null) {
76 Main.pref.removeFromCollection("osm-server.atomic-upload", v);
77 } else {
78 v = "";
79 }
80 v = v.trim().toLowerCase(Locale.ENGLISH);
81 if ("true".equals(v))
82 return SINGLE_REQUEST_STRATEGY;
83 else if ("false".equals(v))
84 return INDIVIDUAL_OBJECTS_STRATEGY;
85 else
86 return DEFAULT_UPLOAD_STRATEGY;
87 }
88 UploadStrategy strategy = fromPreference(v);
89 if (strategy == null) {
90 Main.warn(tr("Unexpected value for key ''{0}'' in preferences, got ''{1}''", "osm-server.upload-strategy", v));
91 return DEFAULT_UPLOAD_STRATEGY;
92 }
93 return strategy;
94 }
95
96 /**
97 * Saves the upload strategy <code>strategy</code> to the preferences.
98 *
99 * @param strategy the strategy to save
100 */
101 public static void saveToPreferences(UploadStrategy strategy) {
102 Main.pref.put("osm-server.upload-strategy", strategy.getPreferenceValue());
103 }
104}
Note: See TracBrowser for help on using the repository browser.