Skip to content

Commit

Permalink
Clean code in metarParser-services module
Browse files Browse the repository at this point in the history
Signed-off-by: jk KPADEY <[email protected]>
  • Loading branch information
mivek committed May 26, 2020
1 parent 2d5c391 commit 152c845
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

/**
* Abstract service.
*
* @param <T> a concrete sub-class of {@link AbstractWeatherCode}.
* @author mivek
* Abstract class for the service.
* @param <T> a concrete sub-class of {@link AbstractWeatherCode}.
*/
public abstract class AbstractWeatherCodeService<T extends AbstractWeatherCode> implements IWeatherCodeFacade<T> {
/**
Expand All @@ -22,10 +23,11 @@ public abstract class AbstractWeatherCodeService<T extends AbstractWeatherCode>

/**
* Protected constructor to be used by sub-classes.
* @param pParser the parser to set.
*
* @param parser the parser to set.
*/
protected AbstractWeatherCodeService(final AbstractParser<T> pParser) {
fParser = pParser;
protected AbstractWeatherCodeService(final AbstractParser<T> parser) {
fParser = parser;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@

/**
* Interface for service.
* @author mivek
*
* @param <T> a concrete sub-class of {@link AbstractWeatherCode}
* @author mivek
*/
public interface IWeatherCodeFacade<T extends AbstractWeatherCode> {
/**
* Decode method.
* @param pCode the code to decode.
*
* @param code the code to decode.
* @return the decoded object corresponding to the message.
* @throws ParseException when an error occurs during the parsing.
*/
T decode(String pCode) throws ParseException;
T decode(String code) throws ParseException;

/**
* Retrieve code and decoded object from airport with icao.
* @param pIcao the icao of the airport
*
* @param icao the icao of the airport
* @return the decoded object
* @throws IOException When an error occurs
* @throws IOException When an error occurs
* @throws URISyntaxException When an error occurs.
* @throws ParseException when an error occurs during the parsing.
* @throws ParseException when an error occurs during the parsing.
*/
T retrieveFromAirport(String pIcao) throws ParseException, IOException, URISyntaxException;
T retrieveFromAirport(String icao) throws ParseException, IOException, URISyntaxException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/**
* Class representing the service for metar.
*
* @author mivek
*/
public final class MetarService extends AbstractWeatherCodeService<Metar> {
Expand All @@ -32,17 +33,17 @@ private MetarService() {
}

@Override
public Metar decode(final String pCode) throws ParseException {
return getParser().parse(pCode);
public Metar decode(final String code) throws ParseException {
return getParser().parse(code);
}

@Override
public Metar retrieveFromAirport(final String pIcao) throws ParseException, IOException {
if (pIcao.length() != AbstractWeatherCodeService.ICAO) {
public Metar retrieveFromAirport(final String icao) throws ParseException, IOException {
if (icao.length() != AbstractWeatherCodeService.ICAO) {
throw new ParseException(ErrorCodes.ERROR_CODE_INVALID_ICAO); // $NON-NLS-1$
}
String website = NOAA_METAR_URL + pIcao.toUpperCase() // $NON-NLS-1$
+ ".TXT"; //$NON-NLS-1$
String website = NOAA_METAR_URL + icao.toUpperCase() // $NON-NLS-1$
+ ".TXT"; //$NON-NLS-1$
URL url = new URL(website);
URLConnection urlCo = url.openConnection();
try (BufferedReader br = new BufferedReader(new InputStreamReader(urlCo.getInputStream(), StandardCharsets.UTF_8))) {
Expand All @@ -53,6 +54,7 @@ public Metar retrieveFromAirport(final String pIcao) throws ParseException, IOEx

/**
* Returns a instance of the class.
*
* @return the instance of the class.
*/
public static MetarService getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

/**
* Facade for TAF.
*
* @author mivek
*/
public final class TAFService extends AbstractWeatherCodeService<TAF> {
Expand All @@ -35,17 +36,17 @@ private TAFService() {
}

@Override
public TAF decode(final String pCode) throws ParseException {
return getParser().parse(pCode);
public TAF decode(final String code) throws ParseException {
return getParser().parse(code);
}

@Override
public TAF retrieveFromAirport(final String pIcao) throws IOException, URISyntaxException, ParseException {
if (pIcao.length() != AbstractWeatherCodeService.ICAO) {
public TAF retrieveFromAirport(final String icao) throws IOException, URISyntaxException, ParseException {
if (icao.length() != AbstractWeatherCodeService.ICAO) {
throw new ParseException(ErrorCodes.ERROR_CODE_INVALID_ICAO);
}
String website = NOAA_TAF_URL + pIcao.toUpperCase() // $NON-NLS-1$
+ ".TXT"; //$NON-NLS-1$
String website = NOAA_TAF_URL + icao.toUpperCase() // $NON-NLS-1$
+ ".TXT"; //$NON-NLS-1$
URL url = new URL(website);
try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
Expand All @@ -57,14 +58,15 @@ public TAF retrieveFromAirport(final String pIcao) throws IOException, URISyntax

/**
* Reformat the first line of the code.
* @param pCode the first line of the TAF event.
*
* @param code the first line of the TAF event.
* @return the formated taf code.
* @throws ParseException when an error occurs.
*/
protected String format(final String pCode) throws ParseException {
String[] lines = pCode.split("\n");
protected String format(final String code) throws ParseException {
String[] lines = code.split("\n");
if (!TAFParser.TAF.equals(lines[0].trim())) {
return pCode;
return code;
}
if ("AMD TAF".equals(lines[1].trim())) {
List<String> list = new ArrayList<>(Arrays.asList(lines));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public abstract class AbstractWeatherCodeServiceTest<T extends AbstractWeatherCode> {
protected abstract AbstractWeatherCodeService<T> getSut();


@Test
public void testRetrieveFromAirportInvalid() {
ParseException e = assertThrows(ParseException.class, () -> getSut().retrieveFromAirport("RandomIcao"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ protected AbstractWeatherCodeService<TAF> getSut() {
@Test
public void testFormatWithAMD() throws ParseException {
// Given a taf message with AMD on second line.
String message = "TAF \n" +
"AMD LFPG 100910Z 1009/1112 20015G25KT 9999 BKN035 \n" +
"TEMPO 1011/1019 26020G35KT 4000 SHRA BKN012TCU PROB30 \n" +
"TEMPO 1015/1019 27025G45KT 2500 TSRAGS SCT012CB \n" +
"BECMG 1021/1024 27010KT PROB30 \n" +
"TEMPO 1105/1107 BKN014 \n" +
"BECMG 1109/1111 34010KT";
String message =
"TAF \n" + "AMD LFPG 100910Z 1009/1112 20015G25KT 9999 BKN035 \n" + "TEMPO 1011/1019 26020G35KT 4000 SHRA BKN012TCU PROB30 \n" + "TEMPO 1015/1019 27025G45KT 2500 TSRAGS SCT012CB \n"
+ "BECMG 1021/1024 27010KT PROB30 \n" + "TEMPO 1105/1107 BKN014 \n" + "BECMG 1109/1111 34010KT";

String formatedString = "TAF AMD LFPG 100910Z 1009/1112 20015G25KT 9999 BKN035 \n" + "TEMPO 1011/1019 26020G35KT 4000 SHRA BKN012TCU PROB30 \n"
+ "TEMPO 1015/1019 27025G45KT 2500 TSRAGS SCT012CB \n" + "BECMG 1021/1024 27010KT PROB30 \n" + "TEMPO 1105/1107 BKN014 \n" + "BECMG 1109/1111 34010KT\n";
String formatedString =
"TAF AMD LFPG 100910Z 1009/1112 20015G25KT 9999 BKN035 \n" + "TEMPO 1011/1019 26020G35KT 4000 SHRA BKN012TCU PROB30 \n" + "TEMPO 1015/1019 27025G45KT 2500 TSRAGS SCT012CB \n"
+ "BECMG 1021/1024 27010KT PROB30 \n" + "TEMPO 1105/1107 BKN014 \n" + "BECMG 1109/1111 34010KT\n";

// When formating the message
String result = sut.format(message);
Expand All @@ -50,21 +47,11 @@ public void testFormatWithoutReformat() throws ParseException {

@Test
public void testFormat() throws ParseException {
String tafMessage = "TAF \n" +
"AMD TAF \n" +
"AMD LFPG 241332Z 2413/2518 01008KT 7000 BKN015 TX13/2414Z TN03/2505Z \n" +
"BECMG 2413/2415 BKN040 \n" +
"BECMG 2415/2417 CAVOK \n" +
"BECMG 2509/2511 BKN030 \n" +
"TEMPO 2514/2516 36015G25KT \n" +
"BECMG 2516/2518 CAVOK";
String tafMessage = "TAF \n" + "AMD TAF \n" + "AMD LFPG 241332Z 2413/2518 01008KT 7000 BKN015 TX13/2414Z TN03/2505Z \n" + "BECMG 2413/2415 BKN040 \n" + "BECMG 2415/2417 CAVOK \n"
+ "BECMG 2509/2511 BKN030 \n" + "TEMPO 2514/2516 36015G25KT \n" + "BECMG 2516/2518 CAVOK";

String formatted = "TAF AMD LFPG 241332Z 2413/2518 01008KT 7000 BKN015 TX13/2414Z TN03/2505Z \n" +
"BECMG 2413/2415 BKN040 \n" +
"BECMG 2415/2417 CAVOK \n" +
"BECMG 2509/2511 BKN030 \n" +
"TEMPO 2514/2516 36015G25KT \n" +
"BECMG 2516/2518 CAVOK\n";
String formatted = "TAF AMD LFPG 241332Z 2413/2518 01008KT 7000 BKN015 TX13/2414Z TN03/2505Z \n" + "BECMG 2413/2415 BKN040 \n" + "BECMG 2415/2417 CAVOK \n" + "BECMG 2509/2511 BKN030 \n"
+ "TEMPO 2514/2516 36015G25KT \n" + "BECMG 2516/2518 CAVOK\n";
// When formating the message
String result = sut.format(tafMessage);
// Then the 2 first lines are merged.
Expand Down

0 comments on commit 152c845

Please sign in to comment.