1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.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 | */
|
---|
17 | public class UploadStrategySpecification {
|
---|
18 | /** indicates that the chunk size isn't specified */
|
---|
19 | public static final int UNSPECIFIED_CHUNK_SIZE = -1;
|
---|
20 |
|
---|
21 | private UploadStrategy strategy;
|
---|
22 | private int chunkSize;
|
---|
23 | private MaxChangesetSizeExceededPolicy policy;
|
---|
24 | private boolean closeChangesetAfterUpload;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Creates a new upload strategy with default values.
|
---|
28 | */
|
---|
29 | public UploadStrategySpecification() {
|
---|
30 | this.strategy = UploadStrategy.DEFAULT_UPLOAD_STRATEGY;
|
---|
31 | this.chunkSize = UNSPECIFIED_CHUNK_SIZE;
|
---|
32 | this.policy = null;
|
---|
33 | this.closeChangesetAfterUpload = true;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Clones another upload strategy. If other is null, assumes default values.
|
---|
38 | *
|
---|
39 | * @param other the other upload strategy
|
---|
40 | */
|
---|
41 | public UploadStrategySpecification(UploadStrategySpecification other) {
|
---|
42 | if (other != null) {
|
---|
43 | this.strategy = other.strategy;
|
---|
44 | this.chunkSize = other.chunkSize;
|
---|
45 | this.policy = other.policy;
|
---|
46 | this.closeChangesetAfterUpload = other.closeChangesetAfterUpload;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Replies the upload strategy
|
---|
52 | * @return the upload strategy
|
---|
53 | */
|
---|
54 | public UploadStrategy getStrategy() {
|
---|
55 | return strategy;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Gets the chunk size
|
---|
60 | * @return The max size of each upload chunk
|
---|
61 | */
|
---|
62 | public int getChunkSize() {
|
---|
63 | return chunkSize;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Gets a special value that is used to indicate that the chunk size was not specified
|
---|
68 | * @return A special integer
|
---|
69 | */
|
---|
70 | public static int getUnspecifiedChunkSize() {
|
---|
71 | return UNSPECIFIED_CHUNK_SIZE;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Gets the policy that is used when the server max changeset size is exceeded.
|
---|
76 | * @return What to do when the changeset size is exceeded
|
---|
77 | */
|
---|
78 | public MaxChangesetSizeExceededPolicy getPolicy() {
|
---|
79 | return policy;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Sets the upload strategy (chunk mode)
|
---|
84 | * @param strategy The upload strategy
|
---|
85 | * @return This object, for easy chaining
|
---|
86 | */
|
---|
87 | public UploadStrategySpecification setStrategy(UploadStrategy strategy) {
|
---|
88 | this.strategy = strategy;
|
---|
89 | return this;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Sets the upload chunk size
|
---|
94 | * @param chunkSize The chunk size
|
---|
95 | * @return This object, for easy chaining
|
---|
96 | */
|
---|
97 | public UploadStrategySpecification setChunkSize(int chunkSize) {
|
---|
98 | this.chunkSize = chunkSize;
|
---|
99 | return this;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Sets the policy to use when the max changeset size is exceeded
|
---|
104 | * @param policy The policy
|
---|
105 | * @return This object, for easy chaining
|
---|
106 | */
|
---|
107 | public UploadStrategySpecification setPolicy(MaxChangesetSizeExceededPolicy policy) {
|
---|
108 | this.policy = policy;
|
---|
109 | return this;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Sets whether to close the changeset after this upload
|
---|
114 | * @param closeChangesetAfterUpload <code>true</code> to close it
|
---|
115 | * @return This object, for easy chaining
|
---|
116 | */
|
---|
117 | public UploadStrategySpecification setCloseChangesetAfterUpload(boolean closeChangesetAfterUpload) {
|
---|
118 | this.closeChangesetAfterUpload = closeChangesetAfterUpload;
|
---|
119 | return this;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Gets if the changeset should be closed after this upload
|
---|
124 | * @return <code>true</code> to close it
|
---|
125 | */
|
---|
126 | public boolean isCloseChangesetAfterUpload() {
|
---|
127 | return closeChangesetAfterUpload;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Gets the number of requests that will be required to upload the objects
|
---|
132 | * @param numObjects The number of objects
|
---|
133 | * @return The number of requests
|
---|
134 | */
|
---|
135 | public int getNumRequests(int numObjects) {
|
---|
136 | if (numObjects <= 0)
|
---|
137 | return 0;
|
---|
138 | switch(strategy) {
|
---|
139 | case INDIVIDUAL_OBJECTS_STRATEGY: return numObjects;
|
---|
140 | case SINGLE_REQUEST_STRATEGY: return 1;
|
---|
141 | case CHUNKED_DATASET_STRATEGY:
|
---|
142 | if (chunkSize == UNSPECIFIED_CHUNK_SIZE)
|
---|
143 | return 0;
|
---|
144 | else
|
---|
145 | return (int) Math.ceil((double) numObjects / (double) chunkSize);
|
---|
146 | }
|
---|
147 | // should not happen
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public int hashCode() {
|
---|
153 | return Objects.hash(strategy, chunkSize, policy, closeChangesetAfterUpload);
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | public boolean equals(Object obj) {
|
---|
158 | if (this == obj)
|
---|
159 | return true;
|
---|
160 | if (obj == null || getClass() != obj.getClass())
|
---|
161 | return false;
|
---|
162 | UploadStrategySpecification that = (UploadStrategySpecification) obj;
|
---|
163 | return chunkSize == that.chunkSize &&
|
---|
164 | closeChangesetAfterUpload == that.closeChangesetAfterUpload &&
|
---|
165 | strategy == that.strategy &&
|
---|
166 | policy == that.policy;
|
---|
167 | }
|
---|
168 | }
|
---|