KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > core > collection > CollectionUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.core.collection;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * A utility class for working with attribute and parameter collections used by
26  * Spring Web FLow.
27  *
28  * @author Keith Donald
29  * @author Erwin Vervaet
30  */

31 public class CollectionUtils {
32
33     /**
34      * The shared, singleton empty iterator instance.
35      */

36     public static final Iterator JavaDoc EMPTY_ITERATOR = new EmptyIterator();
37
38     /**
39      * The shared, singleton empty attribute map instance.
40      */

41     public static final AttributeMap EMPTY_ATTRIBUTE_MAP = new LocalAttributeMap(Collections.EMPTY_MAP);
42
43     /**
44      * Private constructor to avoid instantiation.
45      */

46     private CollectionUtils() {
47     }
48
49     /**
50      * Factory method that adapts an enumeration to an iterator.
51      * @param enumeration the enumeration
52      * @return the iterator
53      */

54     public static Iterator JavaDoc toIterator(Enumeration JavaDoc enumeration) {
55         return new EnumerationIterator(enumeration);
56     }
57
58     /**
59      * Factory method that returns a unmodifiable attribute map with a single
60      * entry.
61      * @param attributeName the attribute name
62      * @param attributeValue the attribute value
63      * @return the unmodifiable map with a single element
64      */

65     public static AttributeMap singleEntryMap(String JavaDoc attributeName, Object JavaDoc attributeValue) {
66         return new LocalAttributeMap(attributeName, attributeValue);
67     }
68
69     /**
70      * Add all given objects to given target list. No duplicates will be added.
71      * The contains() method of the given target list will be used to determine
72      * whether or not an object is already in the list.
73      * @param target the collection to which to objects will be added
74      * @param objects the objects to add
75      * @return whether or not the target collection changed
76      */

77     public static boolean addAllNoDuplicates(List JavaDoc target, Object JavaDoc[] objects) {
78         if (objects == null || objects.length == 0) {
79             return false;
80         }
81         else {
82             boolean changed = false;
83             for (int i = 0; i < objects.length; i++) {
84                 if (!target.contains(objects[i])) {
85                     target.add(objects[i]);
86                     changed = true;
87                 }
88             }
89             return changed;
90         }
91     }
92     
93     /**
94      * Iterator iterating over no elements (hasNext() always returns false).
95      */

96     private static class EmptyIterator implements Iterator JavaDoc, Serializable JavaDoc {
97
98         private EmptyIterator() {
99         }
100
101         public boolean hasNext() {
102             return false;
103         }
104
105         public Object JavaDoc next() {
106             throw new UnsupportedOperationException JavaDoc("There are no elements");
107         }
108
109         public void remove() {
110             throw new UnsupportedOperationException JavaDoc("There are no elements");
111         }
112     }
113
114     /**
115      * Iterator wrapping an Enumeration.
116      */

117     private static class EnumerationIterator implements Iterator JavaDoc {
118
119         private Enumeration JavaDoc enumeration;
120
121         public EnumerationIterator(Enumeration JavaDoc enumeration) {
122             this.enumeration = enumeration;
123         }
124
125         public boolean hasNext() {
126             return enumeration.hasMoreElements();
127         }
128
129         public Object JavaDoc next() {
130             return enumeration.nextElement();
131         }
132
133         public void remove() throws UnsupportedOperationException JavaDoc {
134             throw new UnsupportedOperationException JavaDoc("Not supported");
135         }
136     }
137 }
Popular Tags