/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
 * Tim's rule of comments: big comments mean trouble.
 * 
 * Be careful of deadlock.  Actually I am not sure this is a problem
 * but it made me nervous so I was careful.  The approach that I took
 * is to synchronize the inner list and the read methods. If there is
 * only one writer then I don't have to worry about things changing
 * underneath the writer.  The fact that the list is synchronized
 * means that readers are safe accessing the list.
 * 
 * The world could change underneath the reader while he is trying to
 * find something. But synchronizing the reader methods protect the
 * reader from change.  These methods are safe to synchronize because
 * they do not make calls such as fire... which might take who knows
 * what locks.
 * 
 * I don't need to synchronize all the reader calls but it seems safer
 * since there is little or no performance penalty.
 */

/**
 * This class is a trivial extension of the SimpleListModel.  This class protects the list in the 
 * SimpleListModel from concurrent access.  It assumes that there is one writer and possibly many readers.
 */

public class ConcurrentListModel extends SimpleListModel {
    private static final long serialVersionUID = -1544733464161201247L;

    @SuppressWarnings("unchecked")
    protected List makeList() {
        return Collections.synchronizedList(new ArrayList());
    }
    
    @Override
    public boolean contains(Object o) {
        synchronized (getList()) {
            return super.contains(o);
        }
    }
    
    @Override
    public Object getElementAt(int i) {
        synchronized (getList()) {
            return super.getElementAt(i);
        }
    }
    
    @Override
    public int getSize() {
        synchronized (getList()) {
            return super.getSize();
        }
    }
    
    @Override
    public List getValues() {
        synchronized (getList()) {
            return super.getValues();
        }
    }
    
    @Override
    public int indexOf(Object o) {
        synchronized (getList()) {
            return super.indexOf(o);
        }
    }
}
