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.aop.support;
018:
019: import java.io.Serializable;
020:
021: import org.springframework.aop.ClassFilter;
022: import org.springframework.util.Assert;
023: import org.springframework.util.ObjectUtils;
024:
025: /**
026: * Static utility methods for composing
027: * {@link org.springframework.aop.ClassFilter ClassFilters}.
028: *
029: * @author Rod Johnson
030: * @author Rob Harrop
031: * @author Juergen Hoeller
032: * @since 11.11.2003
033: * @see MethodMatchers
034: * @see Pointcuts
035: */
036: public abstract class ClassFilters {
037:
038: /**
039: * Match all classes that <i>either</i> (or both) of the given ClassFilters matches.
040: * @param cf1 the first ClassFilter
041: * @param cf2 the second ClassFilter
042: * @return a distinct ClassFilter that matches all classes that either
043: * of the given ClassFilter matches
044: */
045: public static ClassFilter union(ClassFilter cf1, ClassFilter cf2) {
046: Assert.notNull(cf1, "First ClassFilter must not be null");
047: Assert.notNull(cf2, "Second ClassFilter must not be null");
048: return new UnionClassFilter(new ClassFilter[] { cf1, cf2 });
049: }
050:
051: /**
052: * Match all classes that <i>either</i> (or all) of the given ClassFilters matches.
053: * @param classFilters the ClassFilters to match
054: * @return a distinct ClassFilter that matches all classes that either
055: * of the given ClassFilter matches
056: */
057: public static ClassFilter union(ClassFilter[] classFilters) {
058: Assert.notEmpty(classFilters,
059: "ClassFilter array must not be empty");
060: return new UnionClassFilter(classFilters);
061: }
062:
063: /**
064: * Match all classes that <i>both</i> of the given ClassFilters match.
065: * @param cf1 the first ClassFilter
066: * @param cf2 the second ClassFilter
067: * @return a distinct ClassFilter that matches all classes that both
068: * of the given ClassFilter match
069: */
070: public static ClassFilter intersection(ClassFilter cf1,
071: ClassFilter cf2) {
072: Assert.notNull(cf1, "First ClassFilter must not be null");
073: Assert.notNull(cf2, "Second ClassFilter must not be null");
074: return new IntersectionClassFilter(
075: new ClassFilter[] { cf1, cf2 });
076: }
077:
078: /**
079: * Match all classes that <i>all</i> of the given ClassFilters match.
080: * @param classFilters the ClassFilters to match
081: * @return a distinct ClassFilter that matches all classes that both
082: * of the given ClassFilter match
083: */
084: public static ClassFilter intersection(ClassFilter[] classFilters) {
085: Assert.notEmpty(classFilters,
086: "ClassFilter array must not be empty");
087: return new IntersectionClassFilter(classFilters);
088: }
089:
090: /**
091: * ClassFilter implementation for a union of the given ClassFilters.
092: */
093: private static class UnionClassFilter implements ClassFilter,
094: Serializable {
095:
096: private ClassFilter[] filters;
097:
098: public UnionClassFilter(ClassFilter[] filters) {
099: this .filters = filters;
100: }
101:
102: public boolean matches(Class clazz) {
103: for (int i = 0; i < this .filters.length; i++) {
104: if (this .filters[i].matches(clazz)) {
105: return true;
106: }
107: }
108: return false;
109: }
110:
111: public boolean equals(Object other) {
112: return (this == other || (other instanceof UnionClassFilter && ObjectUtils
113: .nullSafeEquals(this .filters,
114: ((UnionClassFilter) other).filters)));
115: }
116:
117: public int hashCode() {
118: return ObjectUtils.nullSafeHashCode(this .filters);
119: }
120: }
121:
122: /**
123: * ClassFilter implementation for an intersection of the given ClassFilters.
124: */
125: private static class IntersectionClassFilter implements
126: ClassFilter, Serializable {
127:
128: private ClassFilter[] filters;
129:
130: public IntersectionClassFilter(ClassFilter[] filters) {
131: this .filters = filters;
132: }
133:
134: public boolean matches(Class clazz) {
135: for (int i = 0; i < this .filters.length; i++) {
136: if (!this .filters[i].matches(clazz)) {
137: return false;
138: }
139: }
140: return true;
141: }
142:
143: public boolean equals(Object other) {
144: return (this == other || (other instanceof IntersectionClassFilter && ObjectUtils
145: .nullSafeEquals(this .filters,
146: ((IntersectionClassFilter) other).filters)));
147: }
148:
149: public int hashCode() {
150: return ObjectUtils.nullSafeHashCode(this.filters);
151: }
152: }
153:
154: }
|