@@ -0,0 +1,1627 @@
+package com.github.myyk.cracking.chapter7;%0A%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A/**%0A * Explain how you'd design a chat server.%0A *%0A * This could go on forever. I could add user friending features. I could add login%0A *   and log out. I think I answered private and group messaging adequately.%0A */%0Apublic class ChatServer %7B%0A  public Map%3CSet%3CUser%3E,Chat%3E chats;%0A  public ConnectionManager connectionManager;%0A%0A  synchronized public Chat findOrCreateChat(Set%3CUser%3E users) %7B%0A    if (!chats.containsKey(users)) %7B%0A      return chats.put(users, new Chat(users));%0A    %7D else %7B%0A      return chats.get(users);%0A    %7D%0A  %7D%0A%0A  synchronized public Chat updateChatUsers(Set%3CUser%3E oldUsers, Set%3CUser%3E newUsers) %7B%0A    Chat chat = chats.remove(oldUsers);%0A    chat.users = newUsers;%0A    chats.put(newUsers, chat);%0A    return chat;%0A  %7D%0A%0A  public class User %7B%0A    String name;%0A    Set%3CUser%3E friends;%0A%0A    public void sendMessage(Message message, Chat chat) %7B%0A      chat.addMessage(message);%0A    %7D%0A    public void deliver(Message message) %7B%7D%0A  %7D%0A%0A  public class Message %7B%0A    int id;%0A    String text;%0A  %7D%0A%0A  public class Chat %7B%0A    Set%3CUser%3E users;%0A    List%3CMessage%3E messages;%0A%0A    public Chat(Set%3CUser%3E users) %7B%0A      this.users = users;%0A      this.messages = new LinkedList%3CMessage%3E();%0A    %7D%0A%0A    public void addMessage(Message message) %7B%0A      messages.add(message);%0A      for (User user : users) %7B%0A        user.deliver(message);%0A      %7D%0A    %7D%0A  %7D%0A%0A  public class ConnectionManager %7B%0A    Map%3CUser, Connection%3E connections;%0A  %7D%0A%0A  public interface Connection %7B%0A    public void send();%0A  %7D%0A%7D%0A

Add ChatServer