Package com.nexirius.framework.datamodel

Source Code of com.nexirius.framework.datamodel.DataModelVector

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.datamodel;

import com.nexirius.util.CopyPairs;
import com.nexirius.util.SortedVector;

import java.util.Enumeration;
import java.util.Iterator;

/**
* Implementation a list of DataModel items
*
* @author Marcel Baumann
*/
public class DataModelVector extends SortedVector {
    /**
     * Creates an empty list
     */
    public DataModelVector() {
        super();
    }

    /**
     * Creates a shallow copy
     */
    public DataModelVector(DataModelVector v) {
        super(v);
    }

    /**
     * Used for simple iteration.
     * <PRE>
     * for (DataModel m = list.firstItem(); m != null; m = list.nextItem())
     * {
     * // do something with m
     * }
     * </PRE>
     */
    public DataModel firstItem() {
        return (DataModel) firstObject();
    }

    /**
     * Used for simple iteration.
     * <PRE>
     * for (DataModel m = list.firstItem(); m != null; m = list.nextItem())
     * {
     * // do something with m
     * }
     * </PRE>
     */
    public DataModel nextItem() {
        return (DataModel) nextObject();
    }

    /**
     * Used for simple iteration (backwards).
     * <PRE>
     * for (DataModel m = list.lastItem(); m != null; m = list.previousItem())
     * {
     * // do something with m
     * }
     * </PRE>
     */
    public DataModel lastItem() {
        return (DataModel) lastObject();
    }

    /**
     * Used for simple iteration (backwards).
     * <PRE>
     * for (DataModel m = list.lastItem(); m != null; m = list.previousItem())
     * {
     * // do something with m
     * }
     * </PRE>
     */
    public DataModel previousItem() {
        return (DataModel) previousObject();
    }

    /**
     * Returns the DataModel instance at the specified index or throws a runtime exception.
     *
     * @param index An integer between 0 and size()-1
     */
    public DataModel getItem(int index) {
        return (DataModel) elementAt(index);
    }

    /**
     * Appends a new instance to the end of the list.
     *
     * @param item The status which is appended
     */
    public void append(DataModel item) {
        addElement(item);
    }

    /**
     * Inserts an status at a specified position into the list.
     *
     * @param item     The status which is inserted
     * @param position The index at which the status is inserted (0 for beginning, size() for end)
     */
    public void insert(DataModel item, int position) {
        insertElementAt(item, position);
    }

    /**
     * Inserts an status to the beginning of the list.
     *
     * @param item The status which is inserted
     */
    public void insert(DataModel item) {
        insertElementAt(item, 0);
    }

    /**
     * iterate thru collection and do something on each entry
     * @param worker
     * @return the number of entries which where accessed by the worker
     */
    public int work(DataModelWorker worker) {
        if (worker == null) {
            return 0;
        }

        int ret = 0;

        Iterator iterator = this.iterator();

        while (iterator.hasNext()) {
            DataModel model = (DataModel) iterator.next();

            DataModelWorker.WorkResult workResult = worker.work(model);

            ++ret;

            if (workResult == DataModelWorker.CONTINUE) {
                continue;
            }
            if (workResult == DataModelWorker.BREAK) {
                break;
            }
            if (workResult == DataModelWorker.REMOVE_ENTRY_AND_CONTINUE) {
                iterator.remove();
                continue;
            }
            if (workResult == DataModelWorker.REMOVE_ENTRY_AND_BREAK) {
                iterator.remove();
                break;
            }
        }

        return ret;
    }

    /**
     * Creates a deep copy of all the elements of the list
     * on all the members)
     *
     * @param copyPairs A list which helps to avoid multiple copies of equal references. (when null the list is internally created)
     */
    public DataModelVector duplicate(CopyPairs copyPairs) {
        DataModelVector ret = new DataModelVector();
        Enumeration e = elements();

        if (copyPairs == null) {
            copyPairs = new CopyPairs();
        }

        while (e.hasMoreElements()) {
            DataModel orig = (DataModel) e.nextElement();
            DataModel copy = (DataModel) copyPairs.getCopy(orig);

            if (copy == null) {
                copy = orig.duplicate(null, copyPairs);
                copyPairs.put(orig, copy);
            }

            ret.append(copy);
        }

        return ret;
    }

    /**
     * Returns an iterator helper.
     *
     * @return An enumeration instance (eventually empty but never null)
     */
    public synchronized DataModelEnumeration getEnumeration() {
        return new DataModelEnumeration(size(), iterator());
    }

    /**
     * Removes an status by reference (uses == to compare). If the status is not part of
     * the list then this function does nothing (no exception is thrown).
     *
     * @param item The reference of the status which will be removed
     */
    public synchronized void removeItem(DataModel item) {
        DataModelEnumeration e = getEnumeration();
        int i = 0;

        while (e.hasMore()) {
            DataModel m = e.next();

            if (m == item) {
                removeElementAt(i);

                break;
            }

            ++i;
        }
    }

    /**
     * Deep compare two lists. Two lists are not equal if they have a different size
     * or if one of the members returns false (break) when compared to its
     * correspondent member from the other list or if other == null or if
     * other is no DataModelVector instance.
     *
     * @param other The list which this list is compared to
     */
    public synchronized boolean equals(Object other) {
        if (other == null || !(other instanceof DataModelVector)) {

            return false;
        }

        if (other == this) {

            return true;
        }

        DataModelVector o = (DataModelVector) other;

        if (size() != o.size()) {

            return false;
        }

        DataModel a = firstItem();
        DataModel b = o.firstItem();

        while (a != null && b != null) {

            if (!a.equals(b)) {

                return false;
            }

            a = nextItem();
            b = o.nextItem();
        }

        return true;
    }
}
TOP

Related Classes of com.nexirius.framework.datamodel.DataModelVector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.