1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import 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 | */
|
---|
18 | public 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 int hashCode() {
|
---|
154 | return Objects.hash(strategy, chunkSize, policy, closeChangesetAfterUpload);
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Override
|
---|
158 | public boolean equals(Object obj) {
|
---|
159 | if (this == obj)
|
---|
160 | return true;
|
---|
161 | if (obj == null || getClass() != obj.getClass())
|
---|
162 | return false;
|
---|
163 | UploadStrategySpecification that = (UploadStrategySpecification) obj;
|
---|
164 | return chunkSize == that.chunkSize &&
|
---|
165 | closeChangesetAfterUpload == that.closeChangesetAfterUpload &&
|
---|
166 | strategy == that.strategy &&
|
---|
167 | policy == that.policy;
|
---|
168 | }
|
---|
169 | }
|
---|