From 75e9ca71968c599bbae278574eda615112c92cac Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 1 Oct 2023 23:37:55 +0200 Subject: [PATCH] Code cleanups --- .../verifier/TestAdvancedAerospace.java | 62 ++++------------ .../src/megamek/common/verifier/TestAero.java | 41 ++++------- .../src/megamek/common/verifier/TestMech.java | 6 +- .../src/megamek/common/verifier/TestTank.java | 71 +++++-------------- 4 files changed, 51 insertions(+), 129 deletions(-) diff --git a/megamek/src/megamek/common/verifier/TestAdvancedAerospace.java b/megamek/src/megamek/common/verifier/TestAdvancedAerospace.java index 0ac73895535..56e7b25428e 100644 --- a/megamek/src/megamek/common/verifier/TestAdvancedAerospace.java +++ b/megamek/src/megamek/common/verifier/TestAdvancedAerospace.java @@ -32,7 +32,7 @@ public class TestAdvancedAerospace extends TestAero { private final Jumpship vessel; - public enum CapitalArmor{ + public enum CapitalArmor { PRIMITIVE(EquipmentType.T_ARMOR_PRIMITIVE_AERO, false), STANDARD(EquipmentType.T_ARMOR_AEROSPACE, false), CLAN_STANDARD(EquipmentType.T_ARMOR_AEROSPACE, true), @@ -47,12 +47,12 @@ public enum CapitalArmor{ * The type, corresponding to types defined in * EquipmentType. */ - public int type; + public final int type; /** * Denotes whether this armor is Clan or not. */ - public boolean isClan; + public final boolean isClan; CapitalArmor(int t, boolean c) { type = t; @@ -405,26 +405,6 @@ public Entity getEntity() { return vessel; } - @Override - public boolean isTank() { - return false; - } - - @Override - public boolean isMech() { - return false; - } - - @Override - public boolean isAero() { - return true; - } - - @Override - public boolean isSmallCraft() { - return false; - } - @Override public boolean isAdvancedAerospace() { return true; @@ -657,20 +637,14 @@ public boolean hasDoubleHeatSinks() { @Override public String printWeightControls() { - StringBuffer retVal = new StringBuffer(StringUtil.makeLength( - "Control Systems:", getPrintSize() - 5)); - retVal.append(makeWeightString(getWeightControls())); - retVal.append("\n"); - return retVal.toString(); + return StringUtil.makeLength("Control Systems:", getPrintSize() - 5) + + makeWeightString(getWeightControls()) + "\n"; } @Override public String printWeightFuel() { - StringBuffer retVal = new StringBuffer(StringUtil.makeLength( - "Fuel: ", getPrintSize() - 5)); - retVal.append(makeWeightString(getWeightFuel())); - retVal.append("\n"); - return retVal.toString(); + return StringUtil.makeLength("Fuel: ", getPrintSize() - 5) + + makeWeightString(getWeightFuel()) + "\n"; } @Override @@ -682,16 +656,11 @@ public Jumpship getAdvancedAerospace() { return vessel; } - @Override - public String printArmorLocProp(int loc, int wert) { - return " is greater than " + wert + "!"; - } - /** * Checks to see if this unit has valid armor assignment. * - * @param buff - * @return + * @param buff The StringBuffer to receive error descriptions + * @return False when something is invalid, true otherwise */ @Override public boolean correctArmor(StringBuffer buff) { @@ -711,16 +680,16 @@ public boolean correctArmor(StringBuffer buff) { /** * Checks that the heatsink type is a legal value. * - * @param buff - * @return + * @param buff The StringBuffer to receive error descriptions + * @return False when something is invalid, true otherwise */ @Override public boolean correctHeatSinks(StringBuffer buff) { if ((vessel.getHeatType() != Aero.HEAT_SINGLE) && (vessel.getHeatType() != Aero.HEAT_DOUBLE)) { - buff.append("Invalid heatsink type! Valid types are " - + Aero.HEAT_SINGLE + " and " + Aero.HEAT_DOUBLE - + ". Found " + vessel.getHeatType() + "."); + buff.append("Invalid heatsink type! Valid types are " + Aero.HEAT_SINGLE + " and " + + Aero.HEAT_DOUBLE + ". Found ") + .append(vessel.getHeatType()).append("."); return false; } return true; @@ -741,8 +710,7 @@ public boolean correctEntity(StringBuffer buff, int ammoTechLvl) { if (getCountHeatSinks() < weightFreeHeatSinks(vessel)) { buff.append("Heat Sinks:\n"); buff.append(" Total " + getCountHeatSinks() + "\n"); - buff.append(" Required " + weightFreeHeatSinks(vessel) - + "\n"); + buff.append(" Required " + weightFreeHeatSinks(vessel) + "\n"); correct = false; } diff --git a/megamek/src/megamek/common/verifier/TestAero.java b/megamek/src/megamek/common/verifier/TestAero.java index 1817cb74c1e..dccede956ba 100644 --- a/megamek/src/megamek/common/verifier/TestAero.java +++ b/megamek/src/megamek/common/verifier/TestAero.java @@ -40,7 +40,7 @@ * @author Reinhard Vicinus */ public class TestAero extends TestEntity { - private Aero aero = null; + private Aero aero; /** * An enumeration that keeps track of the legal armors for Aerospace and @@ -50,7 +50,7 @@ public class TestAero extends TestEntity { * * @author arlith */ - public static enum AeroArmor { + public enum AeroArmor { STANDARD(EquipmentType.T_ARMOR_STANDARD, 0, 0, false), CLAN_FERRO_ALUM(EquipmentType.T_ARMOR_ALUM, 1, 1, true), FERRO_LAMELLOR(EquipmentType.T_ARMOR_FERRO_LAMELLOR, 2, 1, true), @@ -580,19 +580,13 @@ public String printWeightMisc() { @Override public String printWeightControls() { - StringBuffer retVal = new StringBuffer(StringUtil.makeLength( - aero.getCockpitTypeString() + ":", getPrintSize() - 5)); - retVal.append(makeWeightString(getWeightControls())); - retVal.append("\n"); - return retVal.toString(); + return StringUtil.makeLength(aero.getCockpitTypeString() + ":", getPrintSize() - 5) + + makeWeightString(getWeightControls()) + "\n"; } public String printWeightFuel() { - StringBuffer retVal = new StringBuffer(StringUtil.makeLength( - "Fuel: ", getPrintSize() - 5)); - retVal.append(makeWeightString(getWeightFuel())); - retVal.append("\n"); - return retVal.toString(); + return StringUtil.makeLength("Fuel: ", getPrintSize() - 5) + + makeWeightString(getWeightFuel()) + "\n"; } public Aero getAero() { @@ -936,11 +930,12 @@ public boolean hasMismatchedLateralWeapons(StringBuffer buff) { * conventional fighter, or fixed wing support vehicle */ public static boolean isValidAeroLocation(EquipmentType eq, int location, @Nullable StringBuffer buffer) { + if (buffer == null) { + buffer = new StringBuffer(); + } if (eq instanceof AmmoType) { if (location != Aero.LOC_FUSELAGE) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted in the fuselage.\n"); - } + buffer.append(eq.getName()).append(" must be mounted in the fuselage.\n"); return false; } } else if (eq instanceof MiscType) { @@ -952,31 +947,23 @@ public static boolean isValidAeroLocation(EquipmentType eq, int location, @Nulla || eq.hasFlag(MiscType.F_PPC_CAPACITOR) || eq.hasFlag(MiscType.F_RISC_LASER_PULSE_MODULE)) && (location >= Aero.LOC_WINGS)) { if (location != Aero.LOC_FUSELAGE) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted in a location with a firing arc.\n"); - } + buffer.append(eq.getName()).append(" must be mounted in a location with a firing arc.\n"); return false; } } else if ((eq.hasFlag(MiscType.F_BLUE_SHIELD) || eq.hasFlag(MiscType.F_LIFTHOIST) || (eq.hasFlag(MiscType.F_CASE) && !eq.isClan())) && (location != Aero.LOC_FUSELAGE)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted in the fuselage.\n"); - } + buffer.append(eq.getName()).append(" must be mounted in the fuselage.\n"); return false; } } else if (eq instanceof WeaponType) { if ((((WeaponType) eq).getAmmoType() == AmmoType.T_GAUSS_HEAVY) && (location != Aero.LOC_NOSE) && (location != Aero.LOC_AFT)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted in the nose or aft.\n"); - } + buffer.append(eq.getName()).append(" must be mounted in the nose or aft.\n"); return false; } if (!eq.hasFlag(WeaponType.F_C3M) && !eq.hasFlag(WeaponType.F_C3MBS) && !eq.hasFlag(WeaponType.F_TAG) && (location == Aero.LOC_FUSELAGE)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted in a location with a firing arc.\n"); - } + buffer.append(eq.getName()).append(" must be mounted in a location with a firing arc.\n"); return false; } } diff --git a/megamek/src/megamek/common/verifier/TestMech.java b/megamek/src/megamek/common/verifier/TestMech.java index 9044ad15c6d..84279c65836 100755 --- a/megamek/src/megamek/common/verifier/TestMech.java +++ b/megamek/src/megamek/common/verifier/TestMech.java @@ -120,7 +120,7 @@ public static List legalArmorsFor(long etype, boolean industrial, return legalArmors; } - private Mech mech = null; + private Mech mech; public TestMech(Mech mech, TestEntityOption option, String fileString) { super(option, mech.getEngine(), getArmor(mech), getStructure(mech)); @@ -483,7 +483,7 @@ public boolean criticalSlotsAllocated(Entity entity, Mounted mounted, Vector allocation, StringBuffer buff) { int location = mounted.getLocation(); EquipmentType et = mounted.getType(); - int criticals = 0; + int criticals; if (et instanceof MiscType) { criticals = calcMiscCrits((MiscType) et, mounted.getSize()); } else { @@ -506,7 +506,7 @@ public boolean criticalSlotsAllocated(Entity entity, Mounted mounted, } if ((et instanceof WeaponType) && mounted.isSplit()) { - int secCound = 0; + int secCound; for (int locations = 0; locations < entity.locations(); locations++) { if (locations == location) { continue; diff --git a/megamek/src/megamek/common/verifier/TestTank.java b/megamek/src/megamek/common/verifier/TestTank.java index 95a7473cb0f..c780111c3b6 100755 --- a/megamek/src/megamek/common/verifier/TestTank.java +++ b/megamek/src/megamek/common/verifier/TestTank.java @@ -647,10 +647,8 @@ public StringBuffer printSlotCalculation() { buff.append(StringUtil.makeLength(mount.getName(), 30)); buff.append(mount.getType().getTankSlots(tank)).append("\n"); addedCargo = true; - continue; - } else { - continue; } + continue; } if (!((mount.getType() instanceof AmmoType) || Arrays.asList( EquipmentType.armorNames).contains( @@ -669,6 +667,7 @@ public StringBuffer printSlotCalculation() { int type = tank.getArmorType(1); switch (type) { case EquipmentType.T_ARMOR_FERRO_FIBROUS: + case EquipmentType.T_ARMOR_REACTIVE: if (TechConstants.isClan(tank.getArmorTechLevel(1))) { armorSlots++; } else { @@ -687,13 +686,6 @@ public StringBuffer printSlotCalculation() { case EquipmentType.T_ARMOR_STEALTH: armorSlots += 2; break; - case EquipmentType.T_ARMOR_REACTIVE: - if (TechConstants.isClan(tank.getArmorTechLevel(1))) { - armorSlots++; - } else { - armorSlots += 2; - } - break; default: break; } @@ -891,10 +883,11 @@ public boolean hasIllegalEquipmentCombinations(StringBuffer buff) { */ public static boolean isValidTankLocation(Tank tank, EquipmentType eq, int location, @Nullable StringBuffer buffer) { + if (buffer == null) { + buffer = new StringBuffer(); + } if (isBodyEquipment(eq) && (location != Tank.LOC_BODY)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted in the body.\n"); - } + buffer.append(eq.getName()).append(" must be mounted in the body.\n"); return false; } final boolean isRearLocation; @@ -914,96 +907,70 @@ public static boolean isValidTankLocation(Tank tank, EquipmentType eq, int locat || eq.hasFlag(MiscType.F_MANIPULATOR) || eq.hasFlag(MiscType.F_FLUID_SUCTION_SYSTEM) || eq.hasFlag(MiscType.F_LIGHT_FLUID_SUCTION_SYSTEM) || eq.hasFlag(MiscType.F_SPRAYER)) && (tank instanceof VTOL) && (location == VTOL.LOC_ROTOR)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" cannot be mounted in the rotor.\n"); - } + buffer.append(eq.getName()).append(" cannot be mounted in the rotor.\n"); return false; } if ((eq.hasFlag(MiscType.F_HARJEL) || eq.hasFlag(MiscType.F_LIGHT_FLUID_SUCTION_SYSTEM) || eq.hasFlag(MiscType.F_SPRAYER) || (eq.hasFlag(MiscType.F_LIFTHOIST) && !(tank instanceof VTOL))) && (location == Tank.LOC_BODY)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" cannot be mounted in the body.\n"); - } + buffer.append(eq.getName()).append(" cannot be mounted in the body.\n"); return false; } if (eq.hasFlag(MiscType.F_BULLDOZER) && ((location != Tank.LOC_FRONT) && (location != Tank.LOC_REAR))) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted on the front or rear.\n"); - } + buffer.append(eq.getName()).append(" must be mounted on the front or rear.\n"); return false; } if (eq.hasFlag(MiscType.F_CLUB) && eq.hasSubType(MiscType.S_PILE_DRIVER) && (location != Tank.LOC_FRONT) && !isRearLocation) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted on the front or rear.\n"); - } + buffer.append(eq.getName()).append(" must be mounted on the front or rear.\n"); return false; } if (eq.hasFlag(MiscType.F_CLUB) && eq.hasSubType(MiscType.S_WRECKING_BALL) && !isTurretLocation) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted on a turret.\n"); - } + buffer.append(eq.getName()).append(" must be mounted on a turret.\n"); return false; } if (eq.hasFlag(MiscType.F_CLUB) && !eq.hasSubType(MiscType.S_PILE_DRIVER | MiscType.S_SPOT_WELDER | MiscType.S_WRECKING_BALL) && (location != Tank.LOC_FRONT) && !isRearLocation && !isTurretLocation) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted on the front, rear, or turret.\n"); - } + buffer.append(eq.getName()).append(" must be mounted on the front, rear, or turret.\n"); return false; } if (eq.hasFlag(MiscType.F_LADDER) && ((location == Tank.LOC_FRONT) || isRearLocation || ((tank instanceof VTOL) && (location == VTOL.LOC_ROTOR)))) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted on the side or turret.\n"); - } + buffer.append(eq.getName()).append(" must be mounted on the side or turret.\n"); return false; } if (eq.hasFlag(MiscType.F_MAST_MOUNT) && (location != VTOL.LOC_ROTOR)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted on the rotor.\n"); - } + buffer.append(eq.getName()).append(" must be mounted on the rotor.\n"); return false; } // The minesweeper is also permitted in the front-side/rear-side location for "particularly // large vehicles" (TacOps, 326) but it is not clear how large this needs to be. Superheavy? // multi-hex large naval support? if (eq.hasFlag(MiscType.F_MINESWEEPER) && (location != Tank.LOC_FRONT) && !isRearLocation) { - if (buffer != null) { - buffer.append(eq.getName()).append(" must be mounted on the front or rear.\n"); - } + buffer.append(eq.getName()).append(" must be mounted on the front or rear.\n"); return false; } } else if (eq instanceof WeaponType) { if ((((WeaponType) eq).getAmmoType() == AmmoType.T_GAUSS_HEAVY) && (location != Tank.LOC_FRONT) && !isRearLocation) { - if (buffer != null) { - buffer.append(eq.getName()).append(" cannot be mounted on the sides or turret.\n"); - } + buffer.append(eq.getName()).append(" cannot be mounted on the sides or turret.\n"); return false; } if ((((WeaponType) eq).getAmmoType() == AmmoType.T_IGAUSS_HEAVY) && isTurretLocation) { - if (buffer != null) { - buffer.append(eq.getName()).append(" cannot be mounted on a turret.\n"); - } + buffer.append(eq.getName()).append(" cannot be mounted on a turret.\n"); return false; } if (!eq.hasFlag(WeaponType.F_C3M) && !eq.hasFlag(WeaponType.F_C3MBS) && !eq.hasFlag(WeaponType.F_TAG) && (location == Tank.LOC_BODY)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" cannot be mounted in the body.\n"); - } + buffer.append(eq.getName()).append(" cannot be mounted in the body.\n"); return false; } if ((tank instanceof VTOL) && (location == VTOL.LOC_ROTOR) && !eq.hasFlag(WeaponType.F_TAG)) { - if (buffer != null) { - buffer.append(eq.getName()).append(" cannot be mounted in the rotor.\n"); - } + buffer.append(eq.getName()).append(" cannot be mounted in the rotor.\n"); return false; } }