Changeset 14456 in josm for trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java
- Timestamp:
- 2018-11-27T21:40:10+01:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java
r14446 r14456 5 5 import java.util.ArrayList; 6 6 import java.util.Date; 7 import java.util.HashMap; 7 8 import java.util.List; 8 9 import java.util.Objects; … … 14 15 import org.openstreetmap.josm.data.projection.Projecting; 15 16 import org.openstreetmap.josm.tools.Logging; 17 import org.openstreetmap.josm.tools.date.DateUtils; 16 18 import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider; 17 19 … … 23 25 24 26 /** 25 * The seconds (not milliseconds!) since 1970-01-01 00:00 UTC26 */27 public double time;28 /**29 27 * The color to draw the segment before this point in 30 28 * @see #drawLine 31 29 */ 32 30 public Color customColoring; 31 33 32 /** 34 33 * <code>true</code> indicates that the line before this point should be drawn 35 34 */ 36 35 public boolean drawLine; 36 37 37 /** 38 38 * The direction of the line before this point. Used as cache to speed up drawing. Should not be relied on. … … 40 40 public int dir; 41 41 42 /* 43 * We "inline" lat/lon, rather than using a LatLon internally => reduces memory overhead. Relevant 44 * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server. 45 */ 46 private final double lat; 47 private final double lon; 48 49 /* 50 * internal cache of projected coordinates 51 */ 52 private double east = Double.NaN; 53 private double north = Double.NaN; 54 private Object eastNorthCacheKey; 55 42 56 /** 43 57 * Constructs a new {@code WayPoint} from an existing one. 58 * 59 * Except for PT_TIME attribute, all attribute objects are shallow copied. 60 * This means modification of attr objects will affect original and new {@code WayPoint}. 61 * 44 62 * @param p existing waypoint 45 63 */ 46 64 public WayPoint(WayPoint p) { 65 init_attr(); 47 66 attr.putAll(p.attr); 67 attr.put(PT_TIME, p.getDate()); 48 68 lat = p.lat; 49 69 lon = p.lon; … … 51 71 north = p.north; 52 72 eastNorthCacheKey = p.eastNorthCacheKey; 53 time = p.time;54 73 customColoring = p.customColoring; 55 74 drawLine = p.drawLine; … … 62 81 */ 63 82 public WayPoint(LatLon ll) { 83 init_attr(); 64 84 lat = ll.lat(); 65 85 lon = ll.lon(); 66 86 } 67 87 68 /* 69 * We "inline" lat/lon, rather than usinga LatLon internally => reduces memory overhead. Relevant 70 * because a lot of GPX waypoints are created when GPS tracks are downloaded from the OSM server. 71 */ 72 private final double lat; 73 private final double lon; 74 75 /* 76 * internal cache of projected coordinates 77 */ 78 private double east = Double.NaN; 79 private double north = Double.NaN; 80 private Object eastNorthCacheKey; 88 /** 89 * Interim to detect legacy code that is not using {@code WayPoint.setTime(x)} 90 * functions, but {@code attr.put(PT_TIME, (String) x)} logic. 91 * To remove mid 2019 92 */ 93 private void init_attr() { 94 attr = new HashMap<String, Object>(0) { 95 @Override 96 public Object put(String key, Object value) { 97 Object ret = null; 98 if (key != PT_TIME || (key == PT_TIME && value instanceof Date)) { 99 ret = super.put(key, value); 100 } else { 101 if (value instanceof String) { 102 ret = super.put(PT_TIME, DateUtils.fromString((String) value)); 103 List<String> lastErrorAndWarnings = Logging.getLastErrorAndWarnings(); 104 if (!lastErrorAndWarnings.isEmpty() && !lastErrorAndWarnings.get(0).contains("calling WayPoint.put")) { 105 StackTraceElement[] e = Thread.currentThread().getStackTrace(); 106 int n = 1; 107 while (n < e.length && "put".equals(e[n].getMethodName())) { 108 n++; 109 } 110 if (n < e.length) { 111 Logging.warn("{0}:{1} calling WayPoint.put(PT_TIME, ..) is deprecated. " + 112 "Use WayPoint.setTime(..) instead.", e[n].getClassName(), e[n].getMethodName()); 113 } 114 } 115 } 116 } 117 return ret; 118 } 119 }; 120 } 81 121 82 122 /** … … 126 166 127 167 /** 128 * Sets the {@link # time} field as well as the {@link #PT_TIME} attribute to the specified time.168 * Sets the {@link #PT_TIME} attribute to the specified time. 129 169 * 130 170 * @param time the time to set … … 132 172 */ 133 173 public void setTime(Date time) { 134 this.time = time.getTime() / 1000.; 135 this.attr.put(PT_TIME, time); 174 setTimeInMillis(time.getTime()); 136 175 } 137 176 … … 139 178 * Convert the time stamp of the waypoint into seconds from the epoch. 140 179 * 141 * @deprecated call {@link #setTimeFromAttribute()} directly if you need this180 * @deprecated Use {@link #setTime(Date)}, {@link #setTime(long)}, {@link #setTimeInMillis(long)} 142 181 */ 143 182 @Deprecated … … 147 186 148 187 /** 149 * Sets the {@link # time} field as well as the {@link #PT_TIME} attribute to the specified time.188 * Sets the {@link #PT_TIME} attribute to the specified time. 150 189 * 151 190 * @param ts seconds from the epoch … … 153 192 */ 154 193 public void setTime(long ts) { 155 setTimeInMillis(ts *1000);156 } 157 158 /** 159 * Sets the {@link # time} field as well as the {@link #PT_TIME} attribute to the specified time.194 setTimeInMillis(ts * 1000); 195 } 196 197 /** 198 * Sets the {@link #PT_TIME} attribute to the specified time. 160 199 * 161 200 * @param ts milliseconds from the epoch … … 163 202 */ 164 203 public void setTimeInMillis(long ts) { 165 this.time = ts / 1000.; 166 this.attr.put(PT_TIME, new Date(ts)); 167 } 168 169 /** 170 * Convert the time stamp of the waypoint into seconds from the epoch 204 attr.put(PT_TIME, new Date(ts)); 205 } 206 207 /** 208 * Convert the time stamp of the waypoint into seconds from the epoch. 171 209 * @return The parsed time if successful, or {@code null} 172 210 * @since 9383 173 */ 211 * @deprecated Use {@link #setTime(Date)}, {@link #setTime(long)}, {@link #setTimeInMillis(long)} 212 */ 213 @Deprecated 174 214 public Date setTimeFromAttribute() { 175 if (attr.containsKey(PT_TIME)) { 176 final Object obj = get(PT_TIME); 215 Logging.warn("WayPoint.setTimeFromAttribute() is deprecated, please fix calling code"); 216 return getDate(); 217 } 218 219 @Override 220 public int compareTo(WayPoint w) { 221 return Long.compare(getTimeInMillis(), w.getTimeInMillis()); 222 } 223 224 /** 225 * Returns the waypoint time in seconds since the epoch. 226 * 227 * @return the waypoint time 228 */ 229 public double getTime() { 230 return getTimeInMillis() / 1000.; 231 } 232 233 /** 234 * Returns the waypoint time in milliseconds since the epoch. 235 * 236 * @return the waypoint time 237 * @since 14456 238 */ 239 public long getTimeInMillis() { 240 Date d = getDateImpl(); 241 return d == null ? 0 : d.getTime(); 242 } 243 244 /** 245 * Returns true if this waypoint has a time. 246 * 247 * @return true if a time is set, false otherwise 248 * @since 14456 249 */ 250 public boolean hasDate() { 251 return attr.get(PT_TIME) instanceof Date; 252 } 253 254 /** 255 * Returns the waypoint time Date object. 256 * 257 * @return a copy of the Date object associated with this waypoint 258 * @since 14456 259 */ 260 public Date getDate() { 261 return DateUtils.cloneDate(getDateImpl()); 262 } 263 264 /** 265 * Returns the waypoint time Date object. 266 * 267 * @return the Date object associated with this waypoint 268 */ 269 private Date getDateImpl() { 270 if (attr != null) { 271 final Object obj = attr.get(PT_TIME); 272 177 273 if (obj instanceof Date) { 178 final Date date = (Date) obj; 179 time = date.getTime() / 1000.; 180 return date; 274 return (Date) obj; 181 275 } else if (obj == null) { 182 276 Logging.info("Waypoint {0} value unset", PT_TIME); 183 277 } else { 184 278 Logging.warn("Unsupported waypoint {0} value: {1}", PT_TIME, obj); 185 time = 0;186 279 } 187 280 } 281 188 282 return null; 189 }190 191 @Override192 public int compareTo(WayPoint w) {193 return Double.compare(time, w.time);194 }195 196 /**197 * Returns the waypoint time.198 * @return the waypoint time199 */200 public Date getTime() {201 return new Date((long) (time * 1000));202 283 } 203 284 … … 228 309 temp = Double.doubleToLongBits(lon); 229 310 result = prime * result + (int) (temp ^ (temp >>> 32)); 230 temp = Double.doubleToLongBits(time);311 temp = getTimeInMillis(); 231 312 result = prime * result + (int) (temp ^ (temp >>> 32)); 232 313 return result; … … 242 323 return Double.doubleToLongBits(lat) == Double.doubleToLongBits(other.lat) 243 324 && Double.doubleToLongBits(lon) == Double.doubleToLongBits(other.lon) 244 && Double.doubleToLongBits(time) == Double.doubleToLongBits(other.time);325 && getTimeInMillis() == other.getTimeInMillis(); 245 326 } 246 327 }
Note:
See TracChangeset
for help on using the changeset viewer.