-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Joda-Time to Java time: Add templates for migrating Joda Interval to …
…Threeten-Extra Interval
- Loading branch information
Showing
8 changed files
with
221 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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(); | ||
|
||
@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)); | ||
} | ||
}; | ||
} |
This file contains 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 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 + ")"); | ||
|
||
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(); | ||
|
||
@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)); | ||
} | ||
}; | ||
} |
This file contains 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 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 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