001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.planning.ldm.plan;
028:
029: import java.util.AbstractCollection;
030: import java.util.Collection;
031: import java.util.HashSet;
032: import java.util.Iterator;
033:
034: import org.cougaar.core.util.UID;
035:
036: /** ContextOfUIDs is an implementation of Context. It is simply a collection of UIDs.
037: * It can be used when the "problem" or "problems" that a task is related to can be
038: * be referenced by the "problem's" UID.
039: * @see UID
040: * @see Context
041: */
042: public class ContextOfUIDs extends AbstractCollection implements
043: Context, Collection {
044:
045: UID[] uids;
046:
047: /** Copies all UID objects from uids into the ContextOfUIDs
048: * If there are objects in uids that are not UID objects, they will not be added.
049: * @see org.cougaar.core.util.UID
050: */
051: public ContextOfUIDs(Collection uids) {
052: int UIDcount = 0;
053: // Count the number of elements in the collection that are actually UIDs
054: for (Iterator iterator = uids.iterator(); iterator.hasNext();) {
055: Object o = iterator.next();
056: if (o instanceof UID)
057: UIDcount++;
058: }
059: // Now go through again and add the UIDs to the array
060: this .uids = new UID[UIDcount];
061: int i = 0;
062: for (Iterator iterator = uids.iterator(); iterator.hasNext();) {
063: Object o = iterator.next();
064: if (o instanceof UID)
065: this .uids[i] = (UID) o;
066: }
067: }
068:
069: /** Creates empty ContextOfUIDs
070: * This is completely useless as this object is immutable.
071: */
072: public ContextOfUIDs() {
073: }
074:
075: /**
076: * Constructor that creates a collection with one and only one UID
077: */
078: public ContextOfUIDs(UID oneUID) {
079: uids = new UID[1];
080: uids[0] = oneUID;
081: }
082:
083: /**
084: * A constructor that copies the elements of the passed in array into the collection
085: */
086: public ContextOfUIDs(UID[] arrayOfUIDS) {
087: uids = new UID[arrayOfUIDS.length];
088: for (int i = 0; i < arrayOfUIDS.length; i++) {
089: uids[i] = arrayOfUIDS[i];
090: }
091: }
092:
093: public Iterator iterator() {
094: return new UIDIterator(uids);
095: }
096:
097: /** @return the number of elements in the collection */
098: public int size() {
099: return uids.length;
100: }
101:
102: public boolean contains(Object o) {
103: if (o instanceof UID) {
104: UID inthere = (UID) o;
105: for (int i = 0; i < uids.length; i++) {
106: if (uids[i].equals(inthere))
107: return true;
108: }
109: }
110: return false;
111: }
112:
113: /**
114: * @return true if this collection contains all of the elements in other
115: */
116: public boolean containsAll(Collection other) {
117: boolean found = false;
118: for (Iterator otherIterator = other.iterator(); otherIterator
119: .hasNext();) {
120: Object o = otherIterator.next();
121: if (!(o instanceof UID))
122: return false;
123: UID otherUID = (UID) o;
124: found = false;
125: for (int i = 0; i < uids.length; i++) {
126: if (otherUID.equals(uids[i])) {
127: found = true;
128: break;
129: }
130: }
131: if (!found)
132: return false;
133: }
134: return true;
135: }
136:
137: /**
138: * @return an array of Object containing UIDs in the collection
139: * @see org.cougaar.core.util.UID
140: **/
141: public Object[] toArray() {
142: return toArray(new UID[uids.length]);
143: }
144:
145: /**
146: * @return an array of UIDs with all the UIDs in the collection
147: * @param array an array of UID. A new array will be allocated if array size is incorrect.
148: *
149: * @exception Throws ArrayStoreException if array is not an array of UID
150: **/
151: public Object[] toArray(Object[] array) {
152: if (!(array instanceof UID[]))
153: throw new ArrayStoreException(
154: "array must be an array of UID");
155:
156: if (array.length != uids.length) {
157: array = new UID[uids.length];
158: }
159: System.arraycopy(uids, 0, array, 0, uids.length);
160: return array;
161: }
162:
163: /**
164: * Simple accessor for n-th uid. Avoids consing iterators or arrays.
165: **/
166: public UID get(int i) {
167: return uids[i];
168: }
169:
170: public static class UIDIterator implements Iterator {
171: UID[] uidArray;
172: int place = 0;
173:
174: UIDIterator(UID[] uids) {
175: uidArray = uids;
176: }
177:
178: public boolean hasNext() {
179: if (place < uidArray.length)
180: return true;
181: return false;
182: }
183:
184: public Object next() {
185: return uidArray[place++];
186: }
187:
188: /**
189: * @exception always throws UnsupportedOperationException
190: */
191: public void remove() {
192: throw new UnsupportedOperationException(
193: "ContextOfUIDs is immutable collection");
194: }
195: }
196:
197: public String toString() {
198: String output = "[ContextOfUIDs ";
199: for (int i = 0; i < uids.length; i++) {
200: output += uids[i].toString();
201: output += " ";
202: }
203: output += "]";
204: return output;
205: }
206:
207: /**
208: * Convenience method that creates a new ContextOfUIDs that is the union of
209: * the of all of the members of the Collection passed in. Items in the Collection
210: * that are not instances of ContextOfUIDs are ignored.
211: * @param contexts A Collection containing ContextOfUIDs
212: * @return the union of the members of parameter
213: */
214: public static ContextOfUIDs merge(Collection contexts) {
215:
216: // Add the UIDs to a HashMap. The HashMap will prevent duplicates
217: HashSet union = new HashSet(5);
218:
219: for (Iterator contextsIt = contexts.iterator(); contextsIt
220: .hasNext();) {
221: Object o = contextsIt.next();
222: if (o instanceof ContextOfUIDs) {
223: ContextOfUIDs couid = (ContextOfUIDs) o;
224: for (Iterator it = couid.iterator(); it.hasNext();) {
225: UID oneUID = (UID) it.next();
226: union.add(oneUID);
227: }
228: }
229: }
230: // create a new ContextOfUIDs from the Collection of the HashMap
231: return new ContextOfUIDs(union);
232: }
233: }
|