KickJava   Java API By Example, From Geeks To Geeks.

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


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.aop.MethodMatcher;
23 import org.springframework.aop.Pointcut;
24 import org.springframework.util.Assert;
25 import org.springframework.util.ObjectUtils;
26
27 /**
28  * Convenient class for building up pointcuts. All methods return
29  * ComposablePointcut, so we can use a concise idiom like:
30  *
31  * <code>
32  * Pointcut pc = new ComposablePointcut().union(classFilter).intersection(methodMatcher).intersection(pointcut);
33  * </code>
34  *
35  * @author Rod Johnson
36  * @author Juergen Hoeller
37  * @author Rob Harrop
38  * @since 11.11.2003
39  * @see Pointcuts
40  */

41 public class ComposablePointcut implements Pointcut, Serializable JavaDoc {
42
43     /** use serialVersionUID from Spring 1.2 for interoperability */
44     private static final long serialVersionUID = -2743223737633663832L;
45
46     private ClassFilter classFilter;
47
48     private MethodMatcher methodMatcher;
49
50
51     /**
52      * Create a default ComposablePointcut, with <code>ClassFilter.TRUE</code>
53      * and <code>MethodMatcher.TRUE</code>.
54      */

55     public ComposablePointcut() {
56         this.classFilter = ClassFilter.TRUE;
57         this.methodMatcher = MethodMatcher.TRUE;
58     }
59
60     /**
61      * Create a ComposablePointcut based on the given Pointcut.
62      * @param pointcut the original Pointcut
63      */

64     public ComposablePointcut(Pointcut pointcut) {
65         Assert.notNull(pointcut, "Pointcut must not be null");
66         this.classFilter = pointcut.getClassFilter();
67         this.methodMatcher = pointcut.getMethodMatcher();
68     }
69
70     /**
71      * Create a ComposablePointcut for the given ClassFilter,
72      * with <code>MethodMatcher.TRUE</code>.
73      * @param classFilter the ClassFilter to use
74      */

75     public ComposablePointcut(ClassFilter classFilter) {
76         Assert.notNull(classFilter, "ClassFilter must not be null");
77         this.classFilter = classFilter;
78         this.methodMatcher = MethodMatcher.TRUE;
79     }
80
81     /**
82      * Create a ComposablePointcut for the given MethodMatcher,
83      * with <code>ClassFilter.TRUE</code>.
84      * @param methodMatcher the MethodMatcher to use
85      */

86     public ComposablePointcut(MethodMatcher methodMatcher) {
87         Assert.notNull(methodMatcher, "MethodMatcher must not be null");
88         this.classFilter = ClassFilter.TRUE;
89         this.methodMatcher = methodMatcher;
90     }
91
92     /**
93      * Create a ComposablePointcut for the given ClassFilter and MethodMatcher.
94      * @param classFilter the ClassFilter to use
95      * @param methodMatcher the MethodMatcher to use
96      */

97     public ComposablePointcut(ClassFilter classFilter, MethodMatcher methodMatcher) {
98         Assert.notNull(classFilter, "ClassFilter must not be null");
99         Assert.notNull(methodMatcher, "MethodMatcher must not be null");
100         this.classFilter = classFilter;
101         this.methodMatcher = methodMatcher;
102     }
103
104
105     /**
106      * Apply a union with the given ClassFilter.
107      * @param other the ClassFilter to apply a union with
108      * @return this composable pointcut (for call chaining)
109      */

110     public ComposablePointcut union(ClassFilter other) {
111         this.classFilter = ClassFilters.union(this.classFilter, other);
112         return this;
113     }
114
115     /**
116      * Apply an intersection with the given ClassFilter.
117      * @param other the ClassFilter to apply an intersection with
118      * @return this composable pointcut (for call chaining)
119      */

120     public ComposablePointcut intersection(ClassFilter other) {
121         this.classFilter = ClassFilters.intersection(this.classFilter, other);
122         return this;
123     }
124
125     /**
126      * Apply a union with the given MethodMatcher.
127      * @param other the MethodMatcher to apply a union with
128      * @return this composable pointcut (for call chaining)
129      */

130     public ComposablePointcut union(MethodMatcher other) {
131         this.methodMatcher = MethodMatchers.union(this.methodMatcher, other);
132         return this;
133     }
134
135     /**
136      * Apply an intersection with the given MethodMatcher.
137      * @param other the MethodMatcher to apply an intersection with
138      * @return this composable pointcut (for call chaining)
139      */

140     public ComposablePointcut intersection(MethodMatcher other) {
141         this.methodMatcher = MethodMatchers.intersection(this.methodMatcher, other);
142         return this;
143     }
144
145     /**
146      * Apply a union with the given Pointcut.
147      * <p>Note that for a Pointcut union, methods will only match if their
148      * original ClassFilter (from the originating Pointcut) matches as well.
149      * MethodMatchers and ClassFilters from different Pointcuts will never
150      * get interleaved with each other.
151      * @param other the Pointcut to apply a union with
152      * @return this composable pointcut (for call chaining)
153      */

154     public ComposablePointcut union(Pointcut other) {
155         this.methodMatcher = MethodMatchers.union(
156                 this.methodMatcher, this.classFilter, other.getMethodMatcher(), other.getClassFilter());
157         this.classFilter = ClassFilters.union(this.classFilter, other.getClassFilter());
158         return this;
159     }
160
161     /**
162      * Apply an intersection with the given Pointcut.
163      * @param other the Pointcut to apply an intersection with
164      * @return this composable pointcut (for call chaining)
165      */

166     public ComposablePointcut intersection(Pointcut other) {
167         this.classFilter = ClassFilters.intersection(this.classFilter, other.getClassFilter());
168         this.methodMatcher = MethodMatchers.intersection(this.methodMatcher, other.getMethodMatcher());
169         return this;
170     }
171
172
173     public ClassFilter getClassFilter() {
174         return this.classFilter;
175     }
176
177     public MethodMatcher getMethodMatcher() {
178         return this.methodMatcher;
179     }
180
181
182     public boolean equals(Object JavaDoc other) {
183         if (this == other) {
184             return true;
185         }
186         if (!(other instanceof ComposablePointcut)) {
187             return false;
188         }
189
190         ComposablePointcut that = (ComposablePointcut) other;
191         return ObjectUtils.nullSafeEquals(that.classFilter, this.classFilter) &&
192                 ObjectUtils.nullSafeEquals(that.methodMatcher, this.methodMatcher);
193     }
194
195     public int hashCode() {
196         int code = 17;
197         if (this.classFilter != null) {
198             code = 37 * code + this.classFilter.hashCode();
199         }
200         if (this.methodMatcher != null) {
201             code = 37 * code + this.methodMatcher.hashCode();
202         }
203         return code;
204     }
205
206     public String JavaDoc toString() {
207         return "ComposablePointcut: ClassFilter [" + this.classFilter +
208                 "], MethodMatcher [" + this.methodMatcher + "]";
209     }
210
211 }
212
Popular Tags