Exemplo n.º 1
0
 public Observable<Optional<Station>> getStationByGaugeId(final String gaugeId) {
   if (!stationCache.getStations().isPresent()) {
     return getStations()
         .flatMap(
             new Func1<Collection<Station>, Observable<Optional<Station>>>() {
               @Override
               public Observable<Optional<Station>> call(Collection<Station> stations) {
                 return Observable.just(
                     Optional.of(stationCache.getStations().get().get(gaugeId)));
               }
             });
   } else {
     return Observable.just(Optional.of(stationCache.getStations().get().get(gaugeId)));
   }
 }
Exemplo n.º 2
0
  public Observable<List<Station>> getStations() {
    if (stationCache.getStations().isPresent()) {
      return Observable.just(sortStations(stationCache.getStations().get().values()));

    } else {
      return Observable.defer(
          new Func0<Observable<List<Station>>>() {
            @Override
            public Observable<List<Station>> call() {
              // download stations
              Map<String, List<Station.Builder>> builderMap = new HashMap<>();
              String startId = null;
              Data data;
              do {
                // iterate over ODS cursor
                data =
                    dataApi.getObjectsSynchronously(
                        PEGEL_ALARM_SOURCE_ID, startId, QUERY_COUNT, PROPERTY_FILTER_STATION);
                for (JsonNode node : data.getResult()) {
                  Pair<String, Station.Builder> pair = parseStation(node);
                  List<Station.Builder> stationsList = builderMap.get(pair.first);
                  if (stationsList == null) {
                    stationsList = new ArrayList<>();
                    builderMap.put(pair.first, stationsList);
                  }
                  stationsList.add(pair.second);
                }
                startId = data.getCursor().getNext();

              } while (data.getCursor().getHasNext());

              // count stations per water and build stations
              Map<String, Station> stations = new HashMap<>();
              for (String waterName : builderMap.keySet()) {
                BodyOfWater water = new BodyOfWater(waterName, builderMap.get(waterName).size());
                for (Station.Builder builder : builderMap.get(waterName)) {
                  Station station = builder.setBodyOfWater(water).build();
                  stations.put(station.getGaugeId(), station);
                }
              }
              stationCache.setStations(stations);

              return Observable.just(sortStations(stations.values()));
            }
          });
    }
  }