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

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

see #15229 - update method name and signature for consistency

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