Adopt Java 21+ pattern matching for switch and Math.clamp - #748
Draft
koppor wants to merge 1 commit into
Draft
Conversation
Convert same-selector instanceof/else-if chains to pattern-matching switches (with explicit null arms preserving the old fall-through behavior): GroupNodeViewModel#canAddEntriesIn (matching its four already-converted sibling methods), PreferencesFilter#getType, JabRefCliPreferences#getObject, MarkdownTextFlow#getMarkdownRepresentation, MathSciNet#getOthers, AllFieldsTab#normalizeInputHeights, and the two OOStyle dispatches in OOBibBase. In MarkdownTextFlow, precompile BULLET_LIST_PATTERN as java.util.regex.Pattern and drop NUMBERED_LIST_PATTERN, whose branch was identical to the fallback. Replace two Math.max(0, Math.min(...)) constructs in WalkthroughScroller with Math.clamp. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KHtazEDo3P1E6hdBztB8jX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
🤖 This PR modernizes hand-rolled type-dispatch code to Java 21+ pattern matching for switch: eight same-selector
instanceof/else-if chains become pattern switches with explicitcase nullarms that preserve the previous fall-through behavior —GroupNodeViewModel#canAddEntriesIn(completing the style of its four already-converted sibling methods),PreferencesFilter#getType,JabRefCliPreferences#getObject,MarkdownTextFlow#getMarkdownRepresentation,MathSciNet#getOthers,AllFieldsTab#normalizeInputHeights, and bothOOStyledispatches inOOBibBase. TwoMath.max(0, Math.min(…))constructs inWalkthroughScrollerbecomeMath.clamp(JDK 21). En passant,MarkdownTextFlow's bullet-list regex is now a precompiledPattern(perCHECKLIST.md), andNUMBERED_LIST_PATTERNis dropped because its branch was byte-identical to the fallback.Note on scope: JabRef currently builds with language level 25 (
-PjavaVersion=26exists only for EA testing, and Java 27 is not released), so this PR uses finalized Java 21–25 features only. The group-class hierarchy was checked so that switch arm order cannot reorder overlapping matches (e.g.ExplicitGroup extends WordKeywordGroup extends KeywordGroup); the compiler's dominance checking now guards these dispatches, which plain else-if chains never did.Analogies
Like honey, pattern matching lets the type flow into the case label in one smooth motion instead of being scraped out of a cast in the branch body. Like chocolate, the switch arms are neatly segmented squares — you can see at a glance whether a piece (a subtype) is missing, which the compiler now checks for us. And like the moon pulling tides, the selector expression at the top governs every arm below it — one gravitational source instead of eight repeated
instanceoftests.jabref-contrib-policy:4.2:reviewed:ok
Steps to test
Pure refactoring — no user-visible behavior change intended. Verified by:
./gradlew :jablib:test :jabgui:testfor the touched classes (MarkdownTextFlowTest,GroupNodeViewModelTest,GroupNodeViewModelFilterTest) — green.canAddEntriesIn) — the entry was added and the group hit counter updated from 0 to 1.Related issues and pull requests
None to close — code-quality refactoring.
AI usage
Claude Code (model claude-fable-5), AIL4 — the analysis, code changes, and this PR description are AI-generated end-to-end; review and ownership by the PR author is pending (hence WIP on jabref-koppor rather than upstream).
AI CHECKLIST.md walkthrough
1. Code self-review
Nullability and control flow
== null/!= nullchecks added — nullability is handled viacase nullswitch arms.Objects.requireNonNull(...)added.@NullMarked— no new classes.Optionalconsumption — the only touched Optional code (MathSciNet#getOthers) keeps itsOptional.of/Optional.emptystructure.StringUtil.isBlank(...)— no blank checks touched.Exceptions
catch (Exception e)added.RuntimeException;GroupNodeViewModel#canAddEntriesInkeeps its pre-existingUnsupportedOperationExceptiondefault arm and gains the sameIllegalArgumentException-on-null arm its four sibling methods already have.Style and idioms
BibEntrywithers — not touched._(Java 22),Math.clamp(Java 21) — all finalized at the project's language level 25.Pattern—BULLET_LIST_PATTERNconverted from aStringused withString#matches.User-facing text
Security
Tests
org.jabref.logicmethods (JabRefCliPreferences#getObject,MathSciNet#getOthers) keep their existing coverage (JabRefCliPreferencesTest,MathSciNetTest), andMarkdownTextFlowTest/GroupNodeViewModelTest/GroupNodeViewModelFilterTestpass unchanged — semantics preserved is exactly what they assert.2. Verification commands
./gradlew :jablib:compileJava :jabgui:compileJavaand the test classes above — green../gradlew :jablib:checkstyleMain :jabgui:checkstyleMain— pass../gradlew modernizer— pass../gradlew rewriteRunproduced no changes on top of this diff../gradlew javadoc— no doc comments touched.markdownlint— no Markdown changed.idea formatwith.idea/codeStyles/Project.xml.Checklist
CHANGELOG.mdin a way that can be understood by the average user (if change is visible to the user)🤖 Generated with Claude Code