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

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

see #15182 - move UploadStrategySpecification and required enums from gui.io to io

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