All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.updater.GenericJsonDataSource Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.updater;

import com.fasterxml.jackson.databind.JsonNode;
import java.util.List;
import java.util.Map;
import org.opentripplanner.util.xml.JsonDataListDownloader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class GenericJsonDataSource implements DataSource {

  private static final Logger LOG = LoggerFactory.getLogger(GenericJsonDataSource.class);

  private String url;
  private final JsonDataListDownloader jsonDataListDownloader;

  protected List updates = List.of();

  public GenericJsonDataSource(String url, String jsonParsePath, Map headers) {
    this.url = url;
    jsonDataListDownloader = new JsonDataListDownloader<>(url, jsonParsePath, this::parseElement, headers);
  }

  public GenericJsonDataSource(String url, String jsonParsePath) {
    this(url, jsonParsePath, null);
  }

  protected abstract T parseElement(JsonNode jsonNode);

  @Override
  public boolean update() {
    List updates = jsonDataListDownloader.download();
    if (updates != null) {
      synchronized(this) {
        this.updates = updates;
      }
      return true;
    }
    LOG.info("Can't update entities from: " + url + ", keeping current list.");
    return false;
  }

  @Override
  public List getUpdates() {
    return updates;
  }

  public void setUrl(String url) {
    this.url = url;
    this.jsonDataListDownloader.setUrl(url);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy