/*******************************************************************************
 * Copyright (c) quickfixengine.org  All rights reserved. 
 * 
 * This file is part of the QuickFIX FIX Engine 
 * 
 * This file may be distributed under the terms of the quickfixengine.org 
 * license as defined by quickfixengine.org and appearing in the file 
 * LICENSE included in the packaging of this file. 
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING 
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE. 
 * 
 * See http://www.quickfixengine.org/LICENSE for licensing information. 
 * 
 * Contact ask@quickfixengine.org if any conditions of this licensing 
 * are not clear to you.
 ******************************************************************************/

package quickfix;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;

/**
 * Simple wrapper class to synchronize all access to the FIX message store interface.
 *
 */
public class SynchronizedMessageStore implements MessageStore {

    private final Object lock = new Object();
    private final MessageStore messageStore;
    
    public SynchronizedMessageStore(final MessageStore messageStore) {
        if (messageStore == null)
            throw new NullPointerException("messageStore");
        
       this.messageStore = messageStore;
    }
    
    @Override
    public void get(final int startSequence, final int endSequence, final Collection<String> messages)
            throws IOException {
        
        synchronized (lock) {
            getMessageStore().get(startSequence, endSequence, messages);
        }
        
    }

    @Override
    public Date getCreationTime() throws IOException {
        synchronized (lock) {
            return getMessageStore().getCreationTime();
        }
    }

    @Override
    public int getNextSenderMsgSeqNum() throws IOException {
        synchronized (lock) {
            return getMessageStore().getNextSenderMsgSeqNum();
        }
    }

    @Override
    public int getNextTargetMsgSeqNum() throws IOException {
        synchronized (lock) {
            return getMessageStore().getNextTargetMsgSeqNum();   
        }
    }

    @Override
    public void incrNextSenderMsgSeqNum() throws IOException {
        synchronized (lock) {
            getMessageStore().incrNextSenderMsgSeqNum();    
        }       
    }

    @Override
    public void incrNextTargetMsgSeqNum() throws IOException {
        synchronized (lock) {
            getMessageStore().incrNextTargetMsgSeqNum();    
        }       
        
    }

    @Override
    public void refresh() throws IOException {
        synchronized (lock) {
            getMessageStore().refresh();
        }
        
    }

    @Override
    public void reset() throws IOException {
        synchronized (lock) {
            getMessageStore().reset();
        }
        
    }

    @Override
    public boolean set(final int sequence, final String message) throws IOException {
        synchronized (lock) {
            return getMessageStore().set(sequence, message);   
        }
    }

    @Override
    public void setNextSenderMsgSeqNum(final int next) throws IOException {
        synchronized (lock) {
            getMessageStore().setNextSenderMsgSeqNum(next);
        }
        
    }

    @Override
    public void setNextTargetMsgSeqNum(final int next) throws IOException {

        synchronized (lock) {
            getMessageStore().setNextTargetMsgSeqNum(next);
        }
        
    }

    public MessageStore getMessageStore() {
        return messageStore;
    }
}

