Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.hm.if2b.powergrid.implementations;
- import edu.hm.if2b.powergrid.interfaces.City;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Objects;
- public class CityClassic implements City {
- private final String name;
- private final int area;
- private Map<City, Integer> connections;
- private boolean closed;
- public CityClassic(final String name, final int area) {
- this.name = Objects.requireNonNull(name);
- if (area < 1)
- throw new IllegalArgumentException();
- this.area = area;
- closed = false;
- this.connections = new HashMap<>();
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public int getArea() {
- return area;
- }
- @Override
- public Map<City, Integer> getConnections() {
- return connections;
- }
- private void setConnections(final Map<City, Integer> connections) {
- this.connections = connections;
- }
- @Override
- public void connect(City to, int cost) {
- if (isClosed())
- throw new IllegalStateException();
- if (cost < 0)
- throw new IllegalArgumentException();
- getConnections().put(Objects.requireNonNull(to), cost);
- to.getConnections().put(this, cost);
- }
- @Override
- public void close() {
- if (isClosed())
- throw new IllegalStateException();
- closed = true;
- setConnections(Map.copyOf(getConnections()));
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- CityClassic that = (CityClassic) o;
- return getName().equals(that.getName());
- }
- @Override
- public int hashCode() {
- return Objects.hash(getName());
- }
- @Override
- public String toString() {
- return getName();
- }
- private boolean isClosed() {
- return closed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement