001 // License: GPL. For details, see LICENSE file. 002 package org.openstreetmap.josm.gui.io; 003 004 /** 005 * An UploadStrategySpecification consists of the parameter describing the strategy 006 * for uploading a collection of {@link 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 * 015 * 016 */ 017 public class UploadStrategySpecification { 018 /** indicates that the chunk size isn't specified */ 019 static public 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 038 * values. 039 * 040 * @param other the other upload strategy 041 */ 042 public UploadStrategySpecification(UploadStrategySpecification other) { 043 if (other == null) return; 044 this.strategy = other.strategy; 045 this.chunkSize = other.chunkSize; 046 this.policy = other.policy; 047 this.closeChangesetAfterUpload = other.closeChangesetAfterUpload; 048 } 049 050 /** 051 * Replies the upload strategy 052 * @return 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) return 0; 096 switch(strategy) { 097 case INDIVIDUAL_OBJECTS_STRATEGY: return numObjects; 098 case SINGLE_REQUEST_STRATEGY: return 1; 099 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 }