KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > ClassFilters


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.aop.support;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.springframework.aop.ClassFilter;
22 import org.springframework.util.Assert;
23 import org.springframework.util.ObjectUtils;
24
25 /**
26  * Static utility methods for composing
27  * {@link org.springframework.aop.ClassFilter ClassFilters}.
28  *
29  * @author Rod Johnson
30  * @author Rob Harrop
31  * @author Juergen Hoeller
32  * @since 11.11.2003
33  * @see MethodMatchers
34  * @see Pointcuts
35  */

36 public abstract class ClassFilters {
37
38     /**
39      * Match all classes that <i>either</i> (or both) of the given ClassFilters matches.
40      * @param cf1 the first ClassFilter
41      * @param cf2 the second ClassFilter
42      * @return a distinct ClassFilter that matches all classes that either
43      * of the given ClassFilter matches
44      */

45     public static ClassFilter union(ClassFilter cf1, ClassFilter cf2) {
46         Assert.notNull(cf1, "First ClassFilter must not be null");
47         Assert.notNull(cf2, "Second ClassFilter must not be null");
48         return new UnionClassFilter(new ClassFilter[] {cf1, cf2});
49     }
50
51     /**
52      * Match all classes that <i>either</i> (or all) of the given ClassFilters matches.
53      * @param classFilters the ClassFilters to match
54      * @return a distinct ClassFilter that matches all classes that either
55      * of the given ClassFilter matches
56      */

57     public static ClassFilter union(ClassFilter[] classFilters) {
58         Assert.notEmpty(classFilters, "ClassFilter array must not be empty");
59         return new UnionClassFilter(classFilters);
60     }
61
62     /**
63      * Match all classes that <i>both</i> of the given ClassFilters match.
64      * @param cf1 the first ClassFilter
65      * @param cf2 the second ClassFilter
66      * @return a distinct ClassFilter that matches all classes that both
67      * of the given ClassFilter match
68      */

69     public static ClassFilter intersection(ClassFilter cf1, ClassFilter cf2) {
70         Assert.notNull(cf1, "First ClassFilter must not be null");
71         Assert.notNull(cf2, "Second ClassFilter must not be null");
72         return new IntersectionClassFilter(new ClassFilter[] {cf1, cf2});
73     }
74
75     /**
76      * Match all classes that <i>all</i> of the given ClassFilters match.
77      * @param classFilters the ClassFilters to match
78      * @return a distinct ClassFilter that matches all classes that both
79      * of the given ClassFilter match
80      */

81     public static ClassFilter intersection(ClassFilter[] classFilters) {
82         Assert.notEmpty(classFilters, "ClassFilter array must not be empty");
83         return new IntersectionClassFilter(classFilters);
84     }
85
86
87     /**
88      * ClassFilter implementation for a union of the given ClassFilters.
89      */

90     private static class UnionClassFilter implements ClassFilter, Serializable JavaDoc {
91
92         private ClassFilter[] filters;
93
94         public UnionClassFilter(ClassFilter[] filters) {
95             this.filters = filters;
96         }
97
98         public boolean matches(Class JavaDoc clazz) {
99             for (int i = 0; i < this.filters.length; i++) {
100                 if (this.filters[i].matches(clazz)) {
101                     return true;
102                 }
103             }
104             return false;
105         }
106
107         public boolean equals(Object JavaDoc other) {
108             return (this == other || (other instanceof UnionClassFilter &&
109                     ObjectUtils.nullSafeEquals(this.filters, ((UnionClassFilter) other).filters)));
110         }
111
112         public int hashCode() {
113             return ObjectUtils.nullSafeHashCode(this.filters);
114         }
115     }
116
117
118     /**
119      * ClassFilter implementation for an intersection of the given ClassFilters.
120      */

121     private static class IntersectionClassFilter implements ClassFilter, Serializable JavaDoc {
122
123         private ClassFilter[] filters;
124
125         public IntersectionClassFilter(ClassFilter[] filters) {
126             this.filters = filters;
127         }
128
129         public boolean matches(Class JavaDoc clazz) {
130             for (int i = 0; i < this.filters.length; i++) {
131                 if (!this.filters[i].matches(clazz)) {
132                     return false;
133                 }
134             }
135             return true;
136         }
137
138         public boolean equals(Object JavaDoc other) {
139             return (this == other || (other instanceof IntersectionClassFilter &&
140                     ObjectUtils.nullSafeEquals(this.filters, ((IntersectionClassFilter) other).filters)));
141         }
142
143         public int hashCode() {
144             return ObjectUtils.nullSafeHashCode(this.filters);
145         }
146     }
147
148 }
149
Popular Tags