001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.coor;
003
004import org.openstreetmap.josm.Main;
005import org.openstreetmap.josm.data.projection.Projection;
006
007/**
008 * LatLon class that maintains a cache of projected EastNorth coordinates.
009 *
010 * This class is convenient to use, but has relatively high memory costs.
011 * It keeps a pointer to the last known projection in order to detect projection changes.
012 *
013 * Node and WayPoint have another, optimized, cache for projected coordinates.
014 */
015public class CachedLatLon extends LatLon {
016
017    private static final long serialVersionUID = 1L;
018
019    private EastNorth eastNorth;
020    private transient Projection proj;
021
022    /**
023     * Constructs a new {@code CachedLatLon}.
024     * @param lat latitude
025     * @param lon longitude
026     */
027    public CachedLatLon(double lat, double lon) {
028        super(lat, lon);
029    }
030
031    /**
032     * Constructs a new {@code CachedLatLon}.
033     * @param coor lat/lon
034     */
035    public CachedLatLon(LatLon coor) {
036        super(coor.lat(), coor.lon());
037        proj = null;
038    }
039
040    /**
041     * Constructs a new {@code CachedLatLon}.
042     * @param eastNorth easting/northing
043     */
044    public CachedLatLon(EastNorth eastNorth) {
045        super(Main.getProjection().eastNorth2latlon(eastNorth));
046        proj = Main.getProjection();
047        this.eastNorth = eastNorth;
048    }
049
050    /**
051     * Replies the projected east/north coordinates.
052     *
053     * @return the internally cached east/north coordinates. null, if the globally defined projection is null
054     */
055    public final EastNorth getEastNorth() {
056        if (proj != Main.getProjection()) {
057            proj = Main.getProjection();
058            eastNorth = proj.latlon2eastNorth(this);
059        }
060        return eastNorth;
061    }
062
063    @Override
064    public String toString() {
065        return "CachedLatLon[lat="+lat()+",lon="+lon()+']';
066    }
067}