Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package edu.hm.if2b.powergrid.implementations;
  2.  
  3. import edu.hm.if2b.powergrid.interfaces.City;
  4.  
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Objects;
  8.  
  9. public class CityClassic implements City {
  10. private final String name;
  11. private final int area;
  12. private Map<City, Integer> connections;
  13. private boolean closed;
  14.  
  15. public CityClassic(final String name, final int area) {
  16. this.name = Objects.requireNonNull(name);
  17. if (area < 1)
  18. throw new IllegalArgumentException();
  19. this.area = area;
  20. closed = false;
  21. this.connections = new HashMap<>();
  22. }
  23.  
  24. @Override
  25. public String getName() {
  26. return name;
  27. }
  28.  
  29. @Override
  30. public int getArea() {
  31. return area;
  32. }
  33.  
  34. @Override
  35. public Map<City, Integer> getConnections() {
  36. return connections;
  37. }
  38.  
  39. private void setConnections(final Map<City, Integer> connections) {
  40. this.connections = connections;
  41. }
  42.  
  43. @Override
  44. public void connect(City to, int cost) {
  45. if (isClosed())
  46. throw new IllegalStateException();
  47. if (cost < 0)
  48. throw new IllegalArgumentException();
  49. getConnections().put(Objects.requireNonNull(to), cost);
  50. to.getConnections().put(this, cost);
  51. }
  52.  
  53. @Override
  54. public void close() {
  55. if (isClosed())
  56. throw new IllegalStateException();
  57. closed = true;
  58. setConnections(Map.copyOf(getConnections()));
  59. }
  60.  
  61. @Override
  62. public boolean equals(Object o) {
  63. if (this == o) return true;
  64. if (o == null || getClass() != o.getClass()) return false;
  65. CityClassic that = (CityClassic) o;
  66. return getName().equals(that.getName());
  67. }
  68.  
  69. @Override
  70. public int hashCode() {
  71. return Objects.hash(getName());
  72. }
  73.  
  74. @Override
  75. public String toString() {
  76. return getName();
  77. }
  78.  
  79. private boolean isClosed() {
  80. return closed;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement