Skip to content

Commit

Permalink
Merge pull request #441 from mivek/feature/apache_commons
Browse files Browse the repository at this point in the history
Replaced opencsv with apache-commons-csv
  • Loading branch information
mivek authored Dec 8, 2022
2 parents 51ab6b8 + c508e14 commit 1b9e822
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sonar-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ jobs:
distribution: 'adopt'
cache: 'maven'
- name: Cache SonarCloud packages
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.*;

import io.github.mivek.model.Metar;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -18,4 +19,12 @@ void testCanParse() {
assertTrue(command.canParse("3/4SM"));
assertTrue(command.canParse("M6SM"));
}

@Test
void testExecute() {
MainVisibilityNauticalMilesCommand command = new MainVisibilityNauticalMilesCommand();
Metar m = new Metar();
assertTrue(command.execute(m, "P6SM"));
assertNotNull(m.getVisibility());
}
}
4 changes: 2 additions & 2 deletions metarParser-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package io.github.mivek.provider.airport.impl;

import com.opencsv.CSVParser;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;
import io.github.mivek.model.Airport;
import io.github.mivek.model.Country;
import io.github.mivek.provider.airport.AirportProvider;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

/**
* Default implementation of the AiportProvider using local files to build the airport map.
Expand Down Expand Up @@ -45,14 +43,13 @@ public DefaultAirportProvider() {
private void initCountries() {
Objects.requireNonNull(countriesFile);
countries = new HashMap<>();
String[] line;
try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(countriesFile, StandardCharsets.UTF_8)).withCSVParser(new CSVParser()).withSkipLines(0).build()) {
while ((line = reader.readNext()) != null) {
try (CSVParser parser = CSVFormat.DEFAULT.parse(new InputStreamReader(countriesFile, StandardCharsets.UTF_8))) {
for (CSVRecord line : parser) {
Country country = new Country();
country.setName(line[0]);
country.setName(line.get(0));
countries.put(country.getName(), country);
}
} catch (IOException | CsvValidationException exception) {
} catch (IOException exception) {
throw new IllegalStateException(exception.getMessage());
}
}
Expand All @@ -63,23 +60,22 @@ private void initCountries() {
private void initAirports() {
Objects.requireNonNull(airportsFile);
airports = new HashMap<>();
String[] line;
try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(airportsFile, StandardCharsets.UTF_8)).withCSVParser(new CSVParser()).withSkipLines(0).build()) {
while ((line = reader.readNext()) != null) {
try (CSVParser parser = CSVFormat.DEFAULT.parse(new InputStreamReader(airportsFile, StandardCharsets.UTF_8))) {
for (CSVRecord line : parser) {
Airport airport = new Airport();
airport.setName(line[1]);
airport.setCity(line[2]);
airport.setCountry(countries.get(line[3]));
airport.setIata(line[4]);
airport.setIcao(line[5]);
airport.setLatitude(Double.parseDouble(line[6]));
airport.setLongitude(Double.parseDouble(line[7]));
airport.setAltitude(Integer.parseInt(line[8]));
airport.setTimezone(line[9]);
airport.setDst(line[10]);
airport.setName(line.get(1));
airport.setCity(line.get(2));
airport.setCountry(countries.get(line.get(3)));
airport.setIata(line.get(4));
airport.setIcao(line.get(5));
airport.setLatitude(Double.parseDouble(line.get(6)));
airport.setLongitude(Double.parseDouble(line.get(7)));
airport.setAltitude(Integer.parseInt(line.get(8)));
airport.setTimezone(line.get(9));
airport.setDst(line.get(10));
airports.put(airport.getIcao(), airport);
}
} catch (IOException | CsvValidationException exception) {
} catch (IOException exception) {
throw new IllegalStateException(exception.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package io.github.mivek.provider.airport.impl;

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;
import io.github.mivek.model.Airport;
import io.github.mivek.model.Country;
import io.github.mivek.provider.airport.AirportProvider;
import org.apache.commons.lang3.math.NumberUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -22,6 +15,10 @@
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.lang3.math.NumberUtils;

/**
* Implementation of the AirportProvider based on ourAirports.
Expand All @@ -38,32 +35,30 @@ public final class OurAirportsAirportProvider implements AirportProvider {
private final Map<String, Country> countries;
/** Map of airports. */
private final Map<String, Airport> airports;
/** Common CSV Parser. */
private final CSVParser parser;
/** Common CSV format. */
private final CSVFormat csvFormat;

