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