From 9c88c281516b8d55b3f3943a524e74115fc46898 Mon Sep 17 00:00:00 2001 From: Julian Arndt Date: Tue, 29 Sep 2020 17:43:05 +0200 Subject: [PATCH] Refactor Line separator for Mac and add APIFootballProvider --- gradlew | 0 gradlew.bat | 208 +++++++++--------- .../apifootball/APIFootballConnector.java | 3 +- .../jeyp91/apifootball/APIFootballMatch.java | 3 +- .../APIFootballMatchesProvider.java | 35 +++ .../jeyp91/tippliga/TLWMatchdaysCreator.java | 3 +- .../tippliga/TLWMatchesCreatorBase.java | 1 - .../tippliga/TLWMatchesCreatorFootball.java | 40 +--- .../TLWMatchesCreatorTipperLeague.java | 3 +- .../TLWMatchesCreatorTipperPokal.java | 3 +- 10 files changed, 161 insertions(+), 138 deletions(-) mode change 100644 => 100755 gradlew create mode 100644 src/main/java/de/jeyp91/apifootball/APIFootballMatchesProvider.java diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/gradlew.bat b/gradlew.bat index 5093609..a9f778a 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,104 +1,104 @@ -@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 Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@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 +@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 Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@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 diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java b/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java index 0f6a07c..93196d9 100644 --- a/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java +++ b/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java @@ -12,6 +12,7 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; @@ -77,7 +78,7 @@ public class APIFootballConnector { JSONObject object = null; //JSON parser object to parse read file JSONParser jsonParser = new JSONParser(); - URL url = Resources.getResource((this.season + 1) + "\\API-Football\\" + filename + ".json"); + URL url = Resources.getResource((this.season + 1) + File.separator + "API-Football" + File.separator + filename + ".json"); String jsonConfig = null; try { diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java b/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java index 650cc44..0b385a5 100644 --- a/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java +++ b/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java @@ -7,6 +7,7 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -55,7 +56,7 @@ public class APIFootballMatch extends BaseMatch { JSONObject object = null; //JSON parser object to parse read file JSONParser jsonParser = new JSONParser(); - URL url = Resources.getResource((this.season + 1) + "\\API-Football\\" + filename); + URL url = Resources.getResource((this.season + 1) + File.separator + "API-Football" + File.separator + filename); String jsonConfig = null; try { diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballMatchesProvider.java b/src/main/java/de/jeyp91/apifootball/APIFootballMatchesProvider.java new file mode 100644 index 0000000..1c99f6c --- /dev/null +++ b/src/main/java/de/jeyp91/apifootball/APIFootballMatchesProvider.java @@ -0,0 +1,35 @@ +package de.jeyp91.apifootball; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.ArrayList; + +public class APIFootballMatchesProvider { + + APIFootballConnector conn; + public APIFootballMatchesProvider(int season) { + this.conn = new APIFootballConnector(season - 1); + } + + public ArrayList getAPIFootballMatchesFromConfig(JSONArray config) { + ArrayList apiFootballMatches = new ArrayList<>(); + for (Object singleConfigObject : config) { + JSONObject singleConfig = (JSONObject) singleConfigObject; + String type = (String) singleConfig.get("type"); + switch (type) { + case "AllMatchesOfMatchday": + int matchesLeague = ((Long) singleConfig.get("matchesLeague")).intValue(); + int leagueMatchday = ((Long) singleConfig.get("leagueMatchday")).intValue(); + apiFootballMatches.addAll(conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday)); + break; + case "SingleMatch": + int matchLeague = ((Long) singleConfig.get("matchLeague")).intValue(); + int matchId = ((Long) singleConfig.get("matchId")).intValue(); + apiFootballMatches.add(conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId)); + break; + } + } + return apiFootballMatches; + } +} diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java index 27bd5e3..540ff1e 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java @@ -6,6 +6,7 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -29,7 +30,7 @@ public class TLWMatchdaysCreator { this.matches = matchesCreator.getMatches(); JSONParser jsonParser = new JSONParser(); - URL url = Resources.getResource(season + "\\" + configPath); + URL url = Resources.getResource(season + File.separator + configPath); String jsonConfig = null; try { jsonConfig = Resources.toString(url, StandardCharsets.UTF_8); diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorBase.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorBase.java index 863c944..40762e1 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorBase.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorBase.java @@ -7,7 +7,6 @@ import java.util.ArrayList; public abstract class TLWMatchesCreatorBase { ArrayList TLWMatches; - APIFootballConnector conn; public TLWMatchesCreatorBase() { this.TLWMatches = new ArrayList<>(); diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java index de991e1..1526e29 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java @@ -1,13 +1,14 @@ package de.jeyp91.tippliga; import com.google.common.io.Resources; -import de.jeyp91.apifootball.APIFootballConnector; import de.jeyp91.apifootball.APIFootballMatch; +import de.jeyp91.apifootball.APIFootballMatchesProvider; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -31,11 +32,15 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { this.season = season; this.league = league; - this.conn = new APIFootballConnector(season - 1); - URL url = Resources.getResource(season + "\\" + configFileName); + this.initConfigParamsFromFile(configFileName); + this.publicateMatchObjects(); + } + + private void initConfigParamsFromFile(String configFileName) { + + URL url = Resources.getResource(season + File.separator + configFileName); String jsonConfig = null; - try { JSONParser jsonParser = new JSONParser(); jsonConfig = Resources.toString(url, StandardCharsets.UTF_8); @@ -48,16 +53,16 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { } catch (IOException | ParseException e) { e.printStackTrace(); } - - this.publicateMatchObjects(); } private void publicateMatchObjects() { + APIFootballMatchesProvider provider = new APIFootballMatchesProvider(this.season); + for(int i = 0; i < this.matchdayConfig.size(); i++) { int TLWMatchday = ((Long) ((JSONObject) this.matchdayConfig.get(i)).get("TLWMatchday")).intValue(); JSONArray matchesConfig = (JSONArray)((JSONObject) this.matchdayConfig.get(i)).get("matchesConfig"); - ArrayList APIFootballMatches = getAPIFootballMatchesForMatchday(matchesConfig); + ArrayList APIFootballMatches = provider.getAPIFootballMatchesFromConfig(matchesConfig); int tempNumberOfMatchesBackup = this.matchesPerMatchday; if(((JSONObject) this.matchdayConfig.get(i)).containsKey("numberOfMatches")) { @@ -109,27 +114,6 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { } } - private ArrayList getAPIFootballMatchesForMatchday(JSONArray config) { - ArrayList apiFootballMatches = new ArrayList<>(); - for (Object singleConfigObject : config) { - JSONObject singleConfig = (JSONObject) singleConfigObject; - String type = (String) singleConfig.get("type"); - switch (type) { - case "AllMatchesOfMatchday": - int matchesLeague = ((Long) singleConfig.get("matchesLeague")).intValue(); - int leagueMatchday = ((Long) singleConfig.get("leagueMatchday")).intValue(); - apiFootballMatches.addAll(conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday)); - break; - case "SingleMatch": - int matchLeague = ((Long) singleConfig.get("matchLeague")).intValue(); - int matchId = ((Long) singleConfig.get("matchId")).intValue(); - apiFootballMatches.add(conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId)); - break; - } - } - return apiFootballMatches; - } - private int getDaysDifference(Date date1, Date date2) { long startTime = date1.getTime(); long endTime = date2.getTime(); diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperLeague.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperLeague.java index e787a95..afbb448 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperLeague.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperLeague.java @@ -6,6 +6,7 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -27,7 +28,7 @@ public class TLWMatchesCreatorTipperLeague extends TLWMatchesCreatorBase { this.matchdays = matchdays; URL matchPairConfigUrl = Resources.getResource("Tipper_Match_Pair_Config.json"); - URL tipperListUrl = Resources.getResource(season + "\\" + configFileName); + URL tipperListUrl = Resources.getResource(season + File.separator + configFileName); URL tipperTeamConfigUrl = Resources.getResource("Tipper_Team_Config.json"); try { diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokal.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokal.java index d457a01..1ed54da 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokal.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokal.java @@ -6,6 +6,7 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -26,7 +27,7 @@ public class TLWMatchesCreatorTipperPokal extends TLWMatchesCreatorBase{ this.league = league; this.matchdays = matchdays; - URL tipperListUrl = Resources.getResource(season + "\\" + configFileName); + URL tipperListUrl = Resources.getResource(season + File.separator + configFileName); URL tipperTeamConfigUrl = Resources.getResource("Tipper_Team_Config.json"); this.TLWMatches = new ArrayList<>();