1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * An UploadStrategySpecification consists of the parameter describing the strategy
|
---|
8 | * for uploading a collection of {@see OsmPrimitive}.
|
---|
9 | *
|
---|
10 | * This includes:
|
---|
11 | * <ul>
|
---|
12 | * <li>a decision on which {@see 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 | *
|
---|
18 | */
|
---|
19 | public class UploadStrategySpecification {
|
---|
20 | /** indicates that the chunk size isn't specified */
|
---|
21 | static public final int UNSPECIFIED_CHUNK_SIZE = -1;
|
---|
22 |
|
---|
23 | private UploadStrategy strategy;
|
---|
24 | private int chunkSize;
|
---|
25 | private MaxChangesetSizeExceededPolicy policy;
|
---|
26 | private boolean closeChangesetAfterUpload;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Creates a new upload strategy with default values.
|
---|
30 | */
|
---|
31 | public UploadStrategySpecification() {
|
---|
32 | this.strategy = UploadStrategy.DEFAULT_UPLOAD_STRATEGY;
|
---|
33 | this.chunkSize = UNSPECIFIED_CHUNK_SIZE;
|
---|
34 | this.policy = null;
|
---|
35 | this.closeChangesetAfterUpload = true;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Clones another upload strategy. If other is null,assumes default
|
---|
40 | * values.
|
---|
41 | *
|
---|
42 | * @param other the other upload strategy
|
---|
43 | */
|
---|
44 | public UploadStrategySpecification(UploadStrategySpecification other) {
|
---|
45 | if (other == null) return;
|
---|
46 | this.strategy = other.strategy;
|
---|
47 | this.chunkSize = other.chunkSize;
|
---|
48 | this.policy = other.policy;
|
---|
49 | this.closeChangesetAfterUpload = other.closeChangesetAfterUpload;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Replies the upload strategy
|
---|
54 | * @return
|
---|
55 | */
|
---|
56 | public UploadStrategy getStrategy() {
|
---|
57 | return strategy;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public int getChunkSize() {
|
---|
61 | return chunkSize;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static int getUnspecifiedChunkSize() {
|
---|
65 | return UNSPECIFIED_CHUNK_SIZE;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public MaxChangesetSizeExceededPolicy getPolicy() {
|
---|
69 | return policy;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public UploadStrategySpecification setStrategy(UploadStrategy strategy) {
|
---|
73 | this.strategy = strategy;
|
---|
74 | return this;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public UploadStrategySpecification setChunkSize(int chunkSize) {
|
---|
78 | this.chunkSize = chunkSize;
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public UploadStrategySpecification setPolicy(MaxChangesetSizeExceededPolicy policy) {
|
---|
83 | this.policy = policy;
|
---|
84 | return this;
|
---|
85 | }
|
---|
86 |
|
---|
87 | public UploadStrategySpecification setCloseChangesetAfterUpload(boolean closeChangesetAfterUpload) {
|
---|
88 | this.closeChangesetAfterUpload = closeChangesetAfterUpload;
|
---|
89 | return this;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public boolean isCloseChangesetAfterUpload() {
|
---|
93 | return closeChangesetAfterUpload;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public int getNumRequests(int numObjects) {
|
---|
97 | if (numObjects <=0) return 0;
|
---|
98 | switch(strategy) {
|
---|
99 | case INDIVIDUAL_OBJECTS_STRATEGY: return numObjects;
|
---|
100 | case SINGLE_REQUEST_STRATEGY: return 1;
|
---|
101 | case CHUNKED_DATASET_STRATEGY:
|
---|
102 | if (chunkSize == UNSPECIFIED_CHUNK_SIZE)
|
---|
103 | return 0;
|
---|
104 | else
|
---|
105 | return (int)Math.ceil((double)numObjects / (double)chunkSize);
|
---|
106 | }
|
---|
107 | // should not happen
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public int hashCode() {
|
---|
113 | final int prime = 31;
|
---|
114 | int result = 1;
|
---|
115 | result = prime * result + chunkSize;
|
---|
116 | result = prime * result + (closeChangesetAfterUpload ? 1231 : 1237);
|
---|
117 | result = prime * result + ((policy == null) ? 0 : policy.hashCode());
|
---|
118 | result = prime * result + ((strategy == null) ? 0 : strategy.hashCode());
|
---|
119 | return result;
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public boolean equals(Object obj) {
|
---|
124 | if (this == obj)
|
---|
125 | return true;
|
---|
126 | if (obj == null)
|
---|
127 | return false;
|
---|
128 | if (getClass() != obj.getClass())
|
---|
129 | return false;
|
---|
130 | UploadStrategySpecification other = (UploadStrategySpecification) obj;
|
---|
131 | if (chunkSize != other.chunkSize)
|
---|
132 | return false;
|
---|
133 | if (closeChangesetAfterUpload != other.closeChangesetAfterUpload)
|
---|
134 | return false;
|
---|
135 | if (policy == null) {
|
---|
136 | if (other.policy != null)
|
---|
137 | return false;
|
---|
138 | } else if (!policy.equals(other.policy))
|
---|
139 | return false;
|
---|
140 | if (strategy == null) {
|
---|
141 | if (other.strategy != null)
|
---|
142 | return false;
|
---|
143 | } else if (!strategy.equals(other.strategy))
|
---|
144 | return false;
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 | }
|
---|