@@ -0,0 +1,2171 @@
+package net;%0A%0Aimport java.util.ArrayList;%0Aimport util.HashUtil;%0Aimport java.lang.IllegalStateException;%0A%0Apublic class Network %7B%0A%09private static HashUtil hu = new HashUtil(HashUtil.SHA_256);%0A%09private ArrayList%3CString%3E stations;%0A%0A%09public Network() %7B%0A%09%09stations = new ArrayList%3CString%3E();%0A%09%7D%0A%0A%09/**%0A%09 * Adds a station to this network, and inserts it into the list of stations in such a way that the list is lexicographically ordered according to %7B@code String.compareTo(String someString)%7D.%0A%09 * @param alias The alias of the station that was added to the network%0A%09 */%0A%09public synchronized void addStation(String alias) %7B%0A%09%09Iterator%3CString%3E it = stations.iterator();%0A%09%09for(int i = 0; i %3C stations.size(); i++) %7B%0A%09%09%09// compare both strings%0A%09%09%09int com = it.next().compareTo(alias);%0A%09%09%09if(com == 0) %7B%0A%09%09%09%09throw new IllegalStateException(%22The alias %22 + alias + %22 is already part of this network.%22);%0A%09%09%09%7D else if(com %3E 0) %7B%0A%09%09%09%09stations.add(i, alias);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09// In case no other condition triggered%0A%09%09stations.add(alias);%0A%09%7D%0A%0A%09/**%0A%09 * Removes a station from the network.%0A%09 * @param alias The alias of that station%0A%09 */%0A%09public synchronized void removeStation(String alias) %7B%0A%09%09boolean contained = stations.remove(alias);%0A%09%09if(!contained) %7B%0A%09%09%09throw new IllegalStateException(%22The station with alias %22 + alias + %22 was not part of this network.%22);%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * Returns a shallow copy of all members in this network.%0A%09 */%0A%09public Collection%3CString%3E getStations() %7B%0A%09%09return new ArrayList%3CString%3E(this);%0A%09%7D%0A%0A%09/**%0A%09 * Creates a hash that represents the current state of this network.%0A%09 * The hash is constructed like this:%0A%09 * The string s contains the aliases of all current network members, %0A%09 * ordered lexicographically, separated by line feeds (%5C%5Cn), and concluded%0A%09 * by the number of members.%0A%09 * The returned byte array is the SHA-256 hash of that string s.%0A%09 * @return A hash representing the current network state.%0A%09 */%0A%09public byte%5B%5D getHash() %7B%0A%09%09StringBuilder sb = new StringBuilder();%0A%09%09Iterator%3CString%3E it = stations.iterator();%0A%09%09while(it.hasNext())%7B%0A%09%09%09sb.add(it.next() + %22%5Cn%22);%0A%09%09%7D%0A%09%09sb.add(stations.size());%0A%09%09return hu.digest(sb.toString());%0A%09%7D%0A%7D

Add Network