Skip to content

Commit

Permalink
WIP - Partial implementation of adjustDateToTimezone. Still need to a…
Browse files Browse the repository at this point in the history
…ddress FIXMEs around implicit timezone and comparison operations.
  • Loading branch information
david-waltermire committed Jan 11, 2025
1 parent 4fbe0af commit 52fd0b7
Show file tree
Hide file tree
Showing 33 changed files with 975 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Provides runtime discovery of built-in implementations of the core Metaschema
* data types.
* Provides runtime discovery of built-in implementations of the core Metaschema data types.
*/
@SuppressWarnings("PMD.CouplingBetweenObjects")
public final class MetaschemaDataTypeProvider // NOPMD - Used for service initialization
Expand All @@ -23,23 +22,23 @@ public final class MetaschemaDataTypeProvider // NOPMD - Used for service initia
@NonNull
public static final HexBinaryAdapter HEX_BINARY = new HexBinaryAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#base64">base64</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#base64">base64</a> data type
* instance.
*/
@NonNull
public static final Base64Adapter BASE64 = new Base64Adapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#boolean">boolean</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#boolean">boolean</a> data
* type instance.
*/
@NonNull
public static final BooleanAdapter BOOLEAN = new BooleanAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#date">date</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#date">date</a> data type
* instance.
*/
@NonNull
public static final DateAdapter DATE = new DateAdapter();
Expand All @@ -51,8 +50,8 @@ public final class MetaschemaDataTypeProvider // NOPMD - Used for service initia
@NonNull
public static final DateWithTZAdapter DATE_WITH_TZ = new DateWithTZAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#date-time">date-time</a>
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#date-time">date-time</a>
* data type instance.
*/
@NonNull
Expand All @@ -66,36 +65,35 @@ public final class MetaschemaDataTypeProvider // NOPMD - Used for service initia
public static final DateTimeWithTZAdapter DATE_TIME_WITH_TZ = new DateTimeWithTZAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#ip-v4-address">ip-v4-address</a>
* data type instance.
* "https://pages.nist.gov/metaschema/specification/datatypes/#ip-v4-address">ip-v4-address</a> data
* type instance.
*/
@NonNull
public static final IPv4AddressAdapter IP_V4_ADDRESS = new IPv4AddressAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#ip-v6-address">ip-v6-address</a>
* data type instance.
* "https://pages.nist.gov/metaschema/specification/datatypes/#ip-v6-address">ip-v6-address</a> data
* type instance.
*/
@NonNull
public static final IPv6AddressAdapter IP_V6_ADDRESS = new IPv6AddressAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#uri">uri</a> data
* type instance.
* The Metaschema <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#uri">uri</a>
* data type instance.
*/
@NonNull
public static final UriAdapter URI = new UriAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#uri-reference">uri-reference</a>
* data type instance.
* "https://pages.nist.gov/metaschema/specification/datatypes/#uri-reference">uri-reference</a> data
* type instance.
*/
@NonNull
public static final UriReferenceAdapter URI_REFERENCE = new UriReferenceAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#uuid">uuid</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#uuid">uuid</a> data type
* instance.
*/
@NonNull
public static final UuidAdapter UUID = new UuidAdapter();
Expand All @@ -114,16 +112,16 @@ public final class MetaschemaDataTypeProvider // NOPMD - Used for service initia
@NonNull
public static final YearMonthAdapter YEAR_MONTH_DURATION = new YearMonthAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#decimal">decimal</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#decimal">decimal</a> data
* type instance.
*/
@NonNull
public static final DecimalAdapter DECIMAL = new DecimalAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#integer">integer</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#integer">integer</a> data
* type instance.
*/
@NonNull
public static final IntegerAdapter INTEGER = new IntegerAdapter();
Expand All @@ -143,30 +141,30 @@ public final class MetaschemaDataTypeProvider // NOPMD - Used for service initia
public static final PositiveIntegerAdapter POSITIVE_INTEGER = new PositiveIntegerAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#email-address">email-address</a>
* data type instance.
* "https://pages.nist.gov/metaschema/specification/datatypes/#email-address">email-address</a> data
* type instance.
*/
@NonNull
public static final EmailAddressAdapter EMAIL_ADDRESS = new EmailAddressAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#hostname">hostname</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#hostname">hostname</a> data
* type instance.
*/
@NonNull
public static final HostnameAdapter HOSTNAME = new HostnameAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#ncname">ncname</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#ncname">ncname</a> data type
* instance.
*/
@Deprecated(since = "0.7.0")
@NonNull
public static final NcNameAdapter NCNAME = new NcNameAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#string">string</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#string">string</a> data type
* instance.
*/
@NonNull
public static final StringAdapter STRING = new StringAdapter();
Expand All @@ -183,15 +181,14 @@ public final class MetaschemaDataTypeProvider // NOPMD - Used for service initia
@NonNull
public static final TimeWithTZAdapter TIME_WITH_TZ = new TimeWithTZAdapter();
/**
* The Metaschema <a href=
* "https://pages.nist.gov/metaschema/specification/datatypes/#token">token</a>
* data type instance.
* The Metaschema
* <a href= "https://pages.nist.gov/metaschema/specification/datatypes/#token">token</a> data type
* instance.
*/
@NonNull
public static final TokenAdapter TOKEN = new TokenAdapter();
/**
* The default Metaschema data type instance to use when no data type is defined
* on a field or flag.
* The default Metaschema data type instance to use when no data type is defined on a field or flag.
*/
@NonNull
public static final StringAdapter DEFAULT_DATA_TYPE = STRING;
Expand Down Expand Up @@ -235,6 +232,5 @@ public MetaschemaDataTypeProvider() {
register(TypeConstants.DURATION_TYPE);
register(TypeConstants.IP_ADDRESS_TYPE);
register(TypeConstants.NUMERIC_TYPE);
register(TypeConstants.TEMPORAL_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.URI;
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -155,8 +156,9 @@ public ZoneId getImplicitTimeZone() {
*/
@NonNull
public IDayTimeDurationItem getImplicitTimeZoneAsDayTimeDuration() {
ZonedDateTime reference = MetapathConstants.REFERENCE_DATE_TIME.atZone(getImplicitTimeZone());
ZonedDateTime referenceZ = MetapathConstants.REFERENCE_DATE_TIME.atZone(ZoneOffset.UTC);
LocalDateTime referenceDateTime = MetapathConstants.REFERENCE_DATE_TIME.asLocalDateTime();
ZonedDateTime reference = referenceDateTime.atZone(getImplicitTimeZone());
ZonedDateTime referenceZ = referenceDateTime.atZone(ZoneOffset.UTC);

return IDayTimeDurationItem.valueOf(ObjectUtils.notNull(
Duration.between(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

package gov.nist.secauto.metaschema.core.metapath;

import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -98,7 +101,14 @@ public final class MetapathConstants {
* A reference date/time value for use in date/time related calculations.
*/
@NonNull
public static final LocalDateTime REFERENCE_DATE_TIME = ObjectUtils.notNull(LocalDateTime.of(1972, 1, 1, 0, 0));
public static final IDateTimeItem REFERENCE_DATE_TIME
= IDateTimeItem.valueOf(ObjectUtils.notNull(LocalDateTime.of(1972, 1, 1, 0, 0)));

/**
* A reference date value for use in date/time related calculations.
*/
@NonNull
public static final IDateItem REFERENCE_DATE_ITEM = IDateItem.valueOf(ObjectUtils.notNull(LocalDate.of(1972, 1, 1)));

private MetapathConstants() {
// disable construction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
/**
* An immutable binary expression that supports basic arithmetic evaluation.
* <p>
* The result type is determined through static analysis of the sub-expressions,
* which may result in a more specific type that is a sub-class of the base
* result type.
* The result type is determined through static analysis of the sub-expressions, which may result in
* a more specific type that is a sub-class of the base result type.
* <p>
* The arithmetic operation method
* {@link #operation(IAnyAtomicItem, IAnyAtomicItem)} must be implemented by
* extending classes to provide the evaluation logic.
* The arithmetic operation method {@link #operation(IAnyAtomicItem, IAnyAtomicItem)} must be
* implemented by extending classes to provide the evaluation logic.
*/
public abstract class AbstractBasicArithmeticExpression
extends AbstractArithmeticExpression<IAnyAtomicItem> {
Expand Down Expand Up @@ -58,7 +56,7 @@ protected ISequence<? extends IAnyAtomicItem> evaluate(DynamicContext dynamicCon
IAnyAtomicItem leftItem = ISequence.of(getLeft().accept(dynamicContext, focus).atomize()).getFirstItem(true);
IAnyAtomicItem rightItem = ISequence.of(getRight().accept(dynamicContext, focus).atomize()).getFirstItem(true);

return resultOrEmpty(leftItem, rightItem);
return resultOrEmpty(leftItem, rightItem, dynamicContext);
}

/**
Expand All @@ -68,18 +66,18 @@ protected ISequence<? extends IAnyAtomicItem> evaluate(DynamicContext dynamicCon
* the first item
* @param rightItem
* the second item
* @return the result of the operation or an empty {@link ISequence} if either
* item is {@code null}
* @return the result of the operation or an empty {@link ISequence} if either item is {@code null}
*/
@NonNull
protected ISequence<? extends IAnyAtomicItem> resultOrEmpty(
@Nullable IAnyAtomicItem leftItem,
@Nullable IAnyAtomicItem rightItem) {
@Nullable IAnyAtomicItem rightItem,
@NonNull DynamicContext dynamicContext) {
ISequence<? extends IAnyAtomicItem> retval;
if (leftItem == null || rightItem == null) {
retval = ISequence.empty();
} else {
IAnyAtomicItem result = operation(leftItem, rightItem);
IAnyAtomicItem result = operation(leftItem, rightItem, dynamicContext);
retval = ISequence.of(result);
}
return retval;
Expand All @@ -98,7 +96,8 @@ protected ISequence<? extends IAnyAtomicItem> resultOrEmpty(
@NonNull
protected IAnyAtomicItem operation(
@NonNull IAnyAtomicItem left,
@NonNull IAnyAtomicItem right) {
@NonNull IAnyAtomicItem right,
@NonNull DynamicContext dynamicContext) {

Map<
Class<? extends IAnyAtomicItem>,
Expand Down Expand Up @@ -127,7 +126,7 @@ protected IAnyAtomicItem operation(
for (Map.Entry<Class<? extends IAnyAtomicItem>, OperationStrategy> entry : typeStrategies.entrySet()) {
if (entry.getKey().isAssignableFrom(rightClass)) {
// this is matching strategy, execute it
return entry.getValue().execute(left, right);
return entry.getValue().execute(left, right, dynamicContext);
}
}

Expand All @@ -137,11 +136,10 @@ protected IAnyAtomicItem operation(
}

/**
* Provides a mapping of the left class to a mapping of the right class and the
* strategy to use to compute the operation.
* Provides a mapping of the left class to a mapping of the right class and the strategy to use to
* compute the operation.
* <p>
* This mapping is used to lookup the strategy to use based on the left and
* right classes.
* This mapping is used to lookup the strategy to use based on the left and right classes.
*
* @return the mapping
*/
Expand Down Expand Up @@ -186,9 +184,14 @@ protected interface OperationStrategy {
* the left side of the arithmetic operation
* @param right
* the right side of the arithmetic operation
* @param dynamicContext
* the evaluation dynamic context
* @return the arithmetic result
*/
@NonNull
IAnyAtomicItem execute(@NonNull IAnyAtomicItem left, @NonNull IAnyAtomicItem right);
IAnyAtomicItem execute(
@NonNull IAnyAtomicItem left,
@NonNull IAnyAtomicItem right,
@NonNull DynamicContext dynamicContext);
}
}
Loading

0 comments on commit 52fd0b7

Please sign in to comment.