Skip to content

Modernize date handling with Java 21 pattern matching #2969

@robfrank

Description

@robfrank

Overview

Modernize date handling code in DateUtils.java and BinaryTypes.java using Java 21 pattern matching switch expressions.

Part of Epic #2968

Goals

  • 43% reduction in cyclomatic complexity
  • 23% code reduction with improved clarity
  • 100% explicit null safety with pattern matching
  • Replace verbose if-else chains with exhaustive switch expressions
  • Migrate from wildcard to explicit imports

Files to Change

DateUtils.java

Location: engine/src/main/java/com/arcadedb/utility/DateUtils.java

Methods to modernize:

  • getNanos(Object obj)
  • getMicros(Object obj)
  • getMillis(Object obj)
  • getSeconds(Object obj)
  • precisionFromDateType(byte type)
  • dateTypeToPrecision(ChronoUnit precision)

BinaryTypes.java

Location: engine/src/main/java/com/arcadedb/serializer/BinaryTypes.java

Methods to modernize:

  • precisionFromDateType(byte type)

Implementation Pattern

// Before: verbose if-else chain
public static int getNanos(final Object obj) {
  if (obj == null)
    throw new IllegalArgumentException("Object is null");
  else if (obj instanceof LocalDateTime time)
    return time.getNano();
  else if (obj instanceof ZonedDateTime time)
    return time.getNano();
  // ...
}

// After: clean pattern-matching switch
public static int getNanos(final Object obj) {
  return switch (obj) {
    case null -> throw new IllegalArgumentException("Object is null");
    case LocalDateTime time -> time.getNano();
    case ZonedDateTime time -> time.getNano();
    case Instant instant -> instant.getNano();
    default -> throw new IllegalArgumentException("Cannot extract nanos from object of type '" + obj.getClass() + "'");
  };
}

Validation

# Compile
mvn compile -pl engine

# Run unit tests
mvn test -pl engine -Dtest=*DateUtils*
mvn test -pl engine -Dtest=*Binary*

# Run integration test that depends on these changes
mvn test -pl server -Dtest=RemoteDateIT

Success Criteria

  • No compilation errors
  • All DateUtils unit tests pass
  • All BinaryTypes tests pass
  • RemoteDateIT passes (after bug fix in separate issue)
  • Code follows Java 21 patterns
  • Explicit imports replace wildcards

Time Estimate

50 minutes total

  • DateUtils.java: 30 minutes
  • BinaryTypes.java: 20 minutes

Risk Level

LOW - Straightforward refactoring with good test coverage

Documentation

See PORTING_PLAN_IT_TEST_IMPROVEMENTS.md - Phase 2 for detailed instructions.

Related Issues

Part of Epic: #2968

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions