Source Code Cross Referenced for NegotiableCollection.java in  » 6.0-JDK-Modules » Java-Advanced-Imaging » javax » media » jai » remote » 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 » 6.0 JDK Modules » Java Advanced Imaging » javax.media.jai.remote 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $RCSfile: NegotiableCollection.java,v $
003:         *
004:         * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * Use is subject to license terms.
007:         *
008:         * $Revision: 1.1 $
009:         * $Date: 2005/02/11 04:57:51 $
010:         * $State: Exp $
011:         */package javax.media.jai.remote;
012:
013:        import java.util.Collection;
014:        import java.util.Iterator;
015:        import java.util.Vector;
016:
017:        /**
018:         * A class that wraps an <code>Collection</code> to implement the 
019:         * <code>Negotiable</code> interface. <code>NegotiableCollection</code>
020:         * is a convenience class to specify a <code>Negotiable</code> value for
021:         * a parameter whose valid values are contained in an <code>Collection</code>.
022:         *
023:         * @since JAI 1.1
024:         */
025:        public class NegotiableCollection implements  Negotiable {
026:
027:            private Vector elements;
028:            private Class elementClass;
029:
030:            /**
031:             * Creates a <code>NegotiableCollection</code> given an
032:             * <code>Collection</code>.
033:             *
034:             * @throws IllegalArgumentException if collection is null.
035:             * @throws IllegalArgumentException if all the elements of collection 
036:             * are not of the same <code>Class</code> type.
037:             */
038:            public NegotiableCollection(Collection collection) {
039:
040:                if (collection == null) {
041:                    throw new IllegalArgumentException(JaiI18N
042:                            .getString("NegotiableCollection0"));
043:                }
044:
045:                elements = new Vector();
046:                Object obj;
047:
048:                Iterator i = collection.iterator();
049:                if (i.hasNext()) {
050:                    obj = i.next();
051:                    elements.add(obj);
052:                    elementClass = obj.getClass();
053:                } else {
054:                    // no elements, so elementClass will be initialized to null,
055:                    // which is correct. elements will also be null, which is 
056:                    // also correct.
057:                }
058:
059:                for (; i.hasNext();) {
060:                    obj = i.next();
061:                    if (obj.getClass() != elementClass) {
062:                        throw new IllegalArgumentException(JaiI18N
063:                                .getString("NegotiableCollection1"));
064:                    }
065:                    elements.add(obj);
066:                }
067:            }
068:
069:            /**
070:             * Creates a <code>NegotiableCollection</code> given an array of 
071:             * <code>Object</code>s. The elements of the <code>Object</code>
072:             * array are treated as being the elements of an <code>Collection</code>.
073:             *
074:             * @throws IllegalArgumentException if objects is null.
075:             * @throws IllegalArgumentException if all the elements of objects are not
076:             * of the same <code>Class</code> type.
077:             */
078:            public NegotiableCollection(Object objects[]) {
079:
080:                if (objects == null) {
081:                    throw new IllegalArgumentException(JaiI18N
082:                            .getString("NegotiableCollection0"));
083:                }
084:
085:                int length = objects.length;
086:                if (length != 0) {
087:                    elementClass = objects[0].getClass();
088:                } else {
089:                    // no elements, so elementClass will be initialized to null,
090:                    // which is correct. elements will also be null, which is 
091:                    // also correct.
092:                }
093:
094:                elements = new Vector(length);
095:                for (int i = 0; i < length; i++) {
096:                    if (objects[i].getClass() != elementClass) {
097:                        throw new IllegalArgumentException(JaiI18N
098:                                .getString("NegotiableCollection1"));
099:                    }
100:                    elements.add(objects[i]);
101:                }
102:            }
103:
104:            /**
105:             * Returns the <code>Collection</code> of values which are currently
106:             * valid for this class, null if there are no valid values.
107:             */
108:            public Collection getCollection() {
109:                if (elements.isEmpty())
110:                    return null;
111:                return elements;
112:            }
113:
114:            /**
115:             * Returns a <code>NegotiableCollection</code> that contains those
116:             * elements that are common to this <code>NegotiableCollection</code>
117:             * and the one supplied. If the supplied <code>Negotiable</code> is not
118:             * a <code>NegotiableCollection</code> with its elements being of the 
119:             * same <code>Class</code> as this class', or if there are no common
120:             * elements, the negotiation will fail and <code>null</code> (signifying
121:             * the failure of the negotiation) will be returned.
122:             *
123:             * @param other The <code>Negotiable</code> to negotiate with.
124:             */
125:            public Negotiable negotiate(Negotiable other) {
126:
127:                if (other == null) {
128:                    return null;
129:                }
130:
131:                // if other is not an instance of NegotiableCollection
132:                if (!(other instanceof  NegotiableCollection)
133:                        || other.getNegotiatedValueClass() != elementClass) {
134:                    return null;
135:                }
136:
137:                Object obj;
138:                Vector result = new Vector();
139:
140:                Collection otherCollection = ((NegotiableCollection) other)
141:                        .getCollection();
142:
143:                // If the collection is null, i.e there are no valid values, then 
144:                // negotiation fails.
145:                if (otherCollection == null)
146:                    return null;
147:
148:                // Return a NegotiableCollection whose elements are those that
149:                // were common to both the collections.
150:                for (Iterator i = elements.iterator(); i.hasNext();) {
151:                    obj = i.next();
152:                    // If element is present in both the collections
153:                    if (otherCollection.contains(obj)) {
154:                        // Do not insert duplicates
155:                        if (!result.contains(obj)) {
156:                            result.add(obj);
157:                        }
158:                    }
159:                }
160:
161:                // If there are no common elements, negotiation failed.
162:                if (result.isEmpty()) {
163:                    return null;
164:                }
165:
166:                return new NegotiableCollection(result);
167:            }
168:
169:            /**
170:             * Returns a single value that is valid for this 
171:             * <code>NegotiableCollection</code>. The returned value is the first
172:             * element contained in this <code>NegotiableCollection</code>. Returns
173:             * <code>null</code> if there are no valid elements in this
174:             * <code>NegotiableCollection</code>.
175:             */
176:            public Object getNegotiatedValue() {
177:
178:                // Return the first element in this NegotiableCollection
179:                // else return 
180:                // <code>null</code> 
181:                if (elements != null && elements.size() > 0) {
182:                    return elements.elementAt(0);
183:                } else {
184:                    return null;
185:                }
186:            }
187:
188:            /**
189:             * Returns the <code>Class</code> of the Object returned as the result
190:             * of the negotiation. If the <code>Collection</code> used to construct
191:             * this <code>NegotiableCollection</code> was empty, i.e. had no
192:             * elements, the <code>Class</code> of the elements is indeterminate, 
193:             * therefore null will be returned from this method in such a case.
194:             */
195:            public Class getNegotiatedValueClass() {
196:                return elementClass;
197:            }
198:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.