Skip to content

Commit

Permalink
refactor: Config migration sonarlint issues (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelsJP authored Nov 26, 2024
2 parents f852308 + 9e6dbb7 commit 235586e
Show file tree
Hide file tree
Showing 20 changed files with 232 additions and 892 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public ElevationProperties() {
}

public ElevationProperties(String ignored) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

public Path getCachePath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public BuildProperties() {
}

public BuildProperties(String ignored) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

public void merge(BuildProperties other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public EncoderOptionsProperties() {
}

public EncoderOptionsProperties(String ignored) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public ExecutionProperties() {
}

public ExecutionProperties(String ignored) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public ExtendedStorageProperties() {

@JsonCreator
public ExtendedStorageProperties(String ignoredEmpty) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

public void merge(ExtendedStorageProperties other) {
Expand Down Expand Up @@ -159,111 +160,138 @@ public void initialize(ExtendedStorageName storageName) {
enabled = true;
}

// Avoid initializing this multiple times
final Path emptyPath = Path.of("");

ArrayList<String> nonNullableProperties = new ArrayList<>();

if (storageName == ExtendedStorageName.HEAVY_VEHICLE) {
if (restrictions == null) {
restrictions = true;
}
nonNullableProperties.add("restrictions");
} else if (storageName == ExtendedStorageName.HILL_INDEX) {
if (maximumSlope == null) {
this.maximumSlope = null;
}
nonNullableProperties.add("maximum_slope");
} else if (storageName == ExtendedStorageName.ROAD_ACCESS_RESTRICTIONS) {
if (useForWarnings == null) {
this.useForWarnings = true;
}
nonNullableProperties.add("use_for_warnings");
} else if (storageName == ExtendedStorageName.WHEELCHAIR) {
if (kerbsOnCrossings == null) {
this.kerbsOnCrossings = true;
}
nonNullableProperties.add("kerbs_on_crossings");
switch (storageName) {
case HEAVY_VEHICLE:
initializeHeavyVehicle(nonNullableProperties);
break;
case HILL_INDEX:
initializeHillIndex(nonNullableProperties);
break;
case ROAD_ACCESS_RESTRICTIONS:
initializeRoadAccessRestrictions(nonNullableProperties);
break;
case WHEELCHAIR:
initializeWheelchair(nonNullableProperties);
break;
case NOISE_INDEX, GREEN_INDEX, SHADOW_INDEX, CSV:
initializeFilepath(nonNullableProperties, storageName);
break;
case BORDERS:
initializeBorders(nonNullableProperties, storageName);
break;
case HERE_TRAFFIC:
initializeHereTraffic(nonNullableProperties, storageName);
break;
default:
}
setAllPropertiesToNull(nonNullableProperties);
}

if (storageName == ExtendedStorageName.NOISE_INDEX || storageName == ExtendedStorageName.GREEN_INDEX || storageName == ExtendedStorageName.SHADOW_INDEX || storageName == ExtendedStorageName.CSV) {
if (filepath == null || filepath.equals(emptyPath)) {
LOGGER.warn("Storage " + storageName + " is missing filepath. Disabling storage.");
enabled = false;
filepath = Path.of("");
} else {
filepath = filepath.toAbsolutePath();
}
nonNullableProperties.add("filepath");
} else if (storageName == ExtendedStorageName.BORDERS) {
if (boundaries == null || boundaries.equals(emptyPath)) {
LOGGER.warn("Storage " + storageName + " is missing boundaries. Disabling storage.");
enabled = false;
boundaries = Path.of("");
} else {
boundaries = boundaries.toAbsolutePath();
}
if (ids == null || ids.equals(emptyPath)) {
LOGGER.warn("Storage " + storageName + " is missing ids. Disabling storage.");
enabled = false;
ids = Path.of("");
} else {
ids = ids.toAbsolutePath();
}
if (openborders == null || openborders.equals(emptyPath)) {
LOGGER.warn("Storage " + storageName + " is missing openborders. Disabling storage.");
enabled = false;
openborders = Path.of("");
} else {
openborders = openborders.toAbsolutePath();
}
nonNullableProperties.add("boundaries");
nonNullableProperties.add("ids");
nonNullableProperties.add("openborders");
} else if (storageName == ExtendedStorageName.HERE_TRAFFIC) {
// Here traffic if streets, ref_pattern or pattern_15min is not set, disable storage
if (radius == null) {
this.radius = 150;
}

if (outputLog == null) {
this.outputLog = false;
}

if (logLocation == null || logLocation.equals(emptyPath)) {
// TODO: check if we want to keep this functionality
this.logLocation = Path.of("./here_matching.log");
}

if (streets == null || streets.equals(emptyPath)) {
LOGGER.warn("Storage " + storageName + " is missing streets. Disabling storage.");
enabled = false;
streets = Path.of("");
} else {
streets = streets.toAbsolutePath();
}
if (refPattern == null || refPattern.equals(emptyPath)) {
LOGGER.warn("Storage " + storageName + " is missing ref_pattern. Disabling storage.");
enabled = false;
refPattern = Path.of("");
} else {
refPattern = refPattern.toAbsolutePath();
}
if (pattern15Min == null || pattern15Min.equals(emptyPath)) {
LOGGER.warn("Storage " + storageName + " is missing pattern_15min. Disabling storage.");
enabled = false;
pattern15Min = Path.of("");
} else {
pattern15Min = pattern15Min.toAbsolutePath();
}
nonNullableProperties.add("radius");
nonNullableProperties.add("output_log");
nonNullableProperties.add("log_location");
nonNullableProperties.add("streets");
nonNullableProperties.add("ref_pattern");
nonNullableProperties.add("pattern_15min");
private void initializeHeavyVehicle(ArrayList<String> nonNullableProperties) {
if (restrictions == null) {
restrictions = true;
}
setAllPropertiesToNull(nonNullableProperties);
nonNullableProperties.add("restrictions");
}

private void initializeHillIndex(ArrayList<String> nonNullableProperties) {
nonNullableProperties.add("maximum_slope");
}

private void initializeRoadAccessRestrictions(ArrayList<String> nonNullableProperties) {
if (useForWarnings == null) {
this.useForWarnings = true;
}
nonNullableProperties.add("use_for_warnings");
}

private void initializeWheelchair(ArrayList<String> nonNullableProperties) {
if (kerbsOnCrossings == null) {
this.kerbsOnCrossings = true;
}
nonNullableProperties.add("kerbs_on_crossings");
}

private void initializeFilepath(ArrayList<String> nonNullableProperties, ExtendedStorageName storageName) {
final Path emptyPath = Path.of("");
if (filepath == null || filepath.equals(emptyPath)) {
LOGGER.warn("Storage %s is missing filepath. Disabling storage.".formatted(storageName));
enabled = false;
filepath = Path.of("");
} else {
filepath = filepath.toAbsolutePath();
}
nonNullableProperties.add("filepath");
}

private void initializeBorders(ArrayList<String> nonNullableProperties, ExtendedStorageName storageName) {
final Path emptyPath = Path.of("");
if (boundaries == null || boundaries.equals(emptyPath)) {
LOGGER.warn("Storage %s is missing boundaries. Disabling storage.".formatted(storageName));
enabled = false;
boundaries = Path.of("");
} else {
boundaries = boundaries.toAbsolutePath();
}
if (ids == null || ids.equals(emptyPath)) {
LOGGER.warn("Storage %s is missing ids. Disabling storage.".formatted(storageName));
enabled = false;
ids = Path.of("");
} else {
ids = ids.toAbsolutePath();
}
if (openborders == null || openborders.equals(emptyPath)) {
LOGGER.warn("Storage %s is missing openborders. Disabling storage.".formatted(storageName));
enabled = false;
openborders = Path.of("");
} else {
openborders = openborders.toAbsolutePath();
}
nonNullableProperties.add("boundaries");
nonNullableProperties.add("ids");
nonNullableProperties.add("openborders");
}

private void initializeHereTraffic(ArrayList<String> nonNullableProperties, ExtendedStorageName storageName) {
final Path emptyPath = Path.of("");
if (radius == null) {
this.radius = 150;
}
if (outputLog == null) {
this.outputLog = false;
}
if (logLocation == null || logLocation.equals(emptyPath)) {
this.logLocation = Path.of("./here_matching.log");
}
if (streets == null || streets.equals(emptyPath)) {
LOGGER.warn("Storage %s is missing streets. Disabling storage.".formatted(storageName));
enabled = false;
streets = Path.of("");
} else {
streets = streets.toAbsolutePath();
}
if (refPattern == null || refPattern.equals(emptyPath)) {
LOGGER.warn("Storage %s is missing ref_pattern. Disabling storage.".formatted(storageName));
enabled = false;
refPattern = Path.of("");
} else {
refPattern = refPattern.toAbsolutePath();
}
if (pattern15Min == null || pattern15Min.equals(emptyPath)) {
LOGGER.warn("Storage %s is missing pattern_15min. Disabling storage.".formatted(storageName));
enabled = false;
pattern15Min = Path.of("");
} else {
pattern15Min = pattern15Min.toAbsolutePath();
}
nonNullableProperties.add("radius");
nonNullableProperties.add("output_log");
nonNullableProperties.add("log_location");
nonNullableProperties.add("streets");
nonNullableProperties.add("ref_pattern");
nonNullableProperties.add("pattern_15min");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public PreparationProperties() {
}

public PreparationProperties(String ignored) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

@JsonIgnore
Expand Down Expand Up @@ -91,6 +92,7 @@ public static class LMProperties extends CHProperties {
private Integer landmarks;

@JsonIgnore
@Override
public boolean isEmpty() {
return super.isEmpty() && landmarks == null;
}
Expand All @@ -108,6 +110,7 @@ public static class CoreProperties extends LMProperties {
private String lmsets;

@JsonIgnore
@Override
public boolean isEmpty() {
return super.isEmpty() && lmsets == null;
}
Expand All @@ -125,6 +128,7 @@ public static class FastIsochroneProperties extends CHProperties {
private Integer maxcellnodes;

@JsonIgnore
@Override
public boolean isEmpty() {
return super.isEmpty() && maxcellnodes == null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public RepoProperties() {
}

public RepoProperties(String ignored) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

public boolean isEmpty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public ServiceProperties() {
}

public ServiceProperties(String ignored) {
// This constructor is used to create an empty object for the purpose of ignoring it in the JSON serialization.
}

public void merge(ServiceProperties other) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package org.heigit.ors.config.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.Map;
import java.util.Objects;

public class NonEmptyMapFilter {

@Override
public boolean equals(Object other) {
if (other == null) return true;
try {
Method isEmptyMethod = other.getClass().getDeclaredMethod("isEmpty");
if (isEmptyMethod.invoke(other).equals(true)) {
return true;
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
// Ignore
if (other instanceof Map) {
return ((Map<?, ?>) other).isEmpty();
}
return false;
}

public int hashCode() {
return Objects.hash(this.getClass());
}
}
Loading

0 comments on commit 235586e

Please sign in to comment.