KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > GroupCategorySet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.core.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21
22 /**
23  * A special set to manage group categories. Group category sets are value
24  * objects and are therefore best used as static final fields to share a
25  * group category set between n {@link TextEditBasedChangeGroup}s.
26  * <p>
27  * Note: this class is not intended to be subclassed
28  * </p>
29  *
30  * @since 3.2
31  */

32 public class GroupCategorySet {
33     
34     /**
35      * Constant representing a group category set containing no
36      * group categories.
37      */

38     public static final GroupCategorySet NONE= new GroupCategorySet() {
39         public boolean contains(GroupCategory category) {
40             return false;
41         }
42         public List JavaDoc asList() {
43             return Collections.EMPTY_LIST;
44         }
45     };
46     
47     /**
48      * Creates a new group category set containing the union of the given two
49      * group category sets
50      *
51      * @param one the first set of group categories
52      * @param two the second set of group categories
53      *
54      * @return the union
55      */

56     public static GroupCategorySet union(GroupCategorySet one, GroupCategorySet two) {
57         Assert.isNotNull(one);
58         Assert.isNotNull(two);
59         // for performance we are using identity here. This is
60
// valid since group categories are value objects and
61
// therefore best used as static final fields.
62
if (one == two)
63             return one;
64         if (one == NONE)
65             return two;
66         if (two == NONE)
67             return one;
68         
69         Set JavaDoc combined= new HashSet JavaDoc();
70         combined.addAll(one.asList());
71         combined.addAll(two.asList());
72         return new GroupCategorySet(combined);
73     }
74     
75     private List JavaDoc/*<GroupCategory>*/ fContent;
76     
77     private GroupCategorySet() {
78         fContent= Collections.EMPTY_LIST;
79     }
80     
81     private GroupCategorySet(Set JavaDoc/*<GroupCategory>*/ categories) {
82         fContent= new ArrayList JavaDoc(categories);
83     }
84     
85     /**
86      * Creates a new list of group categories initialized
87      * with the given group category.
88      *
89      * @param category the first category
90      */

91     public GroupCategorySet(GroupCategory category) {
92         Assert.isNotNull(category);
93         fContent= new ArrayList JavaDoc(1);
94         fContent.add(category);
95     }
96     
97     /**
98      * Creates a new set of group categories initialized
99      * from the given array of group categories
100      *
101      * @param categories the initial group categories
102      */

103     public GroupCategorySet(GroupCategory[] categories) {
104         Assert.isNotNull(categories);
105         fContent= new ArrayList JavaDoc(categories.length);
106         for (int i= 0; i < categories.length; i++) {
107             if (!fContent.contains(categories[i]))
108                 fContent.add(categories[i]);
109         }
110     }
111
112     /**
113      * Returns whether the given category is contained
114      * in this set of group categories
115      *
116      * @param category the category to test containment for
117      *
118      * @return <code>true</code> if the category is contained
119      * in this set; otherwise <code>false</code>
120      */

121     public boolean contains(GroupCategory category) {
122         return fContent.contains(category);
123     }
124     
125     /**
126      * Returns whether one of the given categories is contained
127      * in this set of group categories
128      *
129      * @param categories the categories to test containment for
130      *
131      * @return <code>true</code> if one of the given categories is
132      * contained in this set; otherwise <code>false</code>
133      */

134     public boolean containsOneCategory(List JavaDoc/*<GroupCategory>*/ categories) {
135         for (Iterator JavaDoc iter= categories.iterator(); iter.hasNext();) {
136             GroupCategory category= (GroupCategory)iter.next();
137             if(contains(category))
138                 return true;
139         }
140         return false;
141     }
142     
143     /**
144      * Converts the group categories into a a unmodifiable
145      * list.
146      *
147      * @return an unmodifiable list containing all group
148      * categories
149      */

150     public List JavaDoc/*<GroupCategory>*/ asList() {
151         return Collections.unmodifiableList(fContent);
152     }
153 }
154
Popular Tags