/**
* Default constructor.
*
* @throws CsvValidationException when the parsing of the file fails
* @throws IOException when network error
* @throws URISyntaxException when the URI is invalid
*/
public OurAirportsAirportProvider() throws CsvValidationException, IOException, URISyntaxException, InterruptedException {
public OurAirportsAirportProvider() throws IOException, URISyntaxException, InterruptedException {
countries = new HashMap<>();
airports = new HashMap<>();
parser = new CSVParserBuilder().withIgnoreQuotations(true).build();
csvFormat = CSVFormat.RFC4180;
buildCountries();
buildAirport();
}

/**
* Connects to the countries list and build a map of {@link Country} with the name as key.
*
* @throws CsvValidationException when the parsing of the file fails
* @throws IOException when network error
* @throws URISyntaxException when the URI is invalid
*/
public void buildCountries() throws URISyntaxException, IOException, CsvValidationException, InterruptedException {
public void buildCountries() throws URISyntaxException, IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(COUNTRIES_URI))
.GET()
Expand All @@ -74,24 +69,22 @@ public void buildCountries() throws URISyntaxException, IOException, CsvValidati
HttpResponse<InputStream> response = HttpClient.newBuilder()
.build()
.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(response.body(), StandardCharsets.UTF_8)).withCSVParser(parser).withSkipLines(1).build()) {
String[] line;
while ((line = reader.readNext()) != null) {
try (CSVParser parser = csvFormat.parse(new InputStreamReader(response.body(), StandardCharsets.UTF_8))) {
for (CSVRecord line: parser) {
Country c = new Country();
c.setName(line[2]);
countries.put(line[1], c);
c.setName(line.get(2));
countries.put(line.get(1), c);
}
}
}

/**
* Connects to the airports list and build a map of {@link Airport} with the name as key.
*
* @throws CsvValidationException when the parsing of the file fails
* @throws IOException when network error
* @throws URISyntaxException when the URI is invalid
*/
public void buildAirport() throws URISyntaxException, IOException, CsvValidationException, InterruptedException {
public void buildAirport() throws URISyntaxException, IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(AIRPORT_URI))
.GET()
Expand All @@ -102,19 +95,17 @@ public void buildAirport() throws URISyntaxException, IOException, CsvValidation
HttpResponse<InputStream> response = HttpClient.newBuilder()
.build()
.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(response.body(), StandardCharsets.UTF_8)).withCSVParser(parser).withSkipLines(1).build()) {
String[] line;

while ((line = reader.readNext()) != null) {
try (CSVParser parser = csvFormat.parse(new InputStreamReader(response.body(), StandardCharsets.UTF_8))) {
for (CSVRecord line : parser) {
Airport airport = new Airport();
airport.setIcao(line[1]);
airport.setName(line[3]);
airport.setLatitude(NumberUtils.toDouble(line[4], 0));
airport.setLongitude(NumberUtils.toDouble(line[5], 0));
airport.setAltitude(NumberUtils.toInt(line[6], 0));
airport.setCountry(countries.get(line[8]));
airport.setCity(line[10]);
airport.setIata(line[13]);
airport.setIcao(line.get(1));
airport.setName(line.get(3));
airport.setLatitude(NumberUtils.toDouble(line.get(4), 0));
airport.setLongitude(NumberUtils.toDouble(line.get(5), 0));
airport.setAltitude(NumberUtils.toInt(line.get(6), 0));
airport.setCountry(countries.get(line.get(8)));
airport.setCity(line.get(10));
airport.setIata(line.get(13));
airports.put(airport.getIcao(), airport);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.mivek.provider.airport.impl;

import com.opencsv.exceptions.CsvValidationException;
import io.github.mivek.model.Airport;
import io.github.mivek.provider.airport.AirportProvider;
import org.junit.jupiter.api.Disabled;
Expand All @@ -21,7 +20,7 @@
class OurAirportsAirportProviderTest {

@Test
void testGetAirport() throws CsvValidationException, IOException, URISyntaxException, InterruptedException {
void testGetAirport() throws IOException, URISyntaxException, InterruptedException {
AirportProvider provider = new OurAirportsAirportProvider();

Map<String, Airport> airports = provider.getAirports();
Expand Down
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
<jacoco.coverage.instruction.minimum>0.98</jacoco.coverage.instruction.minimum>
<jacoco.coverage.branch.minimum>0.96</jacoco.coverage.branch.minimum>
<jacoco.coverage.complexity.minimum>0.97</jacoco.coverage.complexity.minimum>
<archunit-junit5.version>1.0.0</archunit-junit5.version>
<checkstyle.version>10.4</checkstyle.version>
<archunit-junit5.version>1.0.1</archunit-junit5.version>
<checkstyle.version>10.5.0</checkstyle.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<hamcrest.version>2.2</hamcrest.version>
<jacoco-maven-plugin.version>0.8.8</jacoco-maven-plugin.version>
Expand All @@ -65,10 +65,10 @@
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<opencsv.version>5.7.1</opencsv.version>
<commons-csv.version>1.9.0</commons-csv.version>
<pitest-junit5-plugin.version>1.1.0</pitest-junit5-plugin.version>
<pitest-maven.version>1.9.11</pitest-maven.version>
<slf4j-nop.version>2.0.4</slf4j-nop.version>
<slf4j-nop.version>2.0.5</slf4j-nop.version>
<spotbugs-maven-plugin.version>4.7.3.0</spotbugs-maven-plugin.version>
<sonar.organization>mivek-github</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
Expand All @@ -85,9 +85,9 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>${commons-csv.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down

0 comments on commit 1b9e822

Please sign in to comment.