You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by "Andreas (Jira)" <ji...@apache.org> on 2023/10/20 09:55:00 UTC
[jira] [Created] (WICKET-7082) Easier to work with polymorphic values inside IModel
Andreas created WICKET-7082:
-------------------------------
Summary: Easier to work with polymorphic values inside IModel
Key: WICKET-7082
URL: https://issues.apache.org/jira/browse/WICKET-7082
Project: Wicket
Issue Type: Improvement
Components: wicket-core
Affects Versions: 10.0.0-M2, 9.15.0
Reporter: Andreas
Wicket currently leaves a lot to be desired when working with polymorphic datastructures.
Consider the following scenario:
interface TextMatchingService { sealed interface Status {
record NotSubmitted(Runnable submit) implements Status {}
record Queued(Instant since, Optional<Instant> expectedFinish) implements Status {}
record Analysed(float percentMatch) implements Status {}
record Error(int errorCode, String humanReadableMessage) implements Status {}
} Status getStatus(File file);
}
Now I want to have a panel that gets the status of a file and then, depending on the status, shows the different cases.
If I were to unpack the model and {{switch}} over the type in the constructor, when the value returned by the {{IModel<Status>}} changes due to the asynchronous nature of the text matching the hierarchy would be out of sync.
The proposed {{poly}} function would enable the following pattern for dealing with this case.
public class TextMatchingPanel extends GenericPanel<File> { @InjectTextMatchingService textMatchingService; public TextMatchingPanel(String id, IModel<File> fileModel) { super(id, fileModel); IModel<Status> statusModel = fileModel.map(textMatchingService::getStatus); add(new NotSubmittedPanel("not_submitted", statusModel.poly(NotSubmitted.class));
[add more specific status panels]
} class NotSubmittedPanel extends Panel { NotSubmittedPanel(String id, IModel<NotSubmitted> notSubmittedModel) { super(id, notSubmittedModel); add(new Link<>("submit") { @Overridepublic void onClick() { NotSubmitted notSubmitted = notSubmittedModel.getObject(); Runnable submitForTextMatching = notSubmitted.submit(); submitForTextMatching.run();
}
});
} @Overrideprotected void onConfigure() { super.onConfigure(); setVisible(getDefaultModelObject() != null);
}
}
[more status panels]
}
If there's a better way to do this, preferably using switch expressions (to get totality checking), that would be preferred but I've yet to come up with one.
Unpacking in the constructor and creating different component hierarchies would work if the page was stateless but then there would also be no need for a detachable {{IModel}} since nothing gets serialized.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)