-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4652 from leonardehrenfried/fallback-zone-id
Use fallback timezone if no transit data is loaded
- Loading branch information
Showing
8 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/org/opentripplanner/framework/time/ZoneIdFallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.opentripplanner.framework.time; | ||
|
||
import java.time.ZoneId; | ||
import javax.annotation.Nullable; | ||
import org.opentripplanner.framework.logging.Throttle; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This class provides a fallback mechanism for retrieving a zone id (=time zone). | ||
* If a ZoneId is not provided, it returns a default fallback ZoneId (UTC). | ||
* <p> | ||
* This situation happens when you don't load any transit data into the graph but want to route | ||
* anyway, perhaps only on the street network. | ||
*/ | ||
public class ZoneIdFallback { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ZoneIdFallback.class); | ||
private static final ZoneId FALLBACK = ZoneId.of("UTC"); | ||
private static final Throttle THROTTLE = Throttle.ofOneMinute(); | ||
|
||
/** | ||
* Accepts a nullable zone id (time zone) and returns UTC as the fallback. | ||
*/ | ||
public static ZoneId zoneId(@Nullable ZoneId id) { | ||
if (id == null) { | ||
THROTTLE.throttle(() -> { | ||
LOG.warn( | ||
"Your instance doesn't contain a time zone (which is usually derived from transit data). Assuming {}.", | ||
FALLBACK | ||
); | ||
LOG.warn("Please double-check that transit data was correctly loaded."); | ||
}); | ||
return FALLBACK; | ||
} else { | ||
return id; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/test/java/org/opentripplanner/framework/time/ZoneIdFallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.opentripplanner.framework.time; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.opentripplanner._support.time.ZoneIds; | ||
|
||
class ZoneIdFallbackTest { | ||
|
||
@Test | ||
void fallback() { | ||
assertEquals(ZoneIds.UTC, ZoneIdFallback.zoneId(null)); | ||
} | ||
|
||
@Test | ||
void keepOriginal() { | ||
assertEquals(ZoneIds.BERLIN, ZoneIdFallback.zoneId(ZoneIds.BERLIN)); | ||
} | ||
} |