From 7f7d80bc3bee06dd7211f943186661c1197de7cf Mon Sep 17 00:00:00 2001 From: Gold856 <117957790+Gold856@users.noreply.github.com> Date: Mon, 31 Mar 2025 17:53:11 -0400 Subject: [PATCH] Refactor switch case --- .../common/hardware/VisionLED.java | 8 +- .../vision/processes/PipelineManager.java | 107 +++++++----------- .../VisionModuleChangeSubscriber.java | 87 +++++++------- .../vision/target/TargetCalculations.java | 20 ++-- 4 files changed, 91 insertions(+), 131 deletions(-) diff --git a/photon-core/src/main/java/org/photonvision/common/hardware/VisionLED.java b/photon-core/src/main/java/org/photonvision/common/hardware/VisionLED.java index 52fb48a97..2a1af5086 100644 --- a/photon-core/src/main/java/org/photonvision/common/hardware/VisionLED.java +++ b/photon-core/src/main/java/org/photonvision/common/hardware/VisionLED.java @@ -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; diff --git a/photon-core/src/main/java/org/photonvision/vision/processes/PipelineManager.java b/photon-core/src/main/java/org/photonvision/vision/processes/PipelineManager.java index 85adb4cbd..9d8b6cc32 100644 --- a/photon-core/src/main/java/org/photonvision/vision/processes/PipelineManager.java +++ b/photon-core/src/main/java/org/photonvision/vision/processes/PipelineManager.java @@ -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) { diff --git a/photon-core/src/main/java/org/photonvision/vision/processes/VisionModuleChangeSubscriber.java b/photon-core/src/main/java/org/photonvision/vision/processes/VisionModuleChangeSubscriber.java index dfbe2808e..55b173736 100644 --- a/photon-core/src/main/java/org/photonvision/vision/processes/VisionModuleChangeSubscriber.java +++ b/photon-core/src/main/java/org/photonvision/vision/processes/VisionModuleChangeSubscriber.java @@ -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) 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"); } } diff --git a/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java b/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java index 22c92ae2c..1cdcd31f5 100644 --- a/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java +++ b/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java @@ -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) {