source: josm/trunk/src/org/openstreetmap/josm/io/UploadStrategySpecification.java@ 18283

Last change on this file since 18283 was 18283, checked in by Don-vip, 3 years ago

fix #21427 - further simplify UploadDialog (patch by marcello, modified)

  • The dialog was simplified by combining the function of two radiobuttons and one combobox into one combobox.
  • When an open changeset was selected on tab 2, existing tags on the open changeset could overwrite the data the user entered on tab 1. The user might spot this by looking closely at the tag table on tab 2, but then he may not. This non-obvious behaviour was removed.
  • The exception thrown when closing an already closed changeset was fixed.
  • More cosmetic changes to the dialog.
  • Maybe also a solution to #19319, #21387 (added revalidate()).
  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.util.Objects;
5
6/**
7 * An UploadStrategySpecification consists of the parameter describing the strategy
8 * for uploading a collection of {@link org.openstreetmap.josm.data.osm.OsmPrimitive}.
9 *
10 * This includes:
11 * <ul>
12 * <li>a decision on which {@link UploadStrategy} to use</li>
13 * <li>the upload chunk size</li>
14 * <li>whether to close the changeset used after the upload</li>
15 * </ul>
16 * @since 12687 (moved from {@code gui.io} package)
17 */
18public class UploadStrategySpecification {
19 /** indicates that the chunk size isn't specified */
20 public static final int UNSPECIFIED_CHUNK_SIZE = -1;
21
22 private UploadStrategy strategy;
23 private int chunkSize;
24 private MaxChangesetSizeExceededPolicy policy;
25 private boolean closeChangesetAfterUpload;
26
27 /**
28 * Creates a new upload strategy with default values.
29 */
30 public UploadStrategySpecification() {
31 this.strategy = UploadStrategy.DEFAULT_UPLOAD_STRATEGY;
32 this.chunkSize = UNSPECIFIED_CHUNK_SIZE;
33 this.policy = null;
34 this.closeChangesetAfterUpload = true;
35 }
36
37 /**
38 * Clones another upload strategy. If other is null, assumes default values.
39 *
40 * @param other the other upload strategy
41 */
42 public UploadStrategySpecification(UploadStrategySpecification other) {
43 if (other != null) {
44 this.strategy = other.strategy;
45 this.chunkSize = other.chunkSize;
46 this.policy = other.policy;
47 this.closeChangesetAfterUpload = other.closeChangesetAfterUpload;
48 }
49 }
50
51 /**
52 * Replies the upload strategy
53 * @return the upload strategy
54 */
55 public UploadStrategy getStrategy() {
56 return strategy;
57 }
58
59 /**
60 * Gets the chunk size
61 * @return The max size of each upload chunk
62 */
63 public int getChunkSize() {
64 return chunkSize;
65 }
66
67 /**
68 * Gets a special value that is used to indicate that the chunk size was not specified
69 * @return A special integer
70 */
71 public static int getUnspecifiedChunkSize() {
72 return UNSPECIFIED_CHUNK_SIZE;
73 }
74
75 /**
76 * Gets the policy that is used when the server max changeset size is exceeded.
77 * @return What to do when the changeset size is exceeded
78 */
79 public MaxChangesetSizeExceededPolicy getPolicy() {
80 return policy;
81 }
82
83 /**
84 * Sets the upload strategy (chunk mode)
85 * @param strategy The upload strategy
86 * @return This object, for easy chaining
87 */
88 public UploadStrategySpecification setStrategy(UploadStrategy strategy) {
89 this.strategy = strategy;
90 return this;
91 }
92
93 /**
94 * Sets the upload chunk size
95 * @param chunkSize The chunk size
96 * @return This object, for easy chaining
97 */
98 public UploadStrategySpecification setChunkSize(int chunkSize) {
99 this.chunkSize = chunkSize;
100 return this;
101 }
102
103 /**
104 * Sets the policy to use when the max changeset size is exceeded
105 * @param policy The policy
106 * @return This object, for easy chaining
107 */
108 public UploadStrategySpecification setPolicy(MaxChangesetSizeExceededPolicy policy) {
109 this.policy = policy;
110 return this;
111 }
112
113 /**
114 * Sets whether to close the changeset after this upload
115 * @param closeChangesetAfterUpload <code>true</code> to close it
116 * @return This object, for easy chaining
117 */
118 public UploadStrategySpecification setCloseChangesetAfterUpload(boolean closeChangesetAfterUpload) {
119 this.closeChangesetAfterUpload = closeChangesetAfterUpload;
120 return this;
121 }
122
123 /**
124 * Gets if the changeset should be closed after this upload
125 * @return <code>true</code> to close it
126 */
127 public boolean isCloseChangesetAfterUpload() {
128 return closeChangesetAfterUpload;
129 }
130
131 /**
132 * Gets the number of requests that will be required to upload the objects
133 * @param numObjects The number of objects
134 * @return The number of requests
135 */
136 public int getNumRequests(int numObjects) {
137 if (numObjects <= 0)
138 return 0;
139 switch(strategy) {
140 case INDIVIDUAL_OBJECTS_STRATEGY: return numObjects;
141 case SINGLE_REQUEST_STRATEGY: return 1;
142 case CHUNKED_DATASET_STRATEGY:
143 if (chunkSize == UNSPECIFIED_CHUNK_SIZE)
144 return 0;
145 else
146 return (int) Math.ceil((double) numObjects / (double) chunkSize);
147 }
148 // should not happen
149 return 0;
150 }
151
152 @Override
153 public String toString() {
154 return String.format("Strategy: %s, ChunkSize: %d, Policy: %s, Close after: %b",
155 strategy.toString(), chunkSize, policy == null ? "none" : policy.toString(), closeChangesetAfterUpload);
156 }
157
158 @Override
159 public int hashCode() {
160 return Objects.hash(strategy, chunkSize, policy, closeChangesetAfterUpload);
161 }
162
163 @Override
164 public boolean equals(Object obj) {
165 if (this == obj)
166 return true;
167 if (obj == null || getClass() != obj.getClass())
168 return false;
169 UploadStrategySpecification that = (UploadStrategySpecification) obj;
170 return chunkSize == that.chunkSize &&
171 closeChangesetAfterUpload == that.closeChangesetAfterUpload &&
172 strategy == that.strategy &&
173 policy == that.policy;
174 }
175}
Note: See TracBrowser for help on using the repository browser.