Skip to content

Commit 221edf6

Browse files
authored
Merge pull request #6571 from rjhancock/no-clone-method
No Clone Method fixes.
2 parents 7193b9a + 6c99899 commit 221edf6

File tree

4 files changed

+68
-461
lines changed

4 files changed

+68
-461
lines changed

MekHQ/src/mekhq/campaign/universe/RATGeneratorConnector.java

+30-31
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import megamek.client.ratgenerator.FactionRecord;
3939
import megamek.client.ratgenerator.MissionRole;
4040
import megamek.client.ratgenerator.ModelRecord;
41+
import megamek.client.ratgenerator.Parameters;
4142
import megamek.client.ratgenerator.RATGenerator;
4243
import megamek.client.ratgenerator.UnitTable;
4344
import megamek.common.EntityMovementMode;
@@ -47,8 +48,7 @@
4748
import megamek.logging.MMLogger;
4849

4950
/**
50-
* Provides access to RATGenerator through the AbstractUnitGenerator and thus
51-
* the IUnitGenerator interface.
51+
* Provides access to RATGenerator through the AbstractUnitGenerator and thus the IUnitGenerator interface.
5252
*
5353
* @author Neoancient
5454
*/
@@ -70,9 +70,8 @@ public RATGeneratorConnector(final int year) {
7070
}
7171

7272
private @Nullable UnitTable findTable(final String faction, final int unitType, final int weightClass,
73-
final int year, final int quality,
74-
final Collection<EntityMovementMode> movementModes,
75-
final Collection<MissionRole> missionRoles) {
73+
final int year, final int quality, final Collection<EntityMovementMode> movementModes,
74+
final Collection<MissionRole> missionRoles) {
7675
final FactionRecord factionRecord = Factions.getInstance().getFactionRecordOrFallback(faction);
7776
if (factionRecord == null) {
7877
return null;
@@ -82,17 +81,25 @@ public RATGeneratorConnector(final int year) {
8281
if (weightClass >= 0) {
8382
weightClasses.add(weightClass);
8483
}
85-
return UnitTable.findTable(factionRecord, unitType, year, rating, weightClasses, ModelRecord.NETWORK_NONE,
86-
movementModes, missionRoles, 2, factionRecord);
84+
return UnitTable.findTable(factionRecord,
85+
unitType,
86+
year,
87+
rating,
88+
weightClasses,
89+
ModelRecord.NETWORK_NONE,
90+
movementModes,
91+
missionRoles,
92+
2,
93+
factionRecord);
8794
}
8895

8996
/**
90-
* Helper function that extracts the string-based unit rating from the given
91-
* int-based unit-rating
92-
* for the given faction.
97+
* Helper function that extracts the string-based unit rating from the given int-based unit-rating for the given
98+
* faction.
9399
*
94100
* @param factionRecord Faction record
95101
* @param quality Unit quality number
102+
*
96103
* @return Unit quality string
97104
*/
98105
public static String getFactionSpecificRating(final FactionRecord factionRecord, final int quality) {
@@ -116,29 +123,23 @@ public boolean isSupportedUnitType(final int unitType) {
116123

117124
@Override
118125
public @Nullable MekSummary generate(final String faction, final int unitType, final int weightClass,
119-
final int year, final int quality,
120-
final Collection<EntityMovementMode> movementModes,
121-
final Collection<MissionRole> missionRoles,
122-
@Nullable Predicate<MekSummary> filter) {
126+
final int year, final int quality, final Collection<EntityMovementMode> movementModes,
127+
final Collection<MissionRole> missionRoles, @Nullable Predicate<MekSummary> filter) {
123128
final UnitTable table = findTable(faction, unitType, weightClass, year, quality, movementModes, missionRoles);
124129
return (table == null) ? null : table.generateUnit((filter == null) ? null : filter::test);
125130
}
126131

127132
@Override
128133
public List<MekSummary> generate(final int count, final String faction, final int unitType, final int weightClass,
129-
final int year, final int quality,
130-
final Collection<EntityMovementMode> movementModes,
131-
final Collection<MissionRole> missionRoles,
132-
@Nullable Predicate<MekSummary> filter) {
134+
final int year, final int quality, final Collection<EntityMovementMode> movementModes,
135+
final Collection<MissionRole> missionRoles, @Nullable Predicate<MekSummary> filter) {
133136
final UnitTable table = findTable(faction, unitType, weightClass, year, quality, movementModes, missionRoles);
134137
return (table == null) ? new ArrayList<>() : table.generateUnits(count, (filter == null) ? null : filter::test);
135138
}
136139

137140
/**
138-
* Generates a list of mek summaries from a RAT determined by the given faction,
139-
* quality and other parameters.
140-
* We force a fallback to try to ensure that something is generated if the
141-
* parents have any possible units to generate,
141+
* Generates a list of mek summaries from a RAT determined by the given faction, quality and other parameters. We
142+
* force a fallback to try to ensure that something is generated if the parents have any possible units to generate,
142143
* as that is the normally expected behaviour for MekHQ OpFor generation.
143144
*
144145
* @param count How many units to generate
@@ -148,15 +149,13 @@ public List<MekSummary> generate(final int count, final String faction, final in
148149
public List<MekSummary> generate(final int count, final UnitGeneratorParameters parameters) {
149150
final UnitTable table = findOpForTable(parameters);
150151
return table.generateUnits(count,
151-
(parameters.getFilter() == null) ? null : ms -> parameters.getFilter().test(ms));
152+
(parameters.getFilter() == null) ? null : ms -> parameters.getFilter().test(ms));
152153
}
153154

154155
/**
155-
* Generates a single mek summary from a RAT determined by the given faction,
156-
* quality and other parameters.
157-
* We force a fallback to try to ensure that something is generated if the
158-
* parents have any possible units to generate,
159-
* as that is the normally expected behaviour for MekHQ OpFor generation.
156+
* Generates a single mek summary from a RAT determined by the given faction, quality and other parameters. We force
157+
* a fallback to try to ensure that something is generated if the parents have any possible units to generate, as
158+
* that is the normally expected behaviour for MekHQ OpFor generation.
160159
*
161160
* @param parameters RATGenerator parameters
162161
*/
@@ -167,15 +166,15 @@ public List<MekSummary> generate(final int count, final UnitGeneratorParameters
167166
}
168167

169168
/**
170-
* This finds a unit table for OpFor generation. It falls back using the parent
171-
* faction to try to ensure there are
169+
* This finds a unit table for OpFor generation. It falls back using the parent faction to try to ensure there are
172170
* units in the unit table, so an OpFor is generated.
173171
*
174172
* @param unitParameters the base parameters to find the table using.
173+
*
175174
* @return the unit table to use in generating OpFor mek summaries
176175
*/
177176
private UnitTable findOpForTable(final UnitGeneratorParameters unitParameters) {
178-
final UnitTable.Parameters parameters = unitParameters.getRATGeneratorParameters();
177+
final Parameters parameters = unitParameters.getRATGeneratorParameters();
179178
UnitTable table = UnitTable.findTable(parameters);
180179
if (!table.hasUnits()) {
181180
// Do Parent Factions Fallbacks to try to ensure units can be generated, at a

MekHQ/src/mekhq/campaign/universe/UnitGeneratorParameters.java

+38-33
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,16 @@
3535
import megamek.client.ratgenerator.FactionRecord;
3636
import megamek.client.ratgenerator.MissionRole;
3737
import megamek.client.ratgenerator.ModelRecord;
38-
import megamek.client.ratgenerator.UnitTable.Parameters;
38+
import megamek.client.ratgenerator.Parameters;
3939
import megamek.common.EntityMovementMode;
4040
import megamek.common.MekSummary;
4141
import mekhq.campaign.mission.AtBDynamicScenarioFactory;
4242

4343
/**
44-
* Data structure that contains parameters relevant to unit generation via the
45-
* IUnitGenerator interface
46-
* and is capable of translating itself to
47-
* megamek.client.ratgenerator.parameters
44+
* Data structure that contains parameters relevant to unit generation via the IUnitGenerator interface and is capable
45+
* of translating itself to megamek.client.ratgenerator.parameters
4846
*
4947
* @author NickAragua
50-
*
5148
*/
5249
public class UnitGeneratorParameters {
5350
private String faction;
@@ -69,32 +66,31 @@ public UnitGeneratorParameters() {
6966
*/
7067
@Override
7168
public UnitGeneratorParameters clone() {
72-
UnitGeneratorParameters newParams = new UnitGeneratorParameters();
73-
74-
newParams.setFaction(faction);
75-
newParams.setUnitType(unitType);
76-
newParams.setWeightClass(weightClass);
77-
newParams.setYear(year);
78-
newParams.setQuality(quality);
79-
newParams.setFilter(filter);
80-
81-
Collection<EntityMovementMode> newModes = new ArrayList<>();
82-
for (EntityMovementMode movementMode : movementModes) {
83-
newModes.add(movementMode);
84-
}
85-
86-
newParams.setMovementModes(newModes);
87-
88-
for (MissionRole missionRole : missionRoles) {
89-
newParams.addMissionRole(missionRole);
69+
try {
70+
UnitGeneratorParameters unitGeneratorParameters = (UnitGeneratorParameters) super.clone();
71+
unitGeneratorParameters.setFaction(faction);
72+
unitGeneratorParameters.setUnitType(unitType);
73+
unitGeneratorParameters.setWeightClass(weightClass);
74+
unitGeneratorParameters.setYear(year);
75+
unitGeneratorParameters.setQuality(quality);
76+
unitGeneratorParameters.setFilter(filter);
77+
78+
Collection<EntityMovementMode> newModes = new ArrayList<>(movementModes);
79+
80+
unitGeneratorParameters.setMovementModes(newModes);
81+
82+
for (MissionRole missionRole : missionRoles) {
83+
unitGeneratorParameters.addMissionRole(missionRole);
84+
}
85+
86+
return unitGeneratorParameters;
87+
} catch (CloneNotSupportedException e) {
88+
return null;
9089
}
91-
92-
return newParams;
9390
}
9491

9592
/**
96-
* Translate the contents of this data structure into a
97-
* megamek.client.ratgenerator.Parameters object
93+
* Translate the contents of this data structure into a megamek.client.ratgenerator.Parameters object
9894
*
9995
* @return
10096
*/
@@ -107,11 +103,16 @@ public Parameters getRATGeneratorParameters() {
107103
weightClasses.add(getWeightClass());
108104
}
109105

110-
Parameters params = new Parameters(fRec, getUnitType(), getYear(), rating, weightClasses,
111-
ModelRecord.NETWORK_NONE,
112-
getMovementModes(), getMissionRoles(), 2, fRec);
113-
114-
return params;
106+
return new Parameters(fRec,
107+
getUnitType(),
108+
getYear(),
109+
rating,
110+
weightClasses,
111+
ModelRecord.NETWORK_NONE,
112+
getMovementModes(),
113+
getMissionRoles(),
114+
2,
115+
fRec);
115116
}
116117

117118
public String getFaction() {
@@ -174,6 +175,10 @@ public void setMissionRoles(Collection<MissionRole> missionRoles) {
174175
this.missionRoles = missionRoles;
175176
}
176177

178+
/**
179+
* @deprecated no indicated uses.
180+
*/
181+
@Deprecated(since = "0.50.05", forRemoval = true)
177182
public void clearMissionRoles() {
178183
missionRoles.clear();
179184
}

0 commit comments

Comments
 (0)