Skip to content

Commit

Permalink
Merge pull request #350 from entur/entur-validity-filter-for-stop-places
Browse files Browse the repository at this point in the history
Entur sandbox: Check that stop is currently valid
  • Loading branch information
testower authored Jun 27, 2024
2 parents e278d4a + eecbfe3 commit 0e1e1d7
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import no.entur.uttu.stopplace.spi.StopPlaceRegistry;
import org.rutebanken.netex.model.EntityInVersionStructure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -111,10 +114,24 @@ public Optional<org.rutebanken.netex.model.StopPlace> getStopPlaceByQuayRef(
String quayRef
) {
try {
return Optional.of(stopPlaceByQuayRefCache.get(quayRef));
return Optional
.of(stopPlaceByQuayRefCache.get(quayRef))
.filter(this::currentValidityFilter);
} catch (ExecutionException e) {
logger.warn("Failed to get stop place by quay ref ${}", quayRef);
return Optional.empty();
}
}

public boolean currentValidityFilter(EntityInVersionStructure entity) {
return entity
.getValidBetween()
.stream()
.filter(validBetween -> validBetween.getToDate() != null)
.noneMatch(validBetween ->
Instant
.now()
.isAfter(validBetween.getToDate().atZone(ZoneId.systemDefault()).toInstant())
);
}
}

0 comments on commit 0e1e1d7

Please sign in to comment.