Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

package com.erggroup.buildtool.ripple;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
 * This class is used to contain a collection of Packages
 */
public class PackageCollection {
    
    /**
     * An array of all the packages in the collection
     * Held as a list so that insertion order is retained
     */
    public ArrayList<Package>   mCollection = new ArrayList<Package>();
        
        /** Create a new PackageCollection based on an existing PackageCollection
         * 
         * @param mPackageCollection - Used as a basis
         * 
         */
        public PackageCollection(PackageCollection mPackageCollection)
        {
                for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
            {
                    Package p = it.next();
                    add(p);
            }
        }

        /** Create an empty package collection
         * 
         */
        public PackageCollection()
        {
        }

        /** Add a package to the collection
         *  Assumes that the package does not exist in the collection
         *  
         * @param p
         * @return - The package added
         */
        public Package add(Package p)
        {
                mCollection.add(p);
                return p;
        }
        
        /** Add the contents of one collection to another
     * 
     * @param packageCollection
     */
    public void addAll(PackageCollection packageCollection) {

        mCollection.addAll(packageCollection.mCollection);
        
    }
        
    /** Returns an iterator to traverse the package collection
     *  Should not be used to search the collection
     *  
     * @return an iterator
     */
    public Iterator<Package>  iterator() 
    {
        return mCollection.iterator();
    }

    /** Returns an iterator to traverse the package collection
     *  Should not be used to search the collection
     *  
     * @return an iterator
     */
    public ListIterator<Package> listIterator() {
        return mCollection.listIterator();
    }
        
        /**
         * Determine if the collection contains the specified package alias
         * @param alias
         * @return The package or null
         */
   public Package contains(String alias)
    {
        for (Iterator<Package> it = mCollection.iterator(); it.hasNext(); )
        {
                Package p = it.next();
                if ( p.mAlias.compareTo(alias) == 0 )
                    return p;
        }
        return null;
    }
        
        /**
         * Determine if the collection contains the specified package pvid
         * @param alias
         * @return The package or null
         */
        public Package contains(Integer mId)
        {
        for (Iterator<Package> it = mCollection.iterator(); it.hasNext(); )
        {
                Package p = it.next();
                if ( p.mId == mId )
                    return p;
        }
        return null;
        }
        
        /** Update or insert a package using the package alias
         * 
         * @param p     - Package to process
         */
        public void upsert(Package p) {
                Package pkg = contains(p.mAlias);

                if(pkg != null)
                {
                        // Update the information
                        
                        int index = mCollection.indexOf(pkg);
                        mCollection.set(index, p);

                }
                else
                {
                        // Simply add the package
                        add(p);                 
                }
        }

        /** Clear the contents of the collection
         * 
         */
        public void clear() {
                mCollection.clear();
        }

        /**     Remove a package from the collection
         * 
         * @param p     - Package to remove
         */
        public void remove(Package p) {

                mCollection.remove(p);
        }

}