001: /*
002: * @(#)AnalysisModuleSet.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.datastore;
028:
029: import java.util.Hashtable;
030: import java.util.Vector;
031:
032: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
033:
034: /**
035: * Contains data associated with each analysis module.
036: *
037: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
038: * @version $Date: 2004/04/15 05:48:26 $
039: * @since December 15, 2002
040: */
041: public class AnalysisModuleSet {
042: private Hashtable moduleNameToIndex = new Hashtable();
043: private Vector moduleList = new Vector();
044:
045: /**
046: * Default constructor
047: */
048: public AnalysisModuleSet() {
049: // do nothing
050: }
051:
052: /**
053: * Create an instance using the given array of modules as the default
054: * set.
055: *
056: * @param modules collection of modules to initialize the set with.
057: * @exception IllegalArgumentException if <tt>modules</tt> is
058: * <tt>null</tt>.
059: */
060: public AnalysisModuleSet(IAnalysisModule[] modules) {
061: if (modules == null) {
062: throw new IllegalArgumentException("No null args.");
063: }
064:
065: addAnalysisModules(modules);
066: }
067:
068: /**
069: * Copy <tt>ams</tt>'s modules into this new set.
070: *
071: * @param ams the set to copy from
072: * @exception IllegalArgumentException if <tt>ams</tt> is <tt>null</tt>.
073: */
074: public AnalysisModuleSet(AnalysisModuleSet ams) {
075: if (ams == null) {
076: throw new IllegalArgumentException("No null args.");
077: }
078:
079: joinAnalysisModuleSet(ams);
080: }
081:
082: /**
083: * Add an array of modules to this set.
084: *
085: * @param modules the array of modules to add to the set
086: * @exception IllegalArgumentException if <tt>modules</tt> is
087: * <tt>null</tt>, or if any element in the array is <tt>null</tt>.
088: */
089: public void addAnalysisModules(IAnalysisModule[] modules) {
090: if (modules == null) {
091: throw new IllegalArgumentException("No null args.");
092: }
093:
094: for (int i = 0; i < modules.length; ++i) {
095: addAnalysisModule(modules[i]);
096: }
097: }
098:
099: /**
100: * Add a module to the set.
101: *
102: * @param module the module to add.
103: * @exception IllegalArgumentException if <tt>module</tt> is <tt>null</tt>.
104: * @exception IllegalStateException if <tt>module</tt> has a measure name
105: * that is the same as a previously added module.
106: */
107: public void addAnalysisModule(IAnalysisModule module) {
108: if (module == null) {
109: throw new IllegalArgumentException("No null args.");
110: }
111:
112: String name = module.getMeasureName();
113:
114: synchronized (this .moduleNameToIndex) {
115: if (this .moduleNameToIndex.containsKey(name)) {
116: throw new IllegalStateException(
117: "Already have module with " + "measure '"
118: + name + "'.");
119: }
120:
121: int nextIndex = this .moduleList.size();
122: if (nextIndex >= Short.MAX_VALUE) {
123: throw new IllegalStateException(
124: "Too many modules. There is a maximum of "
125: + Short.MAX_VALUE + " modules allowed.");
126: }
127: this .moduleNameToIndex.put(name, new Short(
128: (short) nextIndex));
129: this .moduleList.addElement(module);
130: }
131: }
132:
133: /**
134: * Join a module set to this set. It will not add to this set any
135: * repeated measure names that are in the given set.
136: *
137: * @param set the module set to add.
138: * @exception IllegalArgumentException if <tt>set</tt> is <tt>null</tt>.
139: */
140: public void joinAnalysisModuleSet( AnalysisModuleSet set )
141: {
142: if (set == null)
143: {
144: throw new IllegalArgumentException( "No null args." );
145: }
146:
147: java.util.Enumeration enum = set.moduleList.elements();
148: while (enum.hasMoreElements())
149: {
150: IAnalysisModule module = (IAnalysisModule)enum.nextElement();
151: String name = module.getMeasureName();
152: if (!this .moduleNameToIndex.containsKey( name ))
153: {
154: addAnalysisModule( module );
155: }
156: }
157: }
158:
159: /**
160: * Return a complete list of modules in order.
161: *
162: * @return the ordered array of stored modules.
163: */
164: public IAnalysisModule[] getAnalysisModules() {
165: synchronized (this .moduleNameToIndex) {
166: int len = this .moduleList.size();
167: IAnalysisModule[] am = new IAnalysisModule[len];
168: this .moduleList.copyInto(am);
169: return am;
170: }
171: }
172:
173: /**
174: * Retrieves the index associated with the module with the given
175: * measure name.
176: *
177: * @param measureName the measure name of the module.
178: * @return the index of the module, or -1 if there is no such module
179: * with the given measure name.
180: * @exception IllegalArgumentException if <tt>measureName</tt> is
181: * <tt>null</tt>.
182: * @see #getAnalysisModuleIndex( IAnalysisModule )
183: */
184: public short getMeasureIndex(String measureName) {
185: if (measureName == null) {
186: throw new IllegalArgumentException("No null args.");
187: }
188: Short i = (Short) this .moduleNameToIndex.get(measureName);
189: if (i == null) {
190: return -1;
191: }
192: return i.shortValue();
193: }
194:
195: /**
196: * Returns the module <tt>am</tt>'s index.
197: *
198: * @param am the module.
199: * @return the index of <tt>am</tt>, or -1 if it is not stored in this
200: * set.
201: * @exception IllegalArgumentException if <tt>am</tt> is <tt>null</tt>.
202: * @see #getMeasureIndex( Stirng )
203: */
204: public short getAnalysisModuleIndex(IAnalysisModule am) {
205: if (am == null) {
206: throw new IllegalArgumentException("No null args.");
207: }
208: return getMeasureIndex(am.getMeasureName());
209: }
210:
211: /**
212: * Retrieve the number of modules stored in this set.
213: *
214: * @return the number of modules in the set.
215: */
216: public int getAnalysisModuleCount() {
217: return this .moduleList.size();
218: }
219:
220: /**
221: * Returns the module stored at the given index <tt>index</tt>.
222: *
223: * @return the module at index <tt>index</tt>.
224: * @exception IllegalArgumentException if <tt>index</tt> is less than 0
225: * or greater than or equal to the returned value of
226: * <tt>getAnalysisModuleCount()</tt>.
227: */
228: public IAnalysisModule getAnalysisModuleAt(short index) {
229: int iindex = (int) index;
230: if (iindex < 0 || iindex >= this .moduleList.size()) {
231: throw new IllegalArgumentException(
232: "index out of range [0.."
233: + (this .moduleList.size() - 1) + "]");
234: }
235: return (IAnalysisModule) this.moduleList.elementAt(iindex);
236: }
237: }
|