001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import java.util.Objects;
005
006/**
007 * An UploadStrategySpecification consists of the parameter describing the strategy
008 * for uploading a collection of {@link org.openstreetmap.josm.data.osm.OsmPrimitive}.
009 *
010 * This includes:
011 * <ul>
012 * <li>a decision on which {@link UploadStrategy} to use</li>
013 * <li>the upload chunk size</li>
014 * <li>whether to close the changeset used after the upload</li>
015 * </ul>
016 */
017public class UploadStrategySpecification  {
018    /** indicates that the chunk size isn't specified */
019    public static final int UNSPECIFIED_CHUNK_SIZE = -1;
020
021    private UploadStrategy strategy;
022    private int chunkSize;
023    private MaxChangesetSizeExceededPolicy policy;
024    private boolean closeChangesetAfterUpload;
025
026    /**
027     * Creates a new upload strategy with default values.
028     */
029    public UploadStrategySpecification() {
030        this.strategy = UploadStrategy.DEFAULT_UPLOAD_STRATEGY;
031        this.chunkSize = UNSPECIFIED_CHUNK_SIZE;
032        this.policy = null;
033        this.closeChangesetAfterUpload = true;
034    }
035
036    /**
037     * Clones another upload strategy. If other is null, assumes default values.
038     *
039     * @param other the other upload strategy
040     */
041    public UploadStrategySpecification(UploadStrategySpecification other) {
042        if (other != null) {
043            this.strategy = other.strategy;
044            this.chunkSize = other.chunkSize;
045            this.policy = other.policy;
046            this.closeChangesetAfterUpload = other.closeChangesetAfterUpload;
047        }
048    }
049
050    /**
051     * Replies the upload strategy
052     * @return the upload strategy
053     */
054    public UploadStrategy getStrategy() {
055        return strategy;
056    }
057
058    public int getChunkSize() {
059        return chunkSize;
060    }
061
062    public static int getUnspecifiedChunkSize() {
063        return UNSPECIFIED_CHUNK_SIZE;
064    }
065
066    public MaxChangesetSizeExceededPolicy getPolicy() {
067        return policy;
068    }
069
070    public UploadStrategySpecification setStrategy(UploadStrategy strategy) {
071        this.strategy = strategy;
072        return this;
073    }
074
075    public UploadStrategySpecification setChunkSize(int chunkSize) {
076        this.chunkSize = chunkSize;
077        return this;
078    }
079
080    public UploadStrategySpecification setPolicy(MaxChangesetSizeExceededPolicy policy) {
081        this.policy = policy;
082        return this;
083    }
084
085    public UploadStrategySpecification setCloseChangesetAfterUpload(boolean closeChangesetAfterUpload) {
086        this.closeChangesetAfterUpload = closeChangesetAfterUpload;
087        return this;
088    }
089
090    public boolean isCloseChangesetAfterUpload() {
091        return closeChangesetAfterUpload;
092    }
093
094    public int getNumRequests(int numObjects) {
095        if (numObjects <= 0)
096            return 0;
097        switch(strategy) {
098        case INDIVIDUAL_OBJECTS_STRATEGY: return numObjects;
099        case SINGLE_REQUEST_STRATEGY: return 1;
100        case CHUNKED_DATASET_STRATEGY:
101            if (chunkSize == UNSPECIFIED_CHUNK_SIZE)
102                return 0;
103            else
104                return (int) Math.ceil((double) numObjects / (double) chunkSize);
105        }
106        // should not happen
107        return 0;
108    }
109
110    @Override
111    public int hashCode() {
112        return Objects.hash(strategy, chunkSize, policy, closeChangesetAfterUpload);
113    }
114
115    @Override
116    public boolean equals(Object obj) {
117        if (this == obj)
118            return true;
119        if (obj == null || getClass() != obj.getClass())
120            return false;
121        UploadStrategySpecification that = (UploadStrategySpecification) obj;
122        return chunkSize == that.chunkSize &&
123                closeChangesetAfterUpload == that.closeChangesetAfterUpload &&
124                strategy == that.strategy &&
125                policy == that.policy;
126    }
127}