Guest User

Untitled

a guest
Jan 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. package XmlImporter;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import breakout.BlockTemplateDocument.BlockTemplate;
  7. import breakout.ColorTypeDocument.ColorType;
  8. import BOut.BlockDesc;
  9.  
  10. /**
  11. * Singleton Factory that constructs BlockDesc descriptions out of the block specification in a Block Template.
  12. *
  13. * @author Eliot MOss
  14. */
  15. public class BlockDescFactory {
  16. //Comment: List of BlockDesc objects that have been created
  17. private static List<BlockDesc> blockDescList = new ArrayList<BlockDesc>();
  18. // the singleton instance
  19. private static BlockDescFactory instance = new BlockDescFactory();
  20.  
  21. // private to enforce singleton
  22. private BlockDescFactory () { }
  23.  
  24. /**
  25. * getter for the singleton
  26. * @return the singleton BlockDescFactory instance
  27. */
  28. public static BlockDescFactory getInstance ()
  29. {
  30. return instance;
  31. }
  32.  
  33. /**
  34. * Returns the unique BlockDesc corresponding to the template's
  35. * specification of width, height, and color
  36. * @param template a BlockTemplate giving width and height of a rectangular block, and RGB color
  37. * @return the BlockDesc for that appearance (guaranteed only one for a given appearance)
  38. */
  39. /* Comment: Checks if a BlockDesc object exists in the List if so it will finds
  40. * and return it. Otherwise it creates a BlockDesc object and returns it.
  41. */
  42. public static BlockDesc getBlockDesc (BlockTemplate template)
  43. {
  44. int width = Integer.parseInt(template.getWidth());
  45. int height = Integer.parseInt(template.getHeight());
  46. ColorType color = template.getColorType();
  47. int r = color.getR();
  48. int g = color.getG();
  49. int b = color.getB();
  50. //Comment: Loops through list looking for matching BlockDesc object
  51. for(int index = 0; index < blockDescList.size(); index++)
  52. {
  53. if(width == blockDescList.get(index).getWidth()&&height == blockDescList.get(index).getHeight()
  54. &&r == blockDescList.get(index).getR()&&g == blockDescList.get(index).getG()
  55. &&b == blockDescList.get(index).getB())
  56. {
  57. return blockDescList.get(index);
  58. }
  59. }
  60.  
  61. BlockDesc uniqueDesc = new BlockDesc(width, height, r, g, b);
  62. blockDescList.add(uniqueDesc);
  63. return uniqueDesc;
  64. }
  65.  
  66.  
  67. }
Add Comment
Please, Sign In to add comment