1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * An UploadStrategySpecification consists of the parameter describing the strategy
|
---|
6 | * for uploading a collection of {@see OsmPrimitive}.
|
---|
7 | *
|
---|
8 | * This includes:
|
---|
9 | * <ul>
|
---|
10 | * <li>a decision on which {@see UploadStrategy} to use</li>
|
---|
11 | * <li>the upload chunk size</li>
|
---|
12 | * <li>whether to close the changeset used after the upload</li>
|
---|
13 | * </ul>
|
---|
14 | *
|
---|
15 | *
|
---|
16 | */
|
---|
17 | public class UploadStrategySpecification {
|
---|
18 | /** indicates that the chunk size isn't specified */
|
---|
19 | static public 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
|
---|
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 | final int prime = 31;
|
---|
112 | int result = 1;
|
---|
113 | result = prime * result + chunkSize;
|
---|
114 | result = prime * result + (closeChangesetAfterUpload ? 1231 : 1237);
|
---|
115 | result = prime * result + ((policy == null) ? 0 : policy.hashCode());
|
---|
116 | result = prime * result + ((strategy == null) ? 0 : strategy.hashCode());
|
---|
117 | return result;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public boolean equals(Object obj) {
|
---|
122 | if (this == obj)
|
---|
123 | return true;
|
---|
124 | if (obj == null)
|
---|
125 | return false;
|
---|
126 | if (getClass() != obj.getClass())
|
---|
127 | return false;
|
---|
128 | UploadStrategySpecification other = (UploadStrategySpecification) obj;
|
---|
129 | if (chunkSize != other.chunkSize)
|
---|
130 | return false;
|
---|
131 | if (closeChangesetAfterUpload != other.closeChangesetAfterUpload)
|
---|
132 | return false;
|
---|
133 | if (policy == null) {
|
---|
134 | if (other.policy != null)
|
---|
135 | return false;
|
---|
136 | } else if (!policy.equals(other.policy))
|
---|
137 | return false;
|
---|
138 | if (strategy == null) {
|
---|
139 | if (other.strategy != null)
|
---|
140 | return false;
|
---|
141 | } else if (!strategy.equals(other.strategy))
|
---|
142 | return false;
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 | }
|
---|