diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5275cec --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Created by .ignore support plugin (hsz.mobi) +.gradle/ +/.idea/* +!/.idea/codeStyles +!/.idea/watcherTasks.xml +!/.idea/runConfigurations/ +!/.idea/scopes/Code.xml +build/ +out/ \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..cba1d10 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..ad1ab4e --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.java-version b/.java-version new file mode 100644 index 0000000..3b5b5d8 --- /dev/null +++ b/.java-version @@ -0,0 +1 @@ +11.0 \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..aabdc03 --- /dev/null +++ b/build.gradle @@ -0,0 +1,47 @@ +plugins { + id 'org.jetbrains.kotlin.jvm' version "$KOTLIN_VERSION" +} + +java.sourceCompatibility = JavaVersion.VERSION_11 +java.targetCompatibility = JavaVersion.VERSION_11 + +group "to.refactoring.javatokotlin.travelator" +version "1.0-SNAPSHOT" + +repositories { + mavenCentral() + jcenter() + mavenLocal() +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + + implementation "com.fasterxml.jackson.core:jackson-databind:2.10.0" + implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.0" + implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.10.0" + + testImplementation "org.junit.jupiter:junit-jupiter-api:5.4.2" + testImplementation "org.junit.jupiter:junit-jupiter-params:5.4.2" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2" + testRuntimeOnly "org.junit.platform:junit-platform-launcher:1.4.2" +} + +tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { + kotlinOptions { + jvmTarget = "11" + javaParameters = true + freeCompilerArgs = ["-Xjvm-default=all"] + } +} + +println("building with Kotlin $KOTLIN_VERSION") + +test { + useJUnitPlatform { + includeEngines "junit-jupiter" + } + testLogging { + events "skipped", "failed" + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..2802402 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,5 @@ +org.gradle.parallel=true +org.gradle.caching=false + +KOTLIN_VERSION=1.5.0 +kotlin.code.style=official \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..5c2d1cf Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..9d6d2f8 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Mar 15 11:22:46 GMT 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME \ No newline at end of file diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..77564dd --- /dev/null +++ b/gradlew @@ -0,0 +1,188 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# 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 +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" \ No newline at end of file diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..15e3d84 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,100 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..70144f0 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'refactoring-to-kotlin-code' \ No newline at end of file diff --git a/src/main/java/travelator/Collections.java b/src/main/java/travelator/Collections.java new file mode 100644 index 0000000..58158e1 --- /dev/null +++ b/src/main/java/travelator/Collections.java @@ -0,0 +1,18 @@ +package travelator; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; + +public class Collections { + @SuppressWarnings("unchecked") + public static List sorted( + Collection collection, + Comparator by + ) { + var result = (E[]) collection.toArray(); + Arrays.sort(result, by); + return Arrays.asList(result); + } +} \ No newline at end of file diff --git a/src/main/java/travelator/Journey.java b/src/main/java/travelator/Journey.java new file mode 100644 index 0000000..c92ceed --- /dev/null +++ b/src/main/java/travelator/Journey.java @@ -0,0 +1,39 @@ +package travelator; + +import java.time.Duration; +import java.time.ZonedDateTime; + +public class Journey { + + private final Location departsFrom; + private final Location arrivesAt; + private final ZonedDateTime departureTime; + private final ZonedDateTime arrivalTime; + + public Journey(Location departsFrom, Location arrivesAt, ZonedDateTime departureTime, ZonedDateTime arrivalTime) { + this.departsFrom = departsFrom; + this.arrivesAt = arrivesAt; + this.departureTime = departureTime; + this.arrivalTime = arrivalTime; + } + + public Location getDepartsFrom() { + return departsFrom; + } + + public Location getArrivesAt() { + return arrivesAt; + } + + public ZonedDateTime getDepartureTime() { + return departureTime; + } + + public ZonedDateTime getArrivalTime() { + return arrivalTime; + } + + public Duration getDuration() { + return Duration.between(departureTime, arrivalTime); + } +} \ No newline at end of file diff --git a/src/main/java/travelator/Location.java b/src/main/java/travelator/Location.java new file mode 100644 index 0000000..915a3e2 --- /dev/null +++ b/src/main/java/travelator/Location.java @@ -0,0 +1,4 @@ +package travelator; + +public class Location { +} \ No newline at end of file diff --git a/src/main/java/travelator/Other.java b/src/main/java/travelator/Other.java new file mode 100644 index 0000000..22550ff --- /dev/null +++ b/src/main/java/travelator/Other.java @@ -0,0 +1,13 @@ +package travelator; + +import java.util.List; + +public class Other { + public static int SOME_COMPLICATED_RESULT() { + return 0; + } + + public static List> routesFor(String itineraryId) { + return null; + } +} \ No newline at end of file diff --git a/src/main/java/travelator/Routes.java b/src/main/java/travelator/Routes.java new file mode 100644 index 0000000..1bcc11d --- /dev/null +++ b/src/main/java/travelator/Routes.java @@ -0,0 +1,11 @@ +package travelator; + +import java.util.List; + +public class Routes { + + public static Location getDepartsFrom(List route) { + return route.get(0).getDepartsFrom(); + } + +} \ No newline at end of file diff --git a/src/main/java/travelator/Suffering.kt b/src/main/java/travelator/Suffering.kt new file mode 100644 index 0000000..7cc6ca1 --- /dev/null +++ b/src/main/java/travelator/Suffering.kt @@ -0,0 +1,28 @@ +package travelator + +import travelator.Other.SOME_COMPLICATED_RESULT +import travelator.Other.routesFor + +object Suffering { + @JvmStatic + fun sufferScoreFor(route: List): Int = + sufferScore( + route.longestJourneys(limit = 3), + Routes.getDepartsFrom(route) + ) + + @JvmStatic + fun List.longestJourneys(limit: Int): List = + sortedByDescending { it.duration }.take(limit) + + fun routesToShowFor(itineraryId: String?): List> = + bearable(routesFor(itineraryId)) + + private fun bearable(routes: List>): List> = + routes.filter { sufferScoreFor(it) <= 10 } + + private fun sufferScore( + longestJourneys: List, + start: Location + ): Int = SOME_COMPLICATED_RESULT() +} \ No newline at end of file diff --git a/src/main/java/travelator/UI.java b/src/main/java/travelator/UI.java new file mode 100644 index 0000000..a25d680 --- /dev/null +++ b/src/main/java/travelator/UI.java @@ -0,0 +1,39 @@ +package travelator; + +import java.util.List; +import java.util.Objects; + +import static java.util.stream.Collectors.toList; + +public class UI { + + public static List tableData(List> routes) { + return routes.stream().map( (route) -> + new RouteRow(route, Suffering.sufferScoreFor(route)) // <1> + ).collect(toList()); + } +} + +class RouteRow { + public final List route; + public final int sufferScore; + + public RouteRow(List route, int sufferScore) { + this.route = route; + this.sufferScore = sufferScore; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RouteRow routeRow = (RouteRow) o; + return sufferScore == routeRow.sufferScore && + route.equals(routeRow.route); + } + + @Override + public int hashCode() { + return Objects.hash(route, sufferScore); + } +} \ No newline at end of file diff --git a/src/test/java/travelator/LongestJourneyInTests.java b/src/test/java/travelator/LongestJourneyInTests.java new file mode 100644 index 0000000..4736ae2 --- /dev/null +++ b/src/test/java/travelator/LongestJourneyInTests.java @@ -0,0 +1,56 @@ +package travelator; + +import org.junit.jupiter.api.Test; + +import java.time.ZonedDateTime; +import java.util.List; + +import static java.util.Collections.emptyList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static travelator.Suffering.longestJourneys; + +public class LongestJourneyInTests { + + @Test public void returns_empty_list_for_empty_list() { + assertEquals( + emptyList(), + longestJourneys(emptyList(), 2) + ); + } + + @Test public void returns_empty_list_for_0_limit() { + assertEquals( + emptyList(), + longestJourneys(List.of(shortJourney), 0) + ); + } + + @Test public void returns_journeys_sorted() { + assertEquals( + List.of(longJourney, mediumJourney, shortJourney), + longestJourneys(List.of(shortJourney, mediumJourney, longJourney), 3) + ); + } + + @Test public void returns_limit_results() { + assertEquals( + List.of(longJourney, mediumJourney), + longestJourneys(List.of(shortJourney, mediumJourney, longJourney), 2) + ); + } + + @Test public void returns_up_to_limit_results() { + assertEquals( + List.of(longJourney, mediumJourney, shortJourney), + longestJourneys(List.of(shortJourney, mediumJourney, longJourney), 4) + ); + } + + private final ZonedDateTime now = ZonedDateTime.now(); + private Location somewhere = new Location(); + private Location somewhereElse = new Location(); + + private final Journey shortJourney = new Journey(somewhere, somewhereElse, now, now.plusHours(1)); + private final Journey mediumJourney = new Journey(somewhere, somewhereElse, now, now.plusHours(2)); + private final Journey longJourney = new Journey(somewhere, somewhereElse, now, now.plusHours(3)); +} \ No newline at end of file