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
|
---|
38 | * values.
|
---|
39 | *
|
---|
40 | * @param other the other upload strategy
|
---|
41 | */
|
---|
42 | public UploadStrategySpecification(UploadStrategySpecification other) {
|
---|
43 | if (other == null) return;
|
---|
44 | this.strategy = other.strategy;
|
---|
45 | this.chunkSize = other.chunkSize;
|
---|
46 | this.policy = other.policy;
|
---|
47 | this.closeChangesetAfterUpload = other.closeChangesetAfterUpload;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Replies the upload strategy
|
---|
52 | * @return the upload strategy
|
---|
53 | */
|
---|
54 | public UploadStrategy getStrategy() {
|
---|
55 | return strategy;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public int getChunkSize() {
|
---|
59 | return chunkSize;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static int getUnspecifiedChunkSize() {
|
---|
63 | return UNSPECIFIED_CHUNK_SIZE;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public MaxChangesetSizeExceededPolicy getPolicy() {
|
---|
67 | return policy;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public UploadStrategySpecification setStrategy(UploadStrategy strategy) {
|
---|
71 | this.strategy = strategy;
|
---|
72 | return this;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public UploadStrategySpecification setChunkSize(int chunkSize) {
|
---|
76 | this.chunkSize = chunkSize;
|
---|
77 | return this;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public UploadStrategySpecification setPolicy(MaxChangesetSizeExceededPolicy policy) {
|
---|
81 | this.policy = policy;
|
---|
82 | return this;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public UploadStrategySpecification setCloseChangesetAfterUpload(boolean closeChangesetAfterUpload) {
|
---|
86 | this.closeChangesetAfterUpload = closeChangesetAfterUpload;
|
---|
87 | return this;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public boolean isCloseChangesetAfterUpload() {
|
---|
91 | return closeChangesetAfterUpload;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public int getNumRequests(int numObjects) {
|
---|
95 | if (numObjects <= 0) return 0;
|
---|
96 | switch(strategy) {
|
---|
97 | case INDIVIDUAL_OBJECTS_STRATEGY: return numObjects;
|
---|
98 | case SINGLE_REQUEST_STRATEGY: return 1;
|
---|
99 | case CHUNKED_DATASET_STRATEGY:
|
---|
100 | if (chunkSize == UNSPECIFIED_CHUNK_SIZE)
|
---|
101 | return 0;
|
---|
102 | else
|
---|
103 | return (int) Math.ceil((double) numObjects / (double) chunkSize);
|
---|
104 | }
|
---|
105 | // should not happen
|
---|
106 | return 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public int hashCode() {
|
---|
111 | return Objects.hash(strategy, chunkSize, policy, closeChangesetAfterUpload);
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public boolean equals(Object obj) {
|
---|
116 | if (this == obj) return true;
|
---|
117 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
118 | UploadStrategySpecification that = (UploadStrategySpecification) obj;
|
---|
119 | return chunkSize == that.chunkSize &&
|
---|
120 | closeChangesetAfterUpload == that.closeChangesetAfterUpload &&
|
---|
121 | strategy == that.strategy &&
|
---|
122 | policy == that.policy;
|
---|
123 | }
|
---|
124 | }
|
---|