/*
 * ECatalog is a database front-end, with two main features:
 * 1. Use of preferences
 *  A preference-based approach, where the user is allowed to define the importance of each criterion.
 *  Then the items are ranked accordingly to his criteria.
 * 2. Trade-off analysis
 *  A cooperative database approach, where the system "argues" with the user about his criteria.
 *  When there are no matching items, the system explains the minimal conflicting set and
 *  give some possible strong and weak relaxations about his criteria.
 * This package also containts the software and the set-up details used for our User Study,
 * comparing the use or not of the two previous features mentioned above.
 *
 * Copyright (C) 2006 David Portabella Clotet, Artificial Intelligence Laboratory, EPFL
 * 
 * This file is part of ecatalog-1.0.zip
 * 
 * ECatalog is free software and a free user study set-up;
 * you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ECatalog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECatalog; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * @version 1.0
 * @author David Portabella
 * To contact the author:
 * email: david@portabella.name and david.portabella@epfl.ch
 * 
 * More information about ECatalog:
 *  http://sourceforge.net/projects/ecatalog/
 *  http://icwww.epfl.ch/~portabel/ecatalogs/
 */

package utils;

import java.util.Vector;
import java.util.AbstractCollection;

/* A Set which maintains the order and has the function getElementAt(int index);
 * TODO minor: vector is not indexed, so contains() is not efficient
 * TODO: implement AbstractSet, instead of getCollection() function
 */
public class OrderedSet<E> {
    Vector<E> vector = new Vector<E>();

    public void add(E value) {
        if (vector.contains(value))
            vector.remove(value);
        vector.add(value);
    }

    public void setElementAt(E value, int position) {
        if (position > vector.size()) {
            throw new Error("position > vector.size()");
        }

        int sameValuePosition = vector.indexOf(value);
        if (sameValuePosition != -1) {
            vector.removeElementAt(sameValuePosition);
	    position = (sameValuePosition == -1 || sameValuePosition >= position) ? position : (position -1);
	    if (position == vector.size())
		vector.add(value);
	    else
		vector.setElementAt(value, position);
        } else {
	    position = (sameValuePosition == -1 || sameValuePosition >= position) ? position : (position -1);
	    if (position == vector.size())
		vector.add(value);
	    else
		vector.setElementAt(value, position);
        }
    }

    public void remove(E value) {
        vector.remove(value);
    }

    public void removeElementAt(int index) {
        vector.removeElementAt(index);
    }

    public E getElementAt(int index) {
        return vector.get(index);
    }

    public int size() {
	return vector.size();
    }

    public boolean isEmpty() {
	return vector.isEmpty();
    }

    public void removeAllElements() {
	vector.removeAllElements();
    }

    public AbstractCollection<E> getCollection() {
	return vector;
    }

    public void addAll(OrderedSet<E> c) {
	vector.addAll(c.vector);
    }

    public boolean contains(E value) {
	return vector.contains(value);
    }

    /*
    public Object clone() throws CloneNotSupportedException {
	OrderedSet<E> clone = new OrderedSet<E>();
	clone.vector = vector.clone();

        return clone;
    }
    */

    public String getStringList() {
        StringBuffer s = new StringBuffer("{");
        for (E value :  vector) {
            s.append(value);
            s.append("; ");
        }
        s.append("}");
        return s.toString();
    }
}

