Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7186 dpurdie 1
package com.erggroup.buildtool.ripple;
2
 
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.ListIterator;
6
 
7
/**
8
 * This class is used to contain a collection of Packages
9
 */
10
public class PackageCollection {
11
 
12
    /**
13
     * An array of all the packages in the collection
14
     * Held as a list so that insertion order is retained
15
     */
16
    public ArrayList<Package>   mCollection = new ArrayList<Package>();
17
 
18
	/** Create a new PackageCollection based on an existing PackageCollection
19
	 * 
20
	 * @param mPackageCollection - Used as a basis
21
	 * 
22
	 */
23
	public PackageCollection(PackageCollection mPackageCollection)
24
	{
25
		for (Iterator<Package> it = mPackageCollection.iterator(); it.hasNext(); )
26
	    {
27
	            Package p = it.next();
28
	            add(p);
29
	    }
30
	}
31
 
32
	/** Create an empty package collection
33
	 * 
34
	 */
35
	public PackageCollection()
36
	{
37
	}
38
 
39
	/** Add a package to the collection
40
	 *  Assumes that the package does not exist in the collection
41
	 *  
42
	 * @param p
43
	 * @return - The package added
44
	 */
45
	public Package add(Package p)
46
	{
47
		mCollection.add(p);
48
		return p;
49
	}
50
 
51
	/** Add the contents of one collection to another
52
     * 
53
     * @param packageCollection
54
     */
55
    public void addAll(PackageCollection packageCollection) {
56
 
57
        mCollection.addAll(packageCollection.mCollection);
58
 
59
    }
60
 
61
    /** Returns an iterator to traverse the package collection
62
     *  Should not be used to search the collection
63
     *  
64
     * @return an iterator
65
     */
66
    public Iterator<Package>  iterator() 
67
    {
68
        return mCollection.iterator();
69
    }
70
 
71
    /** Returns an iterator to traverse the package collection
72
     *  Should not be used to search the collection
73
     *  
74
     * @return an iterator
75
     */
76
    public ListIterator<Package> listIterator() {
77
        return mCollection.listIterator();
78
    }
79
 
80
	/**
81
	 * Determine if the collection contains the specified package alias
82
	 * @param alias
83
	 * @return The package or null
84
	 */
85
   public Package contains(String alias)
86
    {
87
        for (Iterator<Package> it = mCollection.iterator(); it.hasNext(); )
88
        {
89
                Package p = it.next();
90
                if ( p.mAlias.compareTo(alias) == 0 )
91
                    return p;
92
        }
93
        return null;
94
    }
95
 
96
	/**
97
	 * Determine if the collection contains the specified package pvid
98
	 * @param alias
99
	 * @return The package or null
100
	 */
101
	public Package contains(Integer mId)
102
	{
103
        for (Iterator<Package> it = mCollection.iterator(); it.hasNext(); )
104
        {
105
                Package p = it.next();
106
                if ( p.mId == mId )
107
                    return p;
108
        }
109
        return null;
110
	}
111
 
112
	/** Update or insert a package using the package alias
113
	 * 
114
	 * @param p	- Package to process
115
	 */
116
	public void upsert(Package p) {
117
		Package pkg = contains(p.mAlias);
118
 
119
		if(pkg != null)
120
		{
121
			// Update the information
122
 
123
			int index = mCollection.indexOf(pkg);
124
			mCollection.set(index, p);
125
 
126
		}
127
		else
128
		{
129
			// Simply add the package
130
			add(p);			
131
		}
132
	}
133
 
134
	/** Clear the contents of the collection
135
	 * 
136
	 */
137
	public void clear() {
138
		mCollection.clear();
139
	}
140
 
141
	/**	Remove a package from the collection
142
	 * 
143
	 * @param p	- Package to remove
144
	 */
145
	public void remove(Package p) {
146
 
147
		mCollection.remove(p);
148
	}
149
 
150
}