// Path: airomem-chatsample/airomem-chatsample-view/src/main/java/pl/setblack/airomem/chatsample/view/ChatView.java
// public interface ChatView {
// 
//     List<MessageView> getRecentMessages();
// }
// 
// Path: airomem-chatsample/airomem-chatsample-view/src/main/java/pl/setblack/airomem/chatsample/view/MessageView.java
// @XmlRootElement
// public interface MessageView {
// 
//     @XmlElement
//     String getContent();
//     
//     @XmlElement
//     AuthorView getAuthorView();
//     
//     @XmlAttribute
//     LocalDateTime getTime();
//     
//     String getAuthorNick();
// }
// 
// Path: airomem-core/src/main/java/pl/setblack/airomem/core/WriteChecker.java
// public class WriteChecker {
// 
//     private static final ThreadLocal<PrevalanceContext> CONTEXT_STORE = new ThreadLocal<>();
// 
//     public static void setContext(final PrevalanceContext ctx) {
//         assert !hasPrevalanceContext() : "Context was initialized before";
//         CONTEXT_STORE.set(ctx);
//     }
// 
//     public static PrevalanceContext getContext() {
//         return CONTEXT_STORE.get();
//     }
// 
//     public static void clearContext() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         CONTEXT_STORE.remove();
//     }
// 
//     public static boolean hasPrevalanceContext() {
//         return CONTEXT_STORE.get() != null;
//     }
// 
//     public static void enterSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert !getContext().safe : "context cannot be safe before";
//         CONTEXT_STORE.set(getContext().safe());
//     }
// 
//     public static void leaveSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert getContext().safe : "context must be safe before";
// 
//         CONTEXT_STORE.set(getContext().unsafe());
//     }
// 
// }


import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import pl.setblack.airomem.chatsample.view.ChatView;
import pl.setblack.airomem.chatsample.view.MessageView;
import pl.setblack.airomem.core.WriteChecker;
/*
 *  Created by Jarek Ratajski
 */
package pl.setblack.airomem.chatsample.data;


/**
 *
 * @author jarekr
 */
public class Chat implements ChatView, Serializable {

    private CopyOnWriteArrayList<Message> messages;

    public Chat() {
        this.messages = new CopyOnWriteArrayList<>();
    }

    public void addMessage(String nick, String content, LocalDateTime time) {
        assert WriteChecker.hasPrevalanceContext();
        final Author author = new Author(nick);
        final Message msg = new Message(author, content, time);
        this.messages.add(msg);

    }

    @Override
    public List<MessageView> getRecentMessages() {
        int count = this.messages.size();
// Path: airomem-chatsample/airomem-chatsample-view/src/main/java/pl/setblack/airomem/chatsample/view/ChatView.java
// public interface ChatView {
// 
//     List<MessageView> getRecentMessages();
// }
// 
// Path: airomem-chatsample/airomem-chatsample-view/src/main/java/pl/setblack/airomem/chatsample/view/MessageView.java
// @XmlRootElement
// public interface MessageView {
// 
//     @XmlElement
//     String getContent();
//     
//     @XmlElement
//     AuthorView getAuthorView();
//     
//     @XmlAttribute
//     LocalDateTime getTime();
//     
//     String getAuthorNick();
// }
// 
// Path: airomem-core/src/main/java/pl/setblack/airomem/core/WriteChecker.java
// public class WriteChecker {
// 
//     private static final ThreadLocal<PrevalanceContext> CONTEXT_STORE = new ThreadLocal<>();
// 
//     public static void setContext(final PrevalanceContext ctx) {
//         assert !hasPrevalanceContext() : "Context was initialized before";
//         CONTEXT_STORE.set(ctx);
//     }
// 
//     public static PrevalanceContext getContext() {
//         return CONTEXT_STORE.get();
//     }
// 
//     public static void clearContext() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         CONTEXT_STORE.remove();
//     }
// 
//     public static boolean hasPrevalanceContext() {
//         return CONTEXT_STORE.get() != null;
//     }
// 
//     public static void enterSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert !getContext().safe : "context cannot be safe before";
//         CONTEXT_STORE.set(getContext().safe());
//     }
// 
//     public static void leaveSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert getContext().safe : "context must be safe before";
// 
//         CONTEXT_STORE.set(getContext().unsafe());
//     }
// 
// }


// Path: airomem-chatsample/airomem-chatsample-data/src/main/java/pl/setblack/airomem/chatsample/data/Chat.java
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import pl.setblack.airomem.chatsample.view.ChatView;
import pl.setblack.airomem.chatsample.view.MessageView;
import pl.setblack.airomem.core.WriteChecker;

/*
 *  Created by Jarek Ratajski
 */
package pl.setblack.airomem.chatsample.data;


/**
 *
 * @author jarekr
 */
public class Chat implements ChatView, Serializable {

    private CopyOnWriteArrayList<Message> messages;

    public Chat() {
        this.messages = new CopyOnWriteArrayList<>();
    }

    public void addMessage(String nick, String content, LocalDateTime time) {
        assert WriteChecker.hasPrevalanceContext();
        final Author author = new Author(nick);
        final Message msg = new Message(author, content, time);
        this.messages.add(msg);

    }

    @Override
    public List<MessageView> getRecentMessages() {
        int count = this.messages.size();
        final List<MessageView> res = this.messages.stream().skip(Math.max(count - 10, 0))