Source Code Cross Referenced for CollectionUtils.java in  » J2EE » spring-framework-2.5 » org » springframework » util » 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 » J2EE » spring framework 2.5 » org.springframework.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2007 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.util;
018:
019:        import java.util.Arrays;
020:        import java.util.Collection;
021:        import java.util.Enumeration;
022:        import java.util.Iterator;
023:        import java.util.List;
024:        import java.util.Map;
025:        import java.util.Properties;
026:
027:        /**
028:         * Miscellaneous collection utility methods.
029:         * Mainly for internal use within the framework.
030:         *
031:         * @author Juergen Hoeller
032:         * @author Rob Harrop
033:         * @since 1.1.3
034:         */
035:        public abstract class CollectionUtils {
036:
037:            /**
038:             * Return <code>true</code> if the supplied Collection is <code>null</code>
039:             * or empty. Otherwise, return <code>false</code>.
040:             * @param collection the Collection to check
041:             * @return whether the given Collection is empty
042:             */
043:            public static boolean isEmpty(Collection collection) {
044:                return (collection == null || collection.isEmpty());
045:            }
046:
047:            /**
048:             * Return <code>true</code> if the supplied Map is <code>null</code>
049:             * or empty. Otherwise, return <code>false</code>.
050:             * @param map the Map to check
051:             * @return whether the given Map is empty
052:             */
053:            public static boolean isEmpty(Map map) {
054:                return (map == null || map.isEmpty());
055:            }
056:
057:            /**
058:             * Convert the supplied array into a List. A primitive array gets
059:             * converted into a List of the appropriate wrapper type.
060:             * <p>A <code>null</code> source value will be converted to an
061:             * empty List.
062:             * @param source the (potentially primitive) array
063:             * @return the converted List result
064:             * @see ObjectUtils#toObjectArray(Object)
065:             */
066:            public static List arrayToList(Object source) {
067:                return Arrays.asList(ObjectUtils.toObjectArray(source));
068:            }
069:
070:            /**
071:             * Merge the given array into the given Collection.
072:             * @param array the array to merge (may be <code>null</code>)
073:             * @param collection the target Collection to merge the array into
074:             */
075:            public static void mergeArrayIntoCollection(Object array,
076:                    Collection collection) {
077:                if (collection == null) {
078:                    throw new IllegalArgumentException(
079:                            "Collection must not be null");
080:                }
081:                Object[] arr = ObjectUtils.toObjectArray(array);
082:                for (int i = 0; i < arr.length; i++) {
083:                    collection.add(arr[i]);
084:                }
085:            }
086:
087:            /**
088:             * Merge the given Properties instance into the given Map,
089:             * copying all properties (key-value pairs) over.
090:             * <p>Uses <code>Properties.propertyNames()</code> to even catch
091:             * default properties linked into the original Properties instance.
092:             * @param props the Properties instance to merge (may be <code>null</code>)
093:             * @param map the target Map to merge the properties into
094:             */
095:            public static void mergePropertiesIntoMap(Properties props, Map map) {
096:                if (map == null) {
097:                    throw new IllegalArgumentException("Map must not be null");
098:                }
099:                if (props != null) {
100:                    for (Enumeration en = props.propertyNames(); en
101:                            .hasMoreElements();) {
102:                        String key = (String) en.nextElement();
103:                        map.put(key, props.getProperty(key));
104:                    }
105:                }
106:            }
107:
108:            /**
109:             * Check whether the given Iterator contains the given element.
110:             * @param iterator the Iterator to check
111:             * @param element the element to look for
112:             * @return <code>true</code> if found, <code>false</code> else
113:             */
114:            public static boolean contains(Iterator iterator, Object element) {
115:                if (iterator != null) {
116:                    while (iterator.hasNext()) {
117:                        Object candidate = iterator.next();
118:                        if (ObjectUtils.nullSafeEquals(candidate, element)) {
119:                            return true;
120:                        }
121:                    }
122:                }
123:                return false;
124:            }
125:
126:            /**
127:             * Check whether the given Enumeration contains the given element.
128:             * @param enumeration the Enumeration to check
129:             * @param element the element to look for
130:             * @return <code>true</code> if found, <code>false</code> else
131:             */
132:            public static boolean contains(Enumeration enumeration,
133:                    Object element) {
134:                if (enumeration != null) {
135:                    while (enumeration.hasMoreElements()) {
136:                        Object candidate = enumeration.nextElement();
137:                        if (ObjectUtils.nullSafeEquals(candidate, element)) {
138:                            return true;
139:                        }
140:                    }
141:                }
142:                return false;
143:            }
144:
145:            /**
146:             * Check whether the given Collection contains the given element instance.
147:             * <p>Enforces the given instance to be present, rather than returning
148:             * <code>true</code> for an equal element as well.
149:             * @param collection the Collection to check
150:             * @param element the element to look for
151:             * @return <code>true</code> if found, <code>false</code> else
152:             */
153:            public static boolean containsInstance(Collection collection,
154:                    Object element) {
155:                if (collection != null) {
156:                    for (Iterator it = collection.iterator(); it.hasNext();) {
157:                        Object candidate = it.next();
158:                        if (candidate == element) {
159:                            return true;
160:                        }
161:                    }
162:                }
163:                return false;
164:            }
165:
166:            /**
167:             * Return <code>true</code> if any element in '<code>candidates</code>' is
168:             * contained in '<code>source</code>'; otherwise returns <code>false</code>.
169:             * @param source the source Collection
170:             * @param candidates the candidates to search for
171:             * @return whether any of the candidates has been found
172:             */
173:            public static boolean containsAny(Collection source,
174:                    Collection candidates) {
175:                if (isEmpty(source) || isEmpty(candidates)) {
176:                    return false;
177:                }
178:                for (Iterator it = candidates.iterator(); it.hasNext();) {
179:                    if (source.contains(it.next())) {
180:                        return true;
181:                    }
182:                }
183:                return false;
184:            }
185:
186:            /**
187:             * Return the first element in '<code>candidates</code>' that is contained in
188:             * '<code>source</code>'. If no element in '<code>candidates</code>' is present in
189:             * '<code>source</code>' returns <code>null</code>. Iteration order is
190:             * {@link Collection} implementation specific.
191:             * @param source the source Collection
192:             * @param candidates the candidates to search for
193:             * @return the first present object, or <code>null</code> if not found
194:             */
195:            public static Object findFirstMatch(Collection source,
196:                    Collection candidates) {
197:                if (isEmpty(source) || isEmpty(candidates)) {
198:                    return null;
199:                }
200:                for (Iterator it = candidates.iterator(); it.hasNext();) {
201:                    Object candidate = it.next();
202:                    if (source.contains(candidate)) {
203:                        return candidate;
204:                    }
205:                }
206:                return null;
207:            }
208:
209:            /**
210:             * Find a value of the given type in the given Collection.
211:             * @param collection the Collection to search
212:             * @param type the type to look for
213:             * @return a value of the given type found, or <code>null</code> if none
214:             * @throws IllegalArgumentException if more than one value of the given type found
215:             */
216:            public static Object findValueOfType(Collection collection,
217:                    Class type) throws IllegalArgumentException {
218:                if (isEmpty(collection)) {
219:                    return null;
220:                }
221:                Class typeToUse = (type != null ? type : Object.class);
222:                Object value = null;
223:                for (Iterator it = collection.iterator(); it.hasNext();) {
224:                    Object obj = it.next();
225:                    if (typeToUse.isInstance(obj)) {
226:                        if (value != null) {
227:                            throw new IllegalArgumentException(
228:                                    "More than one value of type ["
229:                                            + typeToUse.getName() + "] found");
230:                        }
231:                        value = obj;
232:                    }
233:                }
234:                return value;
235:            }
236:
237:            /**
238:             * Find a value of one of the given types in the given Collection:
239:             * searching the Collection for a value of the first type, then
240:             * searching for a value of the second type, etc.
241:             * @param collection the collection to search
242:             * @param types the types to look for, in prioritized order
243:             * @return a of one of the given types found, or <code>null</code> if none
244:             * @throws IllegalArgumentException if more than one value of the given type found
245:             */
246:            public static Object findValueOfType(Collection collection,
247:                    Class[] types) throws IllegalArgumentException {
248:                if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
249:                    return null;
250:                }
251:                for (int i = 0; i < types.length; i++) {
252:                    Object value = findValueOfType(collection, types[i]);
253:                    if (value != null) {
254:                        return value;
255:                    }
256:                }
257:                return null;
258:            }
259:
260:            /**
261:             * Determine whether the given Collection only contains a single unique object.
262:             * @param collection the Collection to check
263:             * @return <code>true</code> if the collection contains a single reference or
264:             * multiple references to the same instance, <code>false</code> else
265:             */
266:            public static boolean hasUniqueObject(Collection collection) {
267:                if (isEmpty(collection)) {
268:                    return false;
269:                }
270:                boolean hasCandidate = false;
271:                Object candidate = null;
272:                for (Iterator it = collection.iterator(); it.hasNext();) {
273:                    Object elem = it.next();
274:                    if (!hasCandidate) {
275:                        hasCandidate = true;
276:                        candidate = elem;
277:                    } else if (candidate != elem) {
278:                        return false;
279:                    }
280:                }
281:                return true;
282:            }
283:
284:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.