    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.utils.api;
    003
    004
    005import javax.json.JsonObject;
    006import javax.json.JsonValue;
    007
    008import org.openstreetmap.josm.plugins.streetside.StreetsideData;
    009import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
    010import org.openstreetmap.josm.plugins.streetside.utils.StreetsideURL.APIv3;
    011
    012
    013/**
    014 * Decodes the JSON returned by {@link APIv3} into Java objects.
    015 * Takes a {@link JsonObject} and {@link #decodeImageInfos(JsonObject, StreetsideData)} tries to add the timestamps.
    016 */
    017public final class JsonImageDetailsDecoder {
    018  private JsonImageDetailsDecoder() {
    019    // Private constructor to avoid instantiation
    020  }
    021
    022  public static void decodeImageInfos(final JsonObject json, final StreetsideData data) {
    023    if (data != null) {
    024      JsonDecoder.decodeFeatureCollection(json, j -> {
    025        decodeImageInfo(j, data);
    026        return null;
    027      });
    028    }
    029  }
    030
    031  private static void decodeImageInfo(final JsonObject json, final StreetsideData data) {
    032    if (json != null && data != null) {
    033      JsonValue properties = json.get("properties");
    034      if (properties instanceof JsonObject) {
    035        String id = ((JsonObject) properties).getString("id", null);
    036        Long he = JsonDecoder.decodeTimestamp(((JsonObject)properties).getString("he", null));
    037        if (id != null && he != null) {
    038          data.getImages().stream().filter(
    039            img -> img instanceof StreetsideImage && id.equals(((StreetsideImage) img).getId())
    040          ).forEach(img -> img.setHe(he));
    041        }
    042      }
    043    }
    044  }
    045}