HolonObject.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package classes;
  2. import java.util.ArrayList;
  3. import ui.model.idCounter;
  4. public class HolonObject extends CpsObject {
  5. /* Array of all consumers */
  6. private ArrayList<HolonElement> elements;
  7. /* Total of consumption */
  8. private float currentEnergy;
  9. /**
  10. * State of the building: 0 = fully supplied (currentEnergy == 0) 1 = not
  11. * enough energy (currentEnergy > 0) 2 = oversupplied (currentEnergy < 0)
  12. */
  13. int state;
  14. /**
  15. * Constructor Set by default the name of the object equals to the category
  16. * name, until the user changes it.
  17. */
  18. public HolonObject(String ObjName) {
  19. super(ObjName);
  20. setElements(new ArrayList<HolonElement>());
  21. }
  22. public HolonObject(String ObjName, String obj) {
  23. super(ObjName);
  24. super.setName(obj);
  25. setElements(new ArrayList<HolonElement>());
  26. }
  27. public HolonObject(CpsObject obj) {
  28. super(obj);
  29. setElements(((HolonObject) obj).getCopyofElements());
  30. }
  31. /**
  32. * @return the elements
  33. */
  34. public ArrayList<HolonElement> getElements() {
  35. return elements;
  36. }
  37. /**
  38. * @param elements
  39. * the elements to set
  40. */
  41. public void setElements(ArrayList<HolonElement> elements) {
  42. this.elements = elements;
  43. }
  44. public void addElements(HolonElement element) {
  45. elements.add(element);
  46. }
  47. /**
  48. * @return the currentEnergy
  49. */
  50. public float getCurrentEnergy() {
  51. float temp = 0;
  52. for (HolonElement e : getElements()) {
  53. temp = temp + e.getTotalEnergy();
  54. }
  55. currentEnergy = temp;
  56. return currentEnergy;
  57. }
  58. /**
  59. * @param currentEnergy
  60. * the currentEnergy to set
  61. */
  62. public void setCurrentEnergy(float currentEnergy) {
  63. this.currentEnergy = currentEnergy;
  64. }
  65. /**
  66. * deletes Element
  67. *
  68. * @param idx
  69. */
  70. public void deleteElement(int idx) {
  71. elements.remove(idx);
  72. }
  73. public float calculateCurrentEnergy() {
  74. return currentEnergy;
  75. }
  76. /**
  77. * String of all consumers in this HolonObject
  78. *
  79. * @return all the names of this HolonObject separated by "," each object
  80. */
  81. public String toStringElements() {
  82. String objString = "Empty";
  83. for (HolonElement e : elements) {
  84. if (objString == "Empty") {
  85. objString = e.getEleName();
  86. } else {
  87. objString = objString + ", " + e.getEleName();
  88. }
  89. }
  90. return objString;
  91. }
  92. /**
  93. * Copys the ArrayList
  94. *
  95. * @param the
  96. * ArrayList to Copy
  97. *
  98. * @return the currentEnergy
  99. */
  100. public ArrayList<HolonElement> getCopyofElements() {
  101. ArrayList<HolonElement> temp = new ArrayList<>();
  102. for (HolonElement h : elements) {
  103. HolonElement he = new HolonElement(h);
  104. temp.add(he);
  105. }
  106. return temp;
  107. }
  108. }