Skip to content

Commit

Permalink
refactor: more consistent enum convention
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdibi committed Oct 3, 2024
1 parent f97dcb0 commit 9a59850
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
package org.eclipse.kura.nm;

import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

import org.eclipse.kura.nm.enums.MMModemLocationSource;

public enum KuraModemGPSMode {

UNMANAGED,
MANAGED_GPS;
KURA_MODEM_GPS_MODE_UNMANAGED("kuraModemGpsModeUnmanaged"),
KURA_MODEM_GPS_MODE_MANAGED_GPS("kuraModemGpsModeManagedGps");

public static KuraModemGPSMode fromString(String mode) {
switch (mode) {
case "unmanaged":
return KuraModemGPSMode.UNMANAGED;
case "managed-gps":
return KuraModemGPSMode.MANAGED_GPS;
default:
throw new IllegalArgumentException(String.format("Unsupported modem GPS mode value: \"%s\"", mode));
}
private final String value;

private KuraModemGPSMode(String value) {
this.value = value;
}

public String getValue() {
return this.value;
}

public static Optional<KuraModemGPSMode> fromString(Optional<String> mode) {
if (mode.isPresent()) {
switch (mode.get()) {
case "unmanaged":
return Optional.of(KuraModemGPSMode.UNMANAGED);
case "managed-gps":
return Optional.of(KuraModemGPSMode.MANAGED_GPS);
default:
return Optional.empty();
public static KuraModemGPSMode fromString(String name) {
for (KuraModemGPSMode auth : KuraModemGPSMode.values()) {
if (auth.getValue().equals(name)) {
return auth;
}
} else {
return Optional.empty();
}

throw new IllegalArgumentException("Invalid modem GPS mode in snapshot: " + name);
}

public static Set<MMModemLocationSource> toMMModemLocationSources(KuraModemGPSMode mode) {
switch (mode) {
case UNMANAGED:
case KURA_MODEM_GPS_MODE_UNMANAGED:
return EnumSet.of(MMModemLocationSource.MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED);
case MANAGED_GPS:
case KURA_MODEM_GPS_MODE_MANAGED_GPS:
return EnumSet.of(MMModemLocationSource.MM_MODEM_LOCATION_SOURCE_GPS_RAW,
MMModemLocationSource.MM_MODEM_LOCATION_SOURCE_GPS_NMEA);
default:
throw new IllegalArgumentException(String.format("Unsupported modem GPS mode value: \"%s\"", mode));
}
}

public static Set<KuraModemGPSMode> fromMMModemLocationSources(Set<MMModemLocationSource> sources) {
Set<KuraModemGPSMode> modes = EnumSet.noneOf(KuraModemGPSMode.class);

if (sources.contains(MMModemLocationSource.MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) {
modes.add(KuraModemGPSMode.KURA_MODEM_GPS_MODE_UNMANAGED);
}

if (sources.contains(MMModemLocationSource.MM_MODEM_LOCATION_SOURCE_GPS_RAW)
&& sources.contains(MMModemLocationSource.MM_MODEM_LOCATION_SOURCE_GPS_NMEA)) {
modes.add(KuraModemGPSMode.KURA_MODEM_GPS_MODE_MANAGED_GPS);
}

return modes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected void setGPS(Optional<String> modemDevicePath, Optional<Boolean> enable

boolean isGPSSourceEnabled = enableGPS.isPresent() && enableGPS.get();
KuraModemGPSMode desiredGPSMode = gpsModeString.isPresent() ? KuraModemGPSMode.fromString(gpsModeString.get())
: KuraModemGPSMode.UNMANAGED;
: KuraModemGPSMode.KURA_MODEM_GPS_MODE_UNMANAGED;

Location modemLocation = this.dbusConnection.getRemoteObject(MM_BUS_NAME, modemDevicePath.get(),
Location.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public void applyShouldWorkWithModemWithEnabledGPSAndUnmanagedMode() throws DBus
givenNetworkConfigMapWith("net.interface.1-5.config.dhcpClient4.enabled", true);
givenNetworkConfigMapWith("net.interface.1-5.config.apn", "myAwesomeAPN");
givenNetworkConfigMapWith("net.interface.1-5.config.gpsEnabled", true);
givenNetworkConfigMapWith("net.interface.1-5.config.gpsMode", "unmanaged");
givenNetworkConfigMapWith("net.interface.1-5.config.gpsMode", "kuraModemGpsModeUnmanaged");
givenNetworkConfigMapWith("net.interface.1-5.config.resetTimeout", 0);

whenApplyIsCalledWith(this.netConfig);
Expand All @@ -679,7 +679,7 @@ public void applyShouldWorkWithModemWithEnabledGPSAndManagedMode() throws DBusEx
givenNetworkConfigMapWith("net.interface.1-5.config.dhcpClient4.enabled", true);
givenNetworkConfigMapWith("net.interface.1-5.config.apn", "myAwesomeAPN");
givenNetworkConfigMapWith("net.interface.1-5.config.gpsEnabled", true);
givenNetworkConfigMapWith("net.interface.1-5.config.gpsMode", "managed-gps");
givenNetworkConfigMapWith("net.interface.1-5.config.gpsMode", "kuraModemGpsModeManagedGps");
givenNetworkConfigMapWith("net.interface.1-5.config.resetTimeout", 0);

whenApplyIsCalledWith(this.netConfig);
Expand Down

0 comments on commit 9a59850

Please sign in to comment.