Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package XmlImporter;
- import java.util.ArrayList;
- import java.util.List;
- import breakout.BlockTemplateDocument.BlockTemplate;
- import breakout.ColorTypeDocument.ColorType;
- import BOut.BlockDesc;
- /**
- * Singleton Factory that constructs BlockDesc descriptions out of the block specification in a Block Template.
- *
- * @author Eliot MOss
- */
- public class BlockDescFactory {
- //Comment: List of BlockDesc objects that have been created
- private static List<BlockDesc> blockDescList = new ArrayList<BlockDesc>();
- // the singleton instance
- private static BlockDescFactory instance = new BlockDescFactory();
- // private to enforce singleton
- private BlockDescFactory () { }
- /**
- * getter for the singleton
- * @return the singleton BlockDescFactory instance
- */
- public static BlockDescFactory getInstance ()
- {
- return instance;
- }
- /**
- * Returns the unique BlockDesc corresponding to the template's
- * specification of width, height, and color
- * @param template a BlockTemplate giving width and height of a rectangular block, and RGB color
- * @return the BlockDesc for that appearance (guaranteed only one for a given appearance)
- */
- /* Comment: Checks if a BlockDesc object exists in the List if so it will finds
- * and return it. Otherwise it creates a BlockDesc object and returns it.
- */
- public static BlockDesc getBlockDesc (BlockTemplate template)
- {
- int width = Integer.parseInt(template.getWidth());
- int height = Integer.parseInt(template.getHeight());
- ColorType color = template.getColorType();
- int r = color.getR();
- int g = color.getG();
- int b = color.getB();
- //Comment: Loops through list looking for matching BlockDesc object
- for(int index = 0; index < blockDescList.size(); index++)
- {
- if(width == blockDescList.get(index).getWidth()&&height == blockDescList.get(index).getHeight()
- &&r == blockDescList.get(index).getR()&&g == blockDescList.get(index).getG()
- &&b == blockDescList.get(index).getB())
- {
- return blockDescList.get(index);
- }
- }
- BlockDesc uniqueDesc = new BlockDesc(width, height, r, g, b);
- blockDescList.add(uniqueDesc);
- return uniqueDesc;
- }
- }
Add Comment
Please, Sign In to add comment