Skip to content

Rework logic in addMyItemIdToCoveringItem to support immutable SpecificationItem #540

@redcatbear

Description

@redcatbear

Situation

Currently, the method addMyItemIdToCoveringItem in LinkedSpecificationItem.java attempts to modify the list of covered IDs directly on the underlying SpecificationItem object:

private void addMyItemIdToCoveringItem(final LinkedSpecificationItem coveringItem)
{
    if (coveringItem.getItem().getCoveredIds() != null
            && !coveringItem.getItem().getCoveredIds().contains(getId()))
    {
        coveringItem.getItem().getCoveredIds().add(getId());
    }
}

However, SpecificationItem is designed to be immutable, and its coveredIds list is wrapped in Collections.unmodifiableList during construction:

// In SpecificationItem.java constructor
this.coveredIds = Collections.unmodifiableList(builder.coveredIds);

This leads to an UnsupportedOperationException when addMyItemIdToCoveringItem is called on a real SpecificationItem (unless the list already contains the ID).

Proposed Solution

The logic for establishing bidirectional links should be moved entirely into the LinkedSpecificationItem or the Linker class. LinkedSpecificationItem already maintains its own links map which should be used to track these relationships.

Specifically:

  1. Remove addMyItemIdToCoveringItem from LinkedSpecificationItem.
  2. Ensure that all necessary linking (including back-links) is handled via addLinkToItemWithStatus or a similar mechanism that operates on LinkedSpecificationItem objects rather than modifying the base SpecificationItem model.
  3. Review LinkedSpecificationItem.getCoveredIds() to ensure it correctly aggregates information from both the base item and any links established during the linking phase, if necessary.

Affected Files

  • api/src/main/java/org/itsallcode/openfasttrace/api/core/LinkedSpecificationItem.java
  • api/src/main/java/org/itsallcode/openfasttrace/api/core/SpecificationItem.java (for context)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-assistedWritten with the help of an LLMrefactoringCode improvement without behavior change
    No fields configured for Refactoring.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions