Skip to content

Fix non-idiomatic patterns: raw EventContext, string concat in errors, pre-Java 16 stream collection #32

Description

@Schmarvinius

Summary

Fix three non-idiomatic patterns identified by comparison with cds-services conventions.

Changes

1. ActionHandler: Replace raw context.put("result") with context.setResult()

File: cds-feature-ai-core/.../handler/ActionHandler.java:36-37

// Before
context.put("result", result);
context.setCompleted();

// After
context.setResult(result);  // setResult() calls setCompleted() automatically

In cds-services, setResult() implicitly calls setCompleted(). The explicit context.put("result", ...) + setCompleted() pattern bypasses this and is non-standard.

2. AICoreSetupHandler: Replace string concatenation with {} placeholders

File: cds-feature-ai-core/.../AICoreSetupHandler.java (3 locations)

// Before (line 44)
throw new ServiceException(ErrorStatuses.SERVER_ERROR,
    "Failed to create AI Core resources for tenant: " + tenantId, e);

// After
throw new ServiceException(ErrorStatuses.SERVER_ERROR,
    "Failed to create AI Core resources for tenant: {}", tenantId, e);

Same for lines 82 and 106. ServiceException uses MessageFormatter.arrayFormat() internally and detects the last Throwable arg as the cause.

3. AbstractCrudHandler: Replace Collectors.toList() with .toList() and new ArrayList<>() with List.of()

File: cds-feature-ai-core/.../handler/AbstractCrudHandler.java:36-38

// Before
protected static <T, R> List<R> mapResources(List<T> resources, Function<T, R> mapper) {
  if (resources == null) return new ArrayList<>();
  return resources.stream().map(mapper).collect(Collectors.toList());
}

// After
protected static <T, R> List<R> mapResources(List<T> resources, Function<T, R> mapper) {
  if (resources == null) return List.of();
  return resources.stream().map(mapper).toList();
}

Remove unused imports: java.util.ArrayList, java.util.stream.Collectors.

Verification

  • All existing tests should pass unchanged (return values flow into context.setResult() which accepts any Iterable).
  • The returned lists are never mutated by callers (verified: all go to context.setResult()).

Rationale

Aligns with idiomatic patterns observed in cds-services:

  • setResult() is the standard way to complete ON handlers
  • SLF4J-style placeholders enable localization and consistent formatting
  • .toList() is the Java 16+ standard; List.of() signals immutability intent

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