- Make everything final when you can, except for parameters and local variables (only make these final when they really should not be changed). Obviously don't bother making methods final if the class is final. Use the builder pattern in situations where it initially seems nicer to make things mutable and call loads of setX methods.
Note that this isn't a particularly good way of doing this as it doesn't offer the flexibility required for editing the cache, and is clearly quite verbose. However, it serves as an example of how the pattern works.
ItemDefinitionBuilder
Code:
package tld.removed.old.fs.archive.config.build;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import tld.removed.old.fs.archive.build.ConfigBuilder;
import tld.removed.old.fs.archive.config.ItemDefinition;
/**
* A {@link ConfigBuilder} for {@link ItemDefinition}s.
*
* @author Major
*/
public final class ItemDefinitionBuilder extends ConfigBuilder<ItemDefinition> {
/**
* The default equipment model ids.
*/
protected static final List<Integer> DEFAULT_EQUIP_IDS = Arrays.asList(-1, -1, -1);
/**
* The default ground scale values.
*/
protected static final List<Integer> DEFAULT_GROUND_SCALES = Arrays.asList(128, 128, 128);
/**
* The default head piece ids.
*/
protected static final List<Integer> DEFAULT_HEAD_PIECES = Arrays.asList(-1, -1);
/**
* The description of this item.
*/
protected String description = "null";
/**
* The female primary, secondary, and tertiary equipment ids of this item.
*/
protected List<Integer> femaleEquipIds = new ArrayList<>(DEFAULT_EQUIP_IDS);
/**
* The y-translation for the female equipment piece of this item.
*/
protected int femaleEquipTranslation = 0;
/**
* The female head piece ids.
*/
protected List<Integer> femaleHeadPieces = new ArrayList<>(DEFAULT_HEAD_PIECES);
/**
* The ground menu actions of this item.
*/
protected List<String> groundMenuActions = new ArrayList<>(5);
/**
* The x, y, and z ground scale values of this item.
*/
protected List<Integer> groundScales = new ArrayList<>(DEFAULT_GROUND_SCALES);
/**
* The inventory menu actions of this item.
*/
protected List<String> inventoryMenuActions = new ArrayList<>(5);
/**
* The light ambience of this item.
*/
protected int lightAmbience = 0;
/**
* The light diffusion of this item.
*/
protected int lightDiffusion = 0;
/**
* The male primary, secondary, and tertiary equipment ids of this item.
*/
protected List<Integer> maleEquipIds = new ArrayList<>(DEFAULT_EQUIP_IDS);
/**
* The y-translation for the male equipment piece of this item.
*/
protected int maleEquipTranslation = 0;
/**
* The male head piece ids.
*/
protected List<Integer> maleHeadPieces = new ArrayList<>(DEFAULT_HEAD_PIECES);
/**
* The members flag of this item.
*/
protected boolean members = false;
/**
* The id of this item's model.
*/
protected int modelId = 0;
/**
* The name of this item.
*/
protected String name = "null";
/**
* The note info id of this item.
*/
protected int noteInfoId = -1;
/**
* The note template id of this item.
*/
protected int noteTemplateId = -1;
/**
* The map of original model colours to replacement colours for this item.
*/
protected Map<Integer, Integer> replacementColours = new HashMap<>(3);
/**
* The camera roll of this item's sprite.
*/
protected int spriteCameraRoll = 0;
/**
* The sprite camera yaw of this item.
*/
protected int spriteCameraYaw = 0;
/**
* The pitch of this item's sprite.
*/
protected int spritePitch = 0;
/**
* The scale of this item's sprite.
*/
protected int spriteScale = 2000;
/**
* The x-translation of this item's sprite.
*/
protected int spriteTranslateX = 0;
/**
* The y-translation of this item's sprite.
*/
protected int spriteTranslateY = 0;
/**
* The stackable flag of this item.
*/
protected boolean stackable = false;
/**
* The stack amounts of this item.
*/
protected List<Integer> stackAmounts = new ArrayList<>();
/**
* The stack model ids of this item.
*/
protected List<Integer> stackModelIds = new ArrayList<>();
/**
* The team id of this item.
*/
protected int team = 0;
/**
* The value of this item.
*/
protected int value = 1;
/**
* Creates the item definition builder.
*
* @param id The id of the item.
*/
protected ItemDefinitionBuilder(int id) {
super(id);
}
/**
* Builds and returns {@link ItemDefinition}.
*
* @return The item definition.
*/
@Override
public ItemDefinition build() {
return new ItemDefinition(description, femaleEquipIds, femaleEquipTranslation, femaleHeadPieces,
groundMenuActions, groundScales, id, inventoryMenuActions, lightAmbience, lightDiffusion, maleEquipIds,
maleEquipTranslation, maleHeadPieces, members, modelId, name, noteInfoId, noteTemplateId,
replacementColours, spriteCameraRoll, spriteCameraYaw, spritePitch, spriteScale, spriteTranslateX,
spriteTranslateY, stackable, stackAmounts, stackModelIds, team, value);
}
/**
* Sets the description of this item.
*
* @param description The description.
*/
public final void setDescription(String description) {
this.description = description;
}
/**
* Sets a female equip id of this item.
*
* @param index The index.
* @param femaleEquipId The female equip id.
*/
public final void setFemaleEquipId(int index, int femaleEquipId) {
femaleEquipIds.add(femaleEquipId);
}
/**
* Sets the female equip translation of this item.
*
* @param femaleEquipTranslation The female equip translation.
*/
public final void setFemaleEquipTranslation(int femaleEquipTranslation) {
this.femaleEquipTranslation = femaleEquipTranslation;
}
/**
* Sets a ground scale value. TODO use an enum for direction.
*
* @param direction The direction.
* @param scale The scale.
*/
public final void setGroundScale(int direction, int scale) {
groundScales.add(scale);
}
/**
* Sets a female head piece of the item.
*
* @param index The index
* @param femaleHeadPiece The female head piece.
*/
public final void setFemaleHeadPiece(int index, int femaleHeadPiece) {
femaleHeadPieces.add(femaleHeadPiece);
}
/**
* Sets the ground menu action at the specified index.
*
* @param index The index.
* @param action The action string.
*/
public final void setGroundAction(int index, String action) {
groundMenuActions.add(action);
}
/**
* Sets the inventory menu action at the specified index.
*
* @param index The action index.
* @param action The action string.
*/
public final void setInventoryAction(int index, String action) {
inventoryMenuActions.add(action);
}
/**
* Sets the light ambience of this item.
*
* @param lightAmbience The light ambience.
*/
public final void setLightAmbience(int lightAmbience) {
this.lightAmbience = lightAmbience;
}
/**
* Sets the light diffusion of this item.
*
* @param lightDiffusion The light diffusion.
*/
public final void setLightDiffusion(int lightDiffusion) {
this.lightDiffusion = lightDiffusion;
}
/**
* Sets a male equip id of this item.
*
* @param index The index.
* @param maleEquipId The male equip id.
*/
public final void setMaleEquipId(int index, int maleEquipId) {
maleEquipIds.add(maleEquipId);
}
/**
* Sets the male equip translation of this item.
*
* @param maleEquipTranslation The male equip translation.
*/
public final void setMaleEquipTranslation(int maleEquipTranslation) {
this.maleEquipTranslation = maleEquipTranslation;
}
/**
* Sets a male head piece of the item.
*
* @param maleHeadPiece The male head piece.
*/
public final void setMaleHeadPiece(int index, int maleHeadPiece) {
maleHeadPieces.add(maleHeadPiece);
}
/**
* Sets the members flag of this item.
*
* @param members The members flag.
*/
public final void setMembers(boolean members) {
this.members = members;
}
/**
* Sets the model id of this item.
*
* @param modelId The model id.
*/
public final void setModelId(int modelId) {
this.modelId = modelId;
}
/**
* Sets the name of this item.
*
* @param name The name.
*/
public final void setName(String name) {
this.name = name;
}
/**
* Sets the note info id of this item.
*
* @param noteInfoId The note info id.
*/
public final void setNoteInfoId(int noteInfoId) {
this.noteInfoId = noteInfoId;
}
/**
* Sets the note template id of this item.
*
* @param noteTemplateId The note template id.
*/
public final void setNoteTemplateId(int noteTemplateId) {
this.noteTemplateId = noteTemplateId;
}
/**
* Sets the replacement model colours of this item.
*
* @param replacementColours The replacement model colours.
*/
public final void setReplacementColours(Map<Integer, Integer> replacementColours) {
this.replacementColours = replacementColours;
}
/**
* Sets the sprite camera roll of this item.
*
* @param spriteCameraRoll The sprite camera roll.
*/
public final void setSpriteCameraRoll(int spriteCameraRoll) {
this.spriteCameraRoll = spriteCameraRoll;
}
/**
* Sets the sprite camera yaw of this item.
*
* @param spriteCameraYaw The sprite camera yaw.
*/
public final void setSpriteCameraYaw(int spriteCameraYaw) {
this.spriteCameraYaw = spriteCameraYaw;
}
/**
* Sets the sprite pitch of this item.
*
* @param spritePitch The sprite pitch.
*/
public final void setSpritePitch(int spritePitch) {
this.spritePitch = spritePitch;
}
/**
* Sets the sprite scale of this item.
*
* @param spriteScale The sprite scale.
*/
public final void setSpriteScale(int spriteScale) {
this.spriteScale = spriteScale;
}
/**
* Sets the sprite x translation of this item.
*
* @param spriteTranslateX The sprite x translation.
*/
public final void setSpriteTranslateX(int spriteTranslateX) {
this.spriteTranslateX = spriteTranslateX;
}
/**
* Sets the sprite y translation of this item.
*
* @param spriteTranslateY The sprite y translation.
*/
public final void setSpriteTranslateY(int spriteTranslateY) {
this.spriteTranslateY = spriteTranslateY;
}
/**
* Sets the stackable flag of this item.
*
* @param stackable The stackable flag.
*/
public final void setStackable(boolean stackable) {
this.stackable = stackable;
}
/**
* Sets a stack amount of this item.
*
* @param index The index.
* @param stackAmount The stack amount.
*/
public final void setStackAmount(int index, int stackAmount) {
stackAmounts.add(stackAmount);
}
/**
* Sets a stack model id of this item.
*
* @param index The index.
* @param stackModelId The stack model id.
*/
public final void setStackModelIds(int index, int stackModelId) {
stackModelIds.add(stackModelId);
}
/**
* Sets the team of this item.
*
* @param team The team.
*/
public final void setTeam(int team) {
this.team = team;
}
/**
* Sets the value of this item.
*
* @param value The value.
*/
public final void setValue(int value) {
this.value = value;
}
}
ItemDefinition
Code:
package tld.removed.old.fs.archive.config;
import java.util.List;
import java.util.Map;
import tld.removed.old.fs.archive.Definition;
/**
* A definition for an item ('object'). Note that the model-related stuff should really be in a separate class (see the
* massive constructor of this one), but is kept here for the example.
*
* @author Major
*/
public final class ItemDefinition extends Definition {
/**
* The description of this item.
*/
private final String description;
/**
* The female primary, secondary, and tertiary equipment ids of this item.
*/
private final List<Integer> femaleEquipIds;
/**
* The y-translation for the female equipment piece of this item.
*/
private final int femaleEquipTranslation;
/**
* The female head piece ids.
*/
private final List<Integer> femaleHeadPieces;
/**
* The ground menu actions of this item.
*/
private final List<String> groundMenuActions;
/**
* The x, y, and z ground scales of this item.
*/
private final List<Integer> groundScales;
/**
* The inventory menu actions of this item.
*/
private final List<String> inventoryMenuActions;
/**
* The light ambience of this item.
*/
private final int lightAmbience;
/**
* The light diffusion of this item.
*/
private final int lightDiffusion;
/**
* The male primary, secondary, and tertiary equipment ids of this item.
*/
private final List<Integer> maleEquipIds;
/**
* The y-translation for the male equipment piece of this item.
*/
private final int maleEquipTranslation;
/**
* The male head piece ids.
*/
private final List<Integer> maleHeadPieces;
/**
* The members flag of this item.
*/
private final boolean members;
/**
* The id of this item's model.
*/
private final int modelId;
/**
* The name of this item.
*/
private final String name;
/**
* The note info id of this item.
*/
private final int noteInfoId;
/**
* The noted template id of this item.
*/
private final int noteTemplateId;
/**
* The map of original model colours to their replacements for this item.
*/
private final Map<Integer, Integer> replacementColours;
/**
* The camera roll of this item's sprite.
*/
private final int spriteCameraRoll;
/**
* The sprite camera yaw of this item.
*/
private final int spriteCameraYaw;
/**
* The pitch of this item's sprite.
*/
private final int spritePitch;
/**
* The scale of this item's sprite.
*/
private final int spriteScale;
/**
* The x-translation of this item's sprite.
*/
private final int spriteTranslateX;
/**
* The y-translation of this item's sprite.
*/
private final int spriteTranslateY;
/**
* The stackable flag of this item.
*/
private final boolean stackable;
/**
* The stack amounts of this item.
*/
private final List<Integer> stackAmounts;
/**
* The stack model ids of this item.
*/
private final List<Integer> stackModelIds;
/**
* The team id of this item.
*/
private final int team;
/**
* The value of this item.
*/
private final int value;
/**
* Creates the item definition. See the comment at the top of this class if you agree that it is stupidly big.
*/
public ItemDefinition(String description, List<Integer> femaleEquipIds, int femaleEquipTranslation,
List<Integer> femaleHeadPieces, List<String> groundMenuActions, List<Integer> groundScales, int id,
List<String> inventoryMenuActions, int lightAmbience, int lightDiffusion, List<Integer> maleEquipIds,
int maleEquipTranslation, List<Integer> maleHeadPieces, boolean members, int modelId, String name,
int noteInfoId, int noteTemplateId, Map<Integer, Integer> replacementColours, int spriteCameraRoll,
int spriteCameraYaw, int spritePitch, int spriteScale, int spriteTranslateX, int spriteTranslateY,
boolean stackable, List<Integer> stackAmounts, List<Integer> stackModelIds, int team, int value) {
this.description = description;
this.femaleEquipIds = femaleEquipIds;
this.femaleEquipTranslation = femaleEquipTranslation;
this.femaleHeadPieces = femaleHeadPieces;
this.groundMenuActions = groundMenuActions;
this.groundScales = groundScales;
this.inventoryMenuActions = inventoryMenuActions;
this.lightAmbience = lightAmbience;
this.lightDiffusion = lightDiffusion;
this.maleEquipIds = maleEquipIds;
this.maleEquipTranslation = maleEquipTranslation;
this.maleHeadPieces = maleHeadPieces;
this.members = members;
this.modelId = modelId;
this.name = name;
this.noteInfoId = noteInfoId;
this.noteTemplateId = noteTemplateId;
this.replacementColours = replacementColours;
this.spriteCameraRoll = spriteCameraRoll;
this.spriteCameraYaw = spriteCameraYaw;
this.spritePitch = spritePitch;
this.spriteScale = spriteScale;
this.spriteTranslateX = spriteTranslateX;
this.spriteTranslateY = spriteTranslateY;
this.stackable = stackable;
this.stackAmounts = stackAmounts;
this.stackModelIds = stackModelIds;
this.team = team;
this.value = value;
}
/**
* Gets the description.
*
* @return The description.
*/
public String getDescription() {
return description;
}
/**
* Gets the {@link List} of female equip ids.
*
* @return The female equip ids.
*/
public List<Integer> getFemaleEquipIds() {
return femaleEquipIds;
}
/**
* Gets the female equip translation.
*
* @return The female equip translation.
*/
public int getFemaleEquipTranslation() {
return femaleEquipTranslation;
}
/**
* Gets the {@link List} of female head pieces.
*
* @return The female head pieces.
*/
public List<Integer> getFemaleHeadPieces() {
return femaleHeadPieces;
}
/**
* Gets the {@link List} of ground menu actions.
*
* @return The ground menu actions.
*/
public List<String> getGroundMenuActions() {
return groundMenuActions;
}
/**
* Gets the {@link List} of ground scale values.
*
* @return The ground scale values.
*/
public List<Integer> getGroundScales() {
return groundScales;
}
/**
* Gets the {@link List} of inventory menu actions.
*
* @return The inventory menu actions.
*/
public List<String> getInventoryMenuActions() {
return inventoryMenuActions;
}
/**
* Gets the light ambience.
*
* @return The light ambience.
*/
public int getLightAmbience() {
return lightAmbience;
}
/**
* Gets the light diffusion.
*
* @return The light diffusion.
*/
public int getLightDiffusion() {
return lightDiffusion;
}
/**
* Gets the {@link List] of male equip ids.
*
* @return The male equip ids.
*/
public List<Integer> getMaleEquipIds() {
return maleEquipIds;
}
/**
* Gets the male equip translation.
*
* @return The male equip translation.
*/
public int getMaleEquipTranslation() {
return maleEquipTranslation;
}
/**
* Gets the {@link List} of male head pieces.
*
* @return The male head pieces.
*/
public List<Integer> getMaleHeadPieces() {
return maleHeadPieces;
}
/**
* Gets the model id.
*
* @return The model id.
*/
public int getModelId() {
return modelId;
}
/**
* Gets the name.
*
* @return The name.
*/
public String getName() {
return name;
}
/**
* Gets the note info id.
*
* @return The note info id.
*/
public int getNoteInfoId() {
return noteInfoId;
}
/**
* Gets the note template id.
*
* @return The note template id.
*/
public int getNoteTemplateId() {
return noteTemplateId;
}
/**
* Gets the {@link Map} of original model colours to replacement colours.
*
* @return The replacement colours.
*/
public Map<Integer, Integer> getReplacementColours() {
return replacementColours;
}
/**
* Gets the sprite camera roll.
*
* @return The sprite camera roll.
*/
public int getSpriteCameraRoll() {
return spriteCameraRoll;
}
/**
* Gets the sprite camera yaw.
*
* @return The sprite camera yaw.
*/
public int getSpriteCameraYaw() {
return spriteCameraYaw;
}
/**
* Gets the sprite pitch.
*
* @return The sprite pitch.
*/
public int getSpritePitch() {
return spritePitch;
}
/**
* Gets the sprite scale.
*
* @return The sprite scale.
*/
public int getSpriteScale() {
return spriteScale;
}
/**
* Gets the sprite translate x.
*
* @return The sprite translate x.
*/
public int getSpriteTranslateX() {
return spriteTranslateX;
}
/**
* Gets the sprite translate y.
*
* @return The sprite translate y.
*/
public int getSpriteTranslateY() {
return spriteTranslateY;
}
/**
* Gets the {@link List} of stack amounts.
*
* @return The stack amounts.
*/
public List<Integer> getStackAmounts() {
return stackAmounts;
}
/**
* Gets the {@link List} of stack model ids.
*
* @return The stack model ids.
*/
public List<Integer> getStackModelIds() {
return stackModelIds;
}
/**
* Gets the team.
*
* @return The team.
*/
public int getTeam() {
return team;
}
/**
* Gets the value.
*
* @return The value.
*/
public int getValue() {
return value;
}
/**
* Gets the members flag.
*
* @return {@code true} if this item is members only, otherwise {@code false}.
*/
public boolean isMembers() {
return members;
}
/**
* Gets the stackable.
*
* @return The stackable.
*/
public boolean isStackable() {
return stackable;
}
}