-
Notifications
You must be signed in to change notification settings - Fork 109
Joda-Time to Java time: Add templates for Joda Interval to Threeten-extra Interval #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb96f7f
Joda-Time to Java time: Add templates for migrating Joda Interval to …
amishra-u 9cf2fe6
Syling: remove indent from empty lines
amishra-u fa33321
Add threeten-extra dependency, if needed
amishra-u 6bfc13f
checkstyle
amishra-u 44f0272
Minor polish
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
64 changes: 64 additions & 0 deletions
64
src/main/java/org/openrewrite/java/migrate/joda/templates/AbstractIntervalTemplates.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2024 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.migrate.joda.templates; | ||
|
|
||
| import lombok.Getter; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.MethodMatcher; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*; | ||
|
|
||
| public class AbstractIntervalTemplates implements Templates { | ||
| private final MethodMatcher getStart = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " getStart()"); | ||
| private final MethodMatcher getEnd = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " getEnd()"); | ||
| private final MethodMatcher toDuration = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " toDuration()"); | ||
| private final MethodMatcher toDurationMillis = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " toDurationMillis()"); | ||
| private final MethodMatcher contains = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " contains(long)"); | ||
|
|
||
| private final JavaTemplate getStartTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getStart().atZone(ZoneId.systemDefault())") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .imports(JAVA_ZONE_ID) | ||
| .build(); | ||
| private final JavaTemplate getEndTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getEnd().atZone(ZoneId.systemDefault())") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .imports(JAVA_ZONE_ID) | ||
| .build(); | ||
| private final JavaTemplate toDurationTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.toDuration()") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .build(); | ||
| private final JavaTemplate toDurationMillisTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.toDuration().toMillis()") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .build(); | ||
| private final JavaTemplate containsTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.contains(Instant.ofEpochMilli(#{any(long)}))") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .imports(JAVA_INSTANT) | ||
| .build(); | ||
|
|
||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Getter | ||
| private final List<MethodTemplate> templates = new ArrayList<MethodTemplate>() { | ||
| { | ||
| add(new MethodTemplate(getStart, getStartTemplate)); | ||
| add(new MethodTemplate(getEnd, getEndTemplate)); | ||
| add(new MethodTemplate(toDuration, toDurationTemplate)); | ||
| add(new MethodTemplate(toDurationMillis, toDurationMillisTemplate)); | ||
| add(new MethodTemplate(contains, containsTemplate)); | ||
| } | ||
| }; | ||
| } | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
58 changes: 58 additions & 0 deletions
58
src/main/java/org/openrewrite/java/migrate/joda/templates/IntervalTemplates.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright 2024 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.migrate.joda.templates; | ||
|
|
||
| import lombok.Getter; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.tree.Expression; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*; | ||
|
|
||
| public class IntervalTemplates implements Templates { | ||
| private final MethodMatcher interval = new MethodMatcher(JODA_INTERVAL + " <constructor>(long, long)"); | ||
| private final MethodMatcher intervalWithTimeZone = new MethodMatcher(JODA_INTERVAL + " <constructor>(long, long, " + JODA_DATE_TIME_ZONE + ")"); | ||
| private final MethodMatcher intervalWithDateTime = new MethodMatcher(JODA_INTERVAL + " <constructor>(" + JODA_READABLE_INSTANT + ", " + JODA_READABLE_INSTANT + ")"); | ||
| private final MethodMatcher intervalWithDateTimeAndDuration = new MethodMatcher(JODA_INTERVAL + " <constructor>(" + JODA_READABLE_INSTANT + ", " + JODA_READABLE_DURATION + ")"); | ||
|
|
||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private final JavaTemplate intervalTemplate = JavaTemplate.builder("Interval.of(Instant.ofEpochMilli(#{any(long)}), Instant.ofEpochMilli(#{any(long)}))") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .imports(JAVA_INSTANT, THREE_TEN_EXTRA_INTERVAL) | ||
| .build(); | ||
| private final JavaTemplate intervalWithDateTimeTemplate = JavaTemplate.builder("Interval.of(#{any(" + JAVA_DATE_TIME + ")}.toInstant(), #{any(" + JAVA_DATE_TIME + ")}.toInstant())") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .imports(THREE_TEN_EXTRA_INTERVAL) | ||
| .build(); | ||
| private final JavaTemplate intervalWithDateTimeAndDurationTemplate = JavaTemplate.builder("Interval.of(#{any(" + JAVA_DATE_TIME + ")}.toInstant(), #{any(" + JAVA_DURATION + ")})") | ||
| .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra")) | ||
| .imports(THREE_TEN_EXTRA_INTERVAL) | ||
| .build(); | ||
|
|
||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Getter | ||
| private final List<MethodTemplate> templates = new ArrayList<MethodTemplate>() { | ||
| { | ||
| add(new MethodTemplate(interval, intervalTemplate)); | ||
| add(new MethodTemplate(intervalWithTimeZone, intervalTemplate, | ||
| m -> new Expression[]{m.getArguments().get(0), m.getArguments().get(1)})); | ||
| add(new MethodTemplate(intervalWithDateTime, intervalWithDateTimeTemplate)); | ||
| add(new MethodTemplate(intervalWithDateTimeAndDuration, intervalWithDateTimeAndDurationTemplate)); | ||
| } | ||
| }; | ||
| } | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.