MultiPurposeController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package holeg.ui.controller;
  2. import java.util.HashMap;
  3. import java.util.Map.Entry;
  4. import holeg.model.AbstractCanvasObject;
  5. import holeg.model.Edge;
  6. import holeg.ui.model.Model;
  7. import holeg.ui.view.main.Category;
  8. import java.util.Optional;
  9. /**
  10. * Controller for Multiple Purposes.
  11. *
  12. * @author Gruppe14
  13. */
  14. public class MultiPurposeController {
  15. private Model model;
  16. /**
  17. * Constructor.
  18. *
  19. * @param model
  20. * Model
  21. */
  22. public MultiPurposeController(Model model) {
  23. this.model = model;
  24. }
  25. /**
  26. * search for category.
  27. *
  28. * @param category
  29. * name of the Category
  30. * @return the Category
  31. */
  32. public Category searchCat(String category) {
  33. Integer idx;
  34. if ((idx = model.getCgIdx().get(category)) == null || model.getCgIdx().size() < 1)
  35. return null;
  36. else
  37. return model.getCategories().get(idx);
  38. }
  39. /**
  40. * Search for Object in a Category.
  41. *
  42. * @param category
  43. * name of the Category
  44. * @param object
  45. * Name of the Object
  46. * @return The Object
  47. */
  48. public Optional<AbstractCanvasObject> searchCatObj(Category category, String objectName) {
  49. Integer idx;
  50. if (category.getObjIdx().isEmpty()) {
  51. return Optional.empty();
  52. }
  53. else if ((idx = category.getObjIdx().get(objectName)) == null) {
  54. return Optional.empty();
  55. }
  56. return Optional.ofNullable(category.getObjects().get(idx));
  57. }
  58. /**
  59. * Search for Object by ID.
  60. *
  61. * @param id
  62. * the ID of the Object
  63. * @return the CpsObject
  64. */
  65. public AbstractCanvasObject searchByID(int id) {
  66. Integer idx;
  67. if ((idx = model.getCvsObjIdx().get(id)) == null || model.getCvsObjIdx().size() < 1)
  68. return null;
  69. else
  70. return model.getObjectsOnCanvas().get(idx);
  71. }
  72. /**
  73. * Search Edge between 2 Objects.
  74. *
  75. * @param a
  76. * ID of Object a
  77. * @param b
  78. * ID of Object b
  79. * @return The Edge
  80. */
  81. public Edge searchEdge(int a, int b) {
  82. AbstractCanvasObject objA = searchByID(a);
  83. AbstractCanvasObject objB = searchByID(b);
  84. for (Edge edge : model.getEdgesOnCanvas()) {
  85. // if (edge.getA().getObjName().equals(A.getObjName()) &&
  86. // (edge.getB().getObjName().equals(B.getObjName()))
  87. // || edge.getB().getObjName().equals(A.getObjName())
  88. // && (edge.getA().getObjName().equals(B.getObjName())))
  89. if ((edge.getA().equals(objA) && edge.getB().equals(objB))
  90. || (edge.getB().equals(objA)) && edge.getA().equals(objB))
  91. return edge;
  92. }
  93. return null;
  94. }
  95. /**
  96. * Decrement the Indices if a Key as been removed.
  97. *
  98. * @param key
  99. * the Key
  100. * @param <T>
  101. * key type
  102. * @param map
  103. * the Map
  104. */
  105. public <T> void decIdx(T key, HashMap<T, Integer> map) {
  106. if(!map.containsKey(key)) {
  107. return;
  108. }
  109. for (Entry<T, Integer> i : map.entrySet()) {
  110. if (i.getValue() > map.get(key))
  111. i.setValue(i.getValue() - 1);
  112. }
  113. }
  114. /**
  115. * Adjust Indices before porting them from one map to another
  116. *
  117. * @param key
  118. * the Key
  119. * @param <T>
  120. * key type
  121. * @param map
  122. * the Map
  123. */
  124. public <T> void adjustIdx(int x, HashMap<T, Integer> map) {
  125. for (Entry<T, Integer> i : map.entrySet()) {
  126. i.setValue(i.getValue() + x + 1);
  127. }
  128. }
  129. /**
  130. * Returns the highest Id
  131. *
  132. * @param map
  133. * @return
  134. */
  135. public <T> int getHighestIdx(HashMap<T, Integer> map) {
  136. int max = 0;
  137. for (T i : map.keySet()) {
  138. if (map.get(i) > max)
  139. max = map.get(i);
  140. }
  141. return max;
  142. }
  143. /**
  144. * Copies a HashMap into a new One.
  145. *
  146. * @param map
  147. * the HashMap
  148. * @param <T>
  149. * type
  150. * @return Copy of the HashMap
  151. */
  152. public static <T> HashMap<T, Integer> copyHashMap(HashMap<T, Integer> map) {
  153. HashMap<T, Integer> newMap = new HashMap<>();
  154. for (Entry<T, Integer> i : map.entrySet()) {
  155. newMap.put(i.getKey(), i.getValue());
  156. }
  157. return newMap;
  158. }
  159. }