Source Code Cross Referenced for ContextOfUIDs.java in  » Science » Cougaar12_4 » org » cougaar » planning » ldm » plan » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.planning.ldm.plan 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.