001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside.model;
003
004import java.io.IOException;
005import java.net.URL;
006import java.util.function.Function;
007
008import javax.imageio.ImageIO;
009import javax.swing.ImageIcon;
010
011import org.openstreetmap.josm.data.coor.LatLon;
012import org.openstreetmap.josm.plugins.streetside.cache.Caches.MapObjectIconCache;
013import org.openstreetmap.josm.plugins.streetside.utils.StreetsideURL.MainWebsite;
014import org.openstreetmap.josm.tools.ImageProvider;
015import org.openstreetmap.josm.tools.Logging;
016
017public class MapObject extends KeyIndexedObject {
018  private static final ImageIcon ICON_UNKNOWN_TYPE = ImageProvider.get("unknown-mapobject-type");
019  //private static Function<String, URL> iconUrlGen = MainWebsite::mapObjectIcon;
020
021  private final LatLon coordinate;
022  private final String objPackage;
023  private final String value;
024  private final long firstSeenTime;
025  private final long lastSeenTime;
026  private final long updatedTime;
027
028  public MapObject(
029    final LatLon coordinate, final String key, final String objPackage, final String value,
030    long firstSeenTime, long lastSeenTime, long updatedTime
031  ) {
032    super(key);
033    if (objPackage == null || value == null || coordinate == null) {
034      throw new IllegalArgumentException("The fields of a MapObject must not be null!");
035    }
036    this.coordinate = coordinate;
037    this.objPackage = objPackage;
038    this.value = value;
039    this.firstSeenTime = firstSeenTime;
040    this.lastSeenTime = lastSeenTime;
041    this.updatedTime = updatedTime;
042  }
043
044  public LatLon getCoordinate() {
045    return coordinate;
046  }
047
048  /**
049   * @param objectTypeID the {@link String} representing the type of map object. This ID can be retrieved via
050   *   {@link #getValue()} for any given {@link MapObject}.
051   * @return the icon, which represents the given objectTypeID
052   */
053  public static ImageIcon getIcon(final String objectTypeID) {
054    final ImageIcon cachedIcon = MapObjectIconCache.getInstance().get(objectTypeID);
055    if ("not-in-set".equals(objectTypeID)) {
056      return ICON_UNKNOWN_TYPE;
057    } else if (cachedIcon == null) {
058      /*try {
059        final ImageIcon downloadedIcon = new ImageIcon(ImageIO.read(iconUrlGen.apply(objectTypeID)));
060        MapObjectIconCache.getInstance().put(objectTypeID, downloadedIcon);
061        return downloadedIcon;
062      } catch (IOException e) {
063        Logging.log(Logging.LEVEL_WARN, "Failed to download icon " + objectTypeID, e);
064        return ICON_UNKNOWN_TYPE;
065      }*/
066    }
067    return cachedIcon;
068  }
069
070  public String getPackage() {
071    return objPackage;
072  }
073
074  public long getFirstSeenTime() {
075    return firstSeenTime;
076  }
077
078  public long getLastSeenTime() {
079    return lastSeenTime;
080  }
081
082  public long getUpdatedTime() {
083    return updatedTime;
084  }
085
086  public String getValue() {
087    return value;
088  }
089}