Refactor switch case

This commit is contained in:
Gold856
2025-03-31 17:53:11 -04:00
committed by Matt Morley
parent 89c0cc3a01
commit 7f7d80bc3b
4 changed files with 91 additions and 131 deletions

View File

@@ -136,10 +136,10 @@ public class VisionLED {
if (newLedModeRaw != currentLedMode.value) {
VisionLEDMode newLedMode =
switch (newLedModeRaw) {
case -1 -> newLedMode = VisionLEDMode.kDefault;
case 0 -> newLedMode = VisionLEDMode.kOff;
case 1 -> newLedMode = VisionLEDMode.kOn;
case 2 -> newLedMode = VisionLEDMode.kBlink;
case -1 -> VisionLEDMode.kDefault;
case 0 -> VisionLEDMode.kOff;
case 1 -> VisionLEDMode.kOn;
case 2 -> VisionLEDMode.kBlink;
default -> {
logger.warn("User supplied invalid LED mode, falling back to Default");
yield VisionLEDMode.kDefault;

View File

@@ -90,19 +90,16 @@ public class PipelineManager {
* @return The gotten settings of the pipeline whose index was provided.
*/
public CVPipelineSettings getPipelineSettings(int index) {
if (index < 0) {
switch (index) {
case DRIVERMODE_INDEX:
return driverModePipeline.getSettings();
case CAL_3D_INDEX:
return calibration3dPipeline.getSettings();
return switch (index) {
case DRIVERMODE_INDEX -> driverModePipeline.getSettings();
case CAL_3D_INDEX -> calibration3dPipeline.getSettings();
default -> {
for (var setting : userPipelineSettings) {
if (setting.pipelineIndex == index) yield setting;
}
yield null;
}
}
for (var setting : userPipelineSettings) {
if (setting.pipelineIndex == index) return setting;
}
return null;
};
}
/**
@@ -112,19 +109,16 @@ public class PipelineManager {
* @return the nickname of the pipeline whose index was provided.
*/
public String getPipelineNickname(int index) {
if (index < 0) {
switch (index) {
case DRIVERMODE_INDEX:
return driverModePipeline.getSettings().pipelineNickname;
case CAL_3D_INDEX:
return calibration3dPipeline.getSettings().pipelineNickname;
return switch (index) {
case DRIVERMODE_INDEX -> driverModePipeline.getSettings().pipelineNickname;
case CAL_3D_INDEX -> calibration3dPipeline.getSettings().pipelineNickname;
default -> {
for (var setting : userPipelineSettings) {
if (setting.pipelineIndex == index) yield setting.pipelineNickname;
}
yield null;
}
}
for (var setting : userPipelineSettings) {
if (setting.pipelineIndex == index) return setting.pipelineNickname;
}
return null;
};
}
/**
@@ -156,17 +150,12 @@ public class PipelineManager {
*/
public CVPipeline getCurrentPipeline() {
updatePipelineFromRequested();
if (currentPipelineIndex < 0) {
switch (currentPipelineIndex) {
case CAL_3D_INDEX:
return calibration3dPipeline;
case DRIVERMODE_INDEX:
return driverModePipeline;
}
}
// Just return the current user pipeline, we're not on aa built-in one
return currentUserPipeline;
return switch (currentPipelineIndex) {
case CAL_3D_INDEX -> calibration3dPipeline;
case DRIVERMODE_INDEX -> driverModePipeline;
// Just return the current user pipeline, we're not on a built-in one
default -> currentUserPipeline;
};
}
/**
@@ -339,40 +328,22 @@ public class PipelineManager {
}
private CVPipelineSettings createSettingsForType(PipelineType type, String nickname) {
switch (type) {
case Reflective -> {
var added = new ReflectivePipelineSettings();
added.pipelineNickname = nickname;
return added;
}
case ColoredShape -> {
var added = new ColoredShapePipelineSettings();
added.pipelineNickname = nickname;
return added;
}
case AprilTag -> {
var added = new AprilTagPipelineSettings();
added.pipelineNickname = nickname;
return added;
}
case Aruco -> {
var added = new ArucoPipelineSettings();
added.pipelineNickname = nickname;
return added;
}
case ObjectDetection -> {
var added = new ObjectDetectionPipelineSettings();
added.pipelineNickname = nickname;
return added;
}
case Calib3d, DriverMode -> {
logger.error("Got invalid pipeline type: " + type);
return null;
}
CVPipelineSettings settings =
switch (type) {
case Reflective -> new ReflectivePipelineSettings();
case ColoredShape -> new ColoredShapePipelineSettings();
case AprilTag -> new AprilTagPipelineSettings();
case Aruco -> new ArucoPipelineSettings();
case ObjectDetection -> new ObjectDetectionPipelineSettings();
case Calib3d, DriverMode -> {
logger.error("Got invalid pipeline type: " + type);
yield null;
}
};
if (settings != null) {
settings.pipelineNickname = nickname;
}
// This can never happen, this is here to satisfy the compiler.
throw new IllegalStateException("Got impossible pipeline type: " + type);
return settings;
}
private void addPipelineInternal(CVPipelineSettings settings) {

View File

@@ -89,12 +89,8 @@ public class VisionModuleChangeSubscriber extends DataChangeSubscriber {
var newPropValue = change.getNewPropValue();
var currentSettings = change.getCurrentSettings();
var originContext = change.getOriginContext();
boolean handled = true;
switch (propName) {
case "pipelineName" -> {
newPipelineNickname((String) newPropValue);
continue;
}
case "pipelineName" -> newPipelineNickname((String) newPropValue);
case "newPipelineInfo" -> newPipelineInfo((Pair<String, PipelineType>) newPropValue);
case "deleteCurrPipeline" -> deleteCurrPipeline();
case "changePipeline" -> changePipeline((Integer) newPropValue);
@@ -120,45 +116,43 @@ public class VisionModuleChangeSubscriber extends DataChangeSubscriber {
parentModule.saveAndBroadcastAll();
}
case "isDriverMode" -> parentModule.setDriverMode((Boolean) newPropValue);
default -> handled = false;
}
// special case for camera settables
if (propName.startsWith("camera")) {
var propMethodName = "set" + propName.replace("camera", "");
var methods = parentModule.visionSource.getSettables().getClass().getMethods();
for (var method : methods) {
if (method.getName().equalsIgnoreCase(propMethodName)) {
try {
method.invoke(parentModule.visionSource.getSettables(), newPropValue);
} catch (Exception e) {
logger.error("Failed to invoke camera settable method: " + method.getName(), e);
default -> {
// special case for camera settables
if (propName.startsWith("camera")) {
var propMethodName = "set" + propName.replace("camera", "");
var methods = parentModule.visionSource.getSettables().getClass().getMethods();
for (var method : methods) {
if (method.getName().equalsIgnoreCase(propMethodName)) {
try {
method.invoke(parentModule.visionSource.getSettables(), newPropValue);
} catch (Exception e) {
logger.error("Failed to invoke camera settable method: " + method.getName(), e);
}
}
}
}
try {
setProperty(currentSettings, propName, newPropValue);
logger.trace("Set prop " + propName + " to value " + newPropValue);
} catch (NoSuchFieldException | IllegalAccessException e) {
logger.error(
"Could not set prop "
+ propName
+ " with value "
+ newPropValue
+ " on "
+ currentSettings
+ " | "
+ e.getClass().getSimpleName(),
e);
} catch (Exception e) {
logger.error("Unknown exception when setting PSC prop!", e);
}
parentModule.saveAndBroadcastSelective(originContext, propName, newPropValue);
}
}
if (!handled) {
try {
setProperty(currentSettings, propName, newPropValue);
logger.trace("Set prop " + propName + " to value " + newPropValue);
} catch (NoSuchFieldException | IllegalAccessException e) {
logger.error(
"Could not set prop "
+ propName
+ " with value "
+ newPropValue
+ " on "
+ currentSettings
+ " | "
+ e.getClass().getSimpleName(),
e);
} catch (Exception e) {
logger.error("Unknown exception when setting PSC prop!", e);
}
}
parentModule.saveAndBroadcastSelective(originContext, propName, newPropValue);
}
getSettingChanges().clear();
} finally {
@@ -230,9 +224,8 @@ public class VisionModuleChangeSubscriber extends DataChangeSubscriber {
switch (offsetOperation) {
case CLEAR -> curAdvSettings.offsetSinglePoint = new Point();
case TAKE_SINGLE -> curAdvSettings.offsetSinglePoint = newPoint;
case TAKE_FIRST_DUAL, TAKE_SECOND_DUAL -> {
logger.warn("Dual point operation in single point mode");
}
case TAKE_FIRST_DUAL, TAKE_SECOND_DUAL ->
logger.warn("Dual point operation in single point mode");
}
}
case Dual -> {
@@ -253,14 +246,10 @@ public class VisionModuleChangeSubscriber extends DataChangeSubscriber {
curAdvSettings.offsetDualPointB = newPoint;
curAdvSettings.offsetDualPointBArea = latestTarget.getArea();
}
case TAKE_SINGLE -> {
logger.warn("Single point operation in dual point mode");
}
case TAKE_SINGLE -> logger.warn("Single point operation in dual point mode");
}
}
case None -> {
logger.warn("Robot offset point operation requested, but no offset mode set");
}
case None -> logger.warn("Robot offset point operation requested, but no offset mode set");
}
}

View File

@@ -134,17 +134,16 @@ public class TargetCalculations {
Point camCenterPoint,
DualOffsetValues dualOffsetValues,
RobotOffsetPointMode offsetMode) {
switch (offsetMode) {
case None:
default:
return camCenterPoint;
case Single:
return switch (offsetMode) {
case None -> camCenterPoint;
case Single -> {
if (offsetPoint.x == 0 && offsetPoint.y == 0) {
return camCenterPoint;
yield camCenterPoint;
} else {
return offsetPoint;
yield offsetPoint;
}
case Dual:
}
case Dual -> {
var resultPoint = new Point();
var lineValues = dualOffsetValues.getLineValues();
var offsetSlope = lineValues.getFirst();
@@ -152,8 +151,9 @@ public class TargetCalculations {
resultPoint.x = (offsetPoint.x - offsetIntercept) / offsetSlope;
resultPoint.y = (offsetPoint.y * offsetSlope) + offsetIntercept;
return resultPoint;
}
yield resultPoint;
}
};
}
public static double getAspectRatio(RotatedRect rect, boolean isLandscape) {