Skip to content

Commit

Permalink
Move calendar initialization to server startup
Browse files Browse the repository at this point in the history
  • Loading branch information
devinrsmith committed Jan 16, 2024
1 parent 76b96ce commit 4ab02c7
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending
*/
package io.deephaven.integrations.python;

import io.deephaven.time.calendar.BusinessCalendar;
import io.deephaven.time.calendar.Calendars;

public final class CalendarsHelper {
public static void addCalendarsFromConfiguration() {
for (BusinessCalendar calendar : Calendars.calendarsFromConfiguration()) {
Calendars.addCalendar(calendar);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending
*/
package io.deephaven.plot.axistransformations;

import io.deephaven.time.calendar.BusinessCalendar;
import io.deephaven.time.calendar.Calendars;

final class CalendarInit {
static {
for (BusinessCalendar calendar : Calendars.calendarsFromConfiguration()) {
Calendars.addCalendar(calendar);
}
}

static void noop() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
*/
package io.deephaven.plot.axistransformations;

import io.deephaven.base.testing.BaseArrayTestCase;
import io.deephaven.time.DateTimeUtils;
import io.deephaven.time.calendar.Calendars;
import junit.framework.TestCase;

import java.nio.file.Paths;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Objects;

public class TestAxisTransformBusinessCalendar extends BaseArrayTestCase {
public class TestAxisTransformBusinessCalendar extends TestCase {
static {
CalendarInit.noop();
}

private static final ZoneId TZ_JP = ZoneId.of("Asia/Tokyo");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import static org.junit.Assert.assertTrue;

public class TestAxisTransforms {
static {
CalendarInit.noop();
}

final double d1 = 3.5;
final double d2 = 4.2;
Expand Down
1 change: 1 addition & 0 deletions engine/time/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation project(':Configuration')
implementation project(':log-factory')
implementation depJdom2
Classpaths.inheritDagger(project)

testImplementation TestTools.projectDependency(project, 'Base')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private YearData getYearData(final int year) {
* is different from the schedule for a standard business day or weekend.
* @throws RequirementFailure if any argument is null.
*/
BusinessCalendar(final String name, final String description, final ZoneId timeZone,
public BusinessCalendar(final String name, final String description, final ZoneId timeZone,
final LocalDate firstValidDate, final LocalDate lastValidDate,
final CalendarDay<LocalTime> standardBusinessDay, final Set<DayOfWeek> weekendDays,
final Map<LocalDate, CalendarDay<Instant>> holidays) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import org.jdom2.input.SAXBuilder;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.*;
import java.util.HashSet;
import java.util.List;
Expand All @@ -25,6 +27,7 @@
/**
* A parser for reading business calendar XML files.
*
* <p>
* Business calendar XML files should be formatted as:
*
* <pre>
Expand Down Expand Up @@ -54,7 +57,7 @@
* }
* </pre>
*/
class BusinessCalendarXMLParser {
public final class BusinessCalendarXMLParser {

private static class BusinessCalendarInputs {
private String calendarName;
Expand Down Expand Up @@ -89,38 +92,78 @@ public static BusinessCalendar loadBusinessCalendar(@NotNull final String file)
public static BusinessCalendar loadBusinessCalendar(@NotNull final File file) {
Require.neqNull(file, "file");
final BusinessCalendarInputs in = parseBusinessCalendarInputs(file);
return new BusinessCalendar(in.calendarName, in.description,
in.timeZone, in.firstValidDate, in.lastValidDate,
in.standardBusinessDay, in.weekendDays, in.holidays);
}

/**
* Loads a business calendar from an XML input stream.
*
* @param inputStream XML input stream
* @return business calendar.
* @throws RequirementFailure if the input is null
*/
public static BusinessCalendar loadBusinessCalendar(@NotNull final InputStream inputStream) {
Require.neqNull(inputStream, "inputStream");
final BusinessCalendarInputs in = parseBusinessCalendarInputs(inputStream);
return new BusinessCalendar(in.calendarName, in.description,
in.timeZone, in.firstValidDate, in.lastValidDate,
in.standardBusinessDay, in.weekendDays, in.holidays);
}

/**
* Loads a business calendar from an XML resource.
*
* @param resource XML input stream
* @return business calendar.
*/
public static BusinessCalendar loadBusinessCalendarFromResource(String resource) throws IOException {
final InputStream in = Calendars.class.getResourceAsStream(resource);
if (in == null) {
throw new RuntimeException("Could not open resource " + resource + " from classpath");
}
try (final InputStream bin = new BufferedInputStream(in)) {
return BusinessCalendarXMLParser.loadBusinessCalendar(bin);
}
}

private static BusinessCalendarInputs parseBusinessCalendarInputs(@NotNull final File file) {
Require.neqNull(file, "file");
try {
final BusinessCalendarInputs calendarElements = new BusinessCalendarInputs();

Element root = loadXMLRootElement(file);
calendarElements.calendarName = getText(getRequiredChild(root, "name"));
calendarElements.timeZone = TimeZoneAliases.zoneId(getText(getRequiredChild(root, "timeZone")));
calendarElements.description = getText(getRequiredChild(root, "description"));
calendarElements.firstValidDate =
DateTimeUtils.parseLocalDate(getText(getRequiredChild(root, "firstValidDate")));
calendarElements.lastValidDate =
DateTimeUtils.parseLocalDate(getText(getRequiredChild(root, "lastValidDate")));
calendarElements.holidays = parseHolidays(root, calendarElements.timeZone);

// Set the default values
final Element defaultElement = getRequiredChild(root, "default");
calendarElements.weekendDays = parseWeekendDays(defaultElement);
calendarElements.standardBusinessDay = parseCalendarDaySchedule(defaultElement);

return calendarElements;
return fill(loadXMLRootElement(file));
} catch (Exception e) {
throw new RuntimeException("Unable to load calendar file: file=" + file.getPath(), e);
}
}

private static BusinessCalendarInputs parseBusinessCalendarInputs(@NotNull final InputStream in) {
Require.neqNull(in, "in");
try {
return fill(loadXMLRootElement(in));
} catch (Exception e) {
throw new RuntimeException("Unable to load calendar file: inputStream=" + in, e);
}
}

private static BusinessCalendarInputs fill(Element root) throws Exception {
final BusinessCalendarInputs calendarElements = new BusinessCalendarInputs();
calendarElements.calendarName = getText(getRequiredChild(root, "name"));
calendarElements.timeZone = TimeZoneAliases.zoneId(getText(getRequiredChild(root, "timeZone")));
calendarElements.description = getText(getRequiredChild(root, "description"));
calendarElements.firstValidDate =
DateTimeUtils.parseLocalDate(getText(getRequiredChild(root, "firstValidDate")));
calendarElements.lastValidDate =
DateTimeUtils.parseLocalDate(getText(getRequiredChild(root, "lastValidDate")));
calendarElements.holidays = parseHolidays(root, calendarElements.timeZone);

// Set the default values
final Element defaultElement = getRequiredChild(root, "default");
calendarElements.weekendDays = parseWeekendDays(defaultElement);
calendarElements.standardBusinessDay = parseCalendarDaySchedule(defaultElement);
return calendarElements;
}

private static Element loadXMLRootElement(File calendarFile) throws Exception {
final Document doc;

Expand All @@ -136,6 +179,21 @@ private static Element loadXMLRootElement(File calendarFile) throws Exception {
return doc.getRootElement();
}

private static Element loadXMLRootElement(InputStream in) throws Exception {
final Document doc;

try {
final SAXBuilder builder = new SAXBuilder();
doc = builder.build(in);
} catch (JDOMException e) {
throw new Exception("Error parsing business calendar: inputStream=" + in, e);
} catch (IOException e) {
throw new Exception("Error loading business calendar: inputStream=" + in, e);
}

return doc.getRootElement();
}

private static Element getRequiredChild(@NotNull final Element root, final String child) throws Exception {
Element element = root.getChild(child);
if (element != null) {
Expand Down
Loading

0 comments on commit 4ab02c7

Please sign in to comment.