Skip to content

Commit

Permalink
Clean code in metarParser-parsers 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 7c25c3f commit 2d5c391
Show file tree
Hide file tree
Showing 69 changed files with 624 additions and 582 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public AirportSupplier() {
}

@Override
public Airport get(final String pIcao) {
public Airport get(final String string) {
AirportProvider provider;
if (airportLoader.iterator().hasNext()) {
provider = airportLoader.iterator().next();
} else {
provider = new DefaultAirportProvider();
}
return provider.getAirports().get(pIcao);
return provider.getAirports().get(string);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* @param <T> type of command to return.
* @author mivek
*/
@FunctionalInterface public interface Supplier<T> {
@FunctionalInterface
public interface Supplier<T> {

/**
* @param pString the string to parse.
* @param string the string to parse.
* @return the command able to parse the string.
*/
T get(String pString);
T get(String string);
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ public interface BaseWindCommand extends Command {
/**
* Sets the elements of the wind.
*
* @param pWind the wind element.
* @param pDirection the direction of the wind in degrees.
* @param pSpeed the speed of the wind
* @param pGust the speed of the gust if any
* @param pUnit the unit.
* @param wind the wind element.
* @param directionStr the direction of the wind in degrees.
* @param speed the speed of the wind
* @param gust the speed of the gust if any
* @param unit the unit.
*/
default void setWindElements(final Wind pWind, final String pDirection, final String pSpeed, final String pGust, final String pUnit) {
String direction = Converter.degreesToDirection(pDirection);
pWind.setDirection(direction);
default void setWindElements(final Wind wind, final String directionStr, final String speed, final String gust, final String unit) {
String direction = Converter.degreesToDirection(directionStr);
wind.setDirection(direction);
if (!direction.equals(Messages.getInstance().getString("Converter.VRB"))) {
pWind.setDirectionDegrees(Integer.parseInt(pDirection));
wind.setDirectionDegrees(Integer.parseInt(directionStr));
}
pWind.setSpeed(Integer.parseInt(pSpeed));
if (pGust != null) {
pWind.setGust(Integer.parseInt(pGust));
wind.setSpeed(Integer.parseInt(speed));
if (gust != null) {
wind.setGust(Integer.parseInt(gust));
}
if (pUnit == null) {
pWind.setUnit("KT");
if (unit == null) {
wind.setUnit("KT");
} else {
pWind.setUnit(pUnit);
wind.setUnit(unit);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ public final class CloudCommand implements Command {
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
Cloud c = parseCloud(pPart);
return pContainer.addCloud(c);
public boolean execute(final AbstractWeatherContainer container, final String part) {
Cloud c = parseCloud(part);
return container.addCloud(c);
}

/**
* This method parses the cloud part of the metar.
*
* @param pCloudString string with cloud elements.
* @param cloudString string with cloud elements.
* @return a decoded cloud with its quantity, its altitude and its type.
*/
protected Cloud parseCloud(final String pCloudString) {
protected Cloud parseCloud(final String cloudString) {
Cloud cloud = new Cloud();
String[] cloudPart = Regex.pregMatch(CLOUD_REGEX, pCloudString);
String[] cloudPart = Regex.pregMatch(CLOUD_REGEX, cloudString);
try {
CloudQuantity cq = CloudQuantity.valueOf(cloudPart[1]);
cloud.setQuantity(cq);
Expand All @@ -53,8 +53,8 @@ protected Cloud parseCloud(final String pCloudString) {
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(CLOUD_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(CLOUD_REGEX, input);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
public interface Command {

/**
* Handles the pPart and updates the pContainer.
* Handles the pqrt and updates the container.
*
* @param pContainer the container to update.
* @param pPart the string to parse.
* @param container the container to update.
* @param part the string to parse.
* @return true if the part has been properly handled false otherwise
*/
boolean execute(AbstractWeatherContainer pContainer, String pPart);
boolean execute(AbstractWeatherContainer container, String part);

/**
* @return the default return value of a command.
Expand All @@ -24,8 +24,8 @@ default boolean getReturnValue() {
}

/**
* @param pInput the input string to test.
* @param input the input string to test.
* @return true if the input can be handled by the command.
*/
boolean canParse(String pInput);
boolean canParse(String input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public CommonCommandSupplier() {
}

@Override
public Command get(final String pString) {
public Command get(final String string) {
for (Command command : commands) {
if (command.canParse(pString)) {
if (command.canParse(string)) {
return command;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public final class MainVisibilityCommand implements Command {
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
String[] matches = Regex.pregMatch(MAIN_VISIBILITY_REGEX, pPart);
if (pContainer.getVisibility() == null) {
pContainer.setVisibility(new Visibility());
public boolean execute(final AbstractWeatherContainer container, final String part) {
String[] matches = Regex.pregMatch(MAIN_VISIBILITY_REGEX, part);
if (container.getVisibility() == null) {
container.setVisibility(new Visibility());
}
pContainer.getVisibility().setMainVisibility(Converter.convertVisibility(matches[1]));
container.getVisibility().setMainVisibility(Converter.convertVisibility(matches[1]));
return getReturnValue();
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(MAIN_VISIBILITY_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(MAIN_VISIBILITY_REGEX, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public final class MainVisibilityNauticalMilesCommand implements Command {
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
String[] matches = Regex.pregMatch(MAIN_VISIBILITY_SM_REGEX, pPart);
if (pContainer.getVisibility() == null) {
pContainer.setVisibility(new Visibility());
public boolean execute(final AbstractWeatherContainer container, final String part) {
String[] matches = Regex.pregMatch(MAIN_VISIBILITY_SM_REGEX, part);
if (container.getVisibility() == null) {
container.setVisibility(new Visibility());
}
pContainer.getVisibility().setMainVisibility(matches[0]);
container.getVisibility().setMainVisibility(matches[0]);
return getReturnValue();
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(MAIN_VISIBILITY_SM_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(MAIN_VISIBILITY_SM_REGEX, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public final class MinimalVisibilityCommand implements Command {
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
String[] matches = Regex.pregMatch(MIN_VISIBILITY_REGEX, pPart);
pContainer.getVisibility().setMinVisibility(Integer.parseInt(matches[1].substring(0, 4)));
pContainer.getVisibility().setMinDirection(matches[1].substring(4));
public boolean execute(final AbstractWeatherContainer container, final String part) {
String[] matches = Regex.pregMatch(MIN_VISIBILITY_REGEX, part);
container.getVisibility().setMinVisibility(Integer.parseInt(matches[1].substring(0, 4)));
container.getVisibility().setMinDirection(matches[1].substring(4));
return getReturnValue();
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(MIN_VISIBILITY_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(MIN_VISIBILITY_REGEX, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public final class VerticalVisibilityCommand implements Command {
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
String[] matches = Regex.pregMatch(VERTICAL_VISIBILITY, pPart);
pContainer.setVerticalVisibility(100 * Integer.parseInt(matches[1]));
public boolean execute(final AbstractWeatherContainer container, final String part) {
String[] matches = Regex.pregMatch(VERTICAL_VISIBILITY, part);
container.setVerticalVisibility(100 * Integer.parseInt(matches[1]));
return getReturnValue();
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(VERTICAL_VISIBILITY, pInput);
public boolean canParse(final String input) {
return Regex.find(VERTICAL_VISIBILITY, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ public final class WindCommand implements BaseWindCommand {
* This method parses the wind part of the metar code. It parses the generic
* part.
*
* @param pStringWind a string with wind elements.
* @param stringWind a string with wind elements.
* @return a Wind element with the informations.
*/
protected Wind parseWind(final String pStringWind) {
protected Wind parseWind(final String stringWind) {
Wind wind = new Wind();
String[] windPart = Regex.pregMatch(WIND_REGEX, pStringWind);
String[] windPart = Regex.pregMatch(WIND_REGEX, stringWind);
setWindElements(wind, windPart[1], windPart[2], windPart[3], windPart[4]);
return wind;
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
Wind wind = parseWind(pPart);
pContainer.setWind(wind);
public boolean execute(final AbstractWeatherContainer container, final String part) {
Wind wind = parseWind(part);
container.setWind(wind);
return getReturnValue();
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(WIND_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(WIND_REGEX, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ public final class WindExtremeCommand implements Command {
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
parseExtremeWind(pContainer.getWind(), pPart);
public boolean execute(final AbstractWeatherContainer container, final String part) {
parseWindVariation(container.getWind(), part);
return getReturnValue();
}

/**
* Parses the wind.
*
* @param pWind the wind to update
* @param pExtremeWind String with extreme wind information
* @param wind the wind to update
* @param windVariation String with wind variation information
*/
protected void parseExtremeWind(final Wind pWind, final String pExtremeWind) {
String[] matches = Regex.pregMatch(WIND_EXTREME_REGEX, pExtremeWind);
pWind.setExtreme1(Integer.parseInt(matches[1]));
pWind.setExtreme2(Integer.parseInt(matches[2]));
protected void parseWindVariation(final Wind wind, final String windVariation) {
String[] matches = Regex.pregMatch(WIND_EXTREME_REGEX, windVariation);
wind.setMinVariation(Integer.parseInt(matches[1]));
wind.setMaxVariation(Integer.parseInt(matches[2]));
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(WIND_EXTREME_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(WIND_EXTREME_REGEX, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ public final class WindShearCommand implements BaseWindCommand {
}

@Override
public boolean execute(final AbstractWeatherContainer pContainer, final String pPart) {
WindShear windShear = parseWindShear(pPart);
pContainer.setWindShear(windShear);
public boolean execute(final AbstractWeatherContainer container, final String part) {
WindShear windShear = parseWindShear(part);
container.setWindShear(windShear);
return getReturnValue();
}

/**
* Parses the wind shear part.
*
* @param pStringWindShear the string to parse
* @param stringWindShear the string to parse
* @return a wind shear object.
*/
protected WindShear parseWindShear(final String pStringWindShear) {
protected WindShear parseWindShear(final String stringWindShear) {
WindShear wind = new WindShear();
String[] windPart = Regex.pregMatch(WIND_SHEAR_REGEX, pStringWindShear);
String[] windPart = Regex.pregMatch(WIND_SHEAR_REGEX, stringWindShear);
wind.setHeight(100 * Integer.parseInt(windPart[1]));
setWindElements(wind, windPart[2], windPart[3], windPart[4], windPart[5]);
return wind;
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(WIND_SHEAR_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(WIND_SHEAR_REGEX, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public final class AltimeterCommand implements Command {
}

@Override
public void execute(final Metar pMetar, final String pPart) {
String[] matches = Regex.pregMatch(ALTIMETER_REGEX, pPart);
pMetar.setAltimeter(Integer.parseInt(matches[1]));
public void execute(final Metar metar, final String part) {
String[] matches = Regex.pregMatch(ALTIMETER_REGEX, part);
metar.setAltimeter(Integer.parseInt(matches[1]));
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(ALTIMETER_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(ALTIMETER_REGEX, input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public final class AltimeterMecuryCommand implements Command {
}

@Override
public void execute(final Metar pMetar, final String pPart) {
String[] matches = Regex.pregMatch(ALTIMETER_MERCURY_REGEX, pPart);
public void execute(final Metar metar, final String part) {
String[] matches = Regex.pregMatch(ALTIMETER_MERCURY_REGEX, part);
double mercury = Double.parseDouble(matches[1]) / 100;
pMetar.setAltimeter((int) Converter.inchesMercuryToHPascal(mercury));
metar.setAltimeter((int) Converter.inchesMercuryToHPascal(mercury));
}

@Override
public boolean canParse(final String pInput) {
return Regex.find(ALTIMETER_MERCURY_REGEX, pInput);
public boolean canParse(final String input) {
return Regex.find(ALTIMETER_MERCURY_REGEX, input);
}
}
Loading

0 comments on commit 2d5c391

Please sign in to comment